Compare Image Filtering Using Correlation and Convolution
This example shows how to filter images using either correlation or convolution operations.
Correlation and convolution are two closely related operations that filter images in the spatial domain. For both operations, the value of an output pixel is a weighted sum of the values of the pixels in the neighborhood of the input pixel. The matrix of weights is called the kernel or the filter. More specifically, the correlation operation uses a correlation kernel, and the convolution operation uses a convolution kernel. Performing correlation with a correlation kernel gives the same results as performing convolution with a convolution kernel.
Some functions perform correlation and expect an input correlation kernel, whereas other functions perform convolution and expect an input convolution kernel. If you have a kernel of the other type, then you can convert the kernel to the expected type by rotating the kernel by 180 degrees about the center element. This example shows how the operations are equivalent and how to convert between the types of kernels.
Start by reading an image that you want to filter.
I = imread("coins.png");
Because these filters can yield negative values, convert the data type from uint8
to double
. Typically, you would convert the data type using the im2double
function so that input pixel values are in the range [0, 1]. For simplicity in demonstrating the operations, this code instead converts the data type using the double
function so that pixels are integer-valued.
I = double(I);
Display the original image.
imshow(I,[])
title("Original Image")
For a quantitative comparison of the operations, this example also displays the values for a 5-by-5 pixel region in the image. Specify the region and display the values of that region.
regionX = 119; regionY = 160; cropRect = [regionX regionY 4 4]; region = imcrop(I,cropRect)
region = 5×5
147 163 169 122 51
152 168 148 71 48
176 167 97 44 57
185 121 45 50 63
132 55 43 61 67
Filter Using Correlation Kernels
Define the correlation kernel.
corrKernel = [1 2 1; 0 0 0; -1 -2 -1]
corrKernel = 3×3
1 2 1
0 0 0
-1 -2 -1
Filter the image using the imfilter
function, which performs correlation by default. Specify the correlation kernel as input to the imfilter
function. Then, display the correlation-filtered image.
filteredImage = imfilter(I,corrKernel);
imshow(filteredImage,[])
title("Correlation with Correlation Kernel")
Extract the filtered 5-by-5 pixel region, and display the pixel values.
filteredRegion = imcrop(filteredImage,cropRect)
filteredRegion = 5×5
5 22 143 254 158
-69 35 218 222 55
-39 164 274 130 -13
190 322 203 10 -40
389 281 36 -54 -32
Because correlation and convolution are related, observe that you can get the same result by filtering the entire image using convolution and specifying the filter as the rotated correlation kernel.
Filter the entire image using the conv2
function, which performs convolution. Specify the rotated correlation kernel as input to the conv2
function.
filteredImageConv = conv2(I,rot90(corrKernel,2),"same");
Alternatively, you can perform convolution by using the imfilter
function with the rotated correlation kernel and specifying "conv"
as an optional input argument, as shown in this code:
filteredImageConv = imfilter(I,rot90(corrKernel,2),"conv");
Display the convolution-filtered image.
imshow(filteredImageConv,[])
title("Convolution with Rotated Correlation Kernel")
Extract the filtered 5-by-5 pixel region, and display the pixel values. These values are identical to the values when performing correlation with a correlation kernel.
filteredRegionConv = imcrop(filteredImageConv,cropRect)
filteredRegionConv = 5×5
5 22 143 254 158
-69 35 218 222 55
-39 164 274 130 -13
190 322 203 10 -40
389 281 36 -54 -32
Confirm the equality of the convolution and correlation operations.
isequal(filteredImage,filteredImageConv)
ans = logical
1
Filter Using Convolution Kernels
Define the convolution kernel.
convKernel = [0 -1 0; -1 0 1; 0 1 0]
convKernel = 3×3
0 -1 0
-1 0 1
0 1 0
Filter the entire image using the conv2
function, which performs convolution. Specify the convolution kernel as input to the conv2
function.
filteredImage = conv2(I,convKernel,"same");
Alternatively, you can perform convolution by using the imfilter
function with the convolution kernel and specifying "conv"
as an optional input argument, as shown in this code:
filteredImage = imfilter(I,convKernel,"conv");
Display the convolution-filtered image.
imshow(filteredImage,[])
title("Convolution with Convolution Kernel")
Extract the filtered 5-by-5 pixel region, and display the pixel values.
filteredRegion = imcrop(filteredImage,cropRect)
filteredRegion = 5×5
-3 -28 69 211 108
-43 0 169 178 3
-39 126 226 61 -36
97 252 125 -35 -26
249 170 -20 -41 -13
Because correlation and convolution are related, observe that you can get the same result by filtering the entire image using correlation and specifying the filter as the rotated convolution kernel.
Filter the entire image using the imfilter
function, which performs correlation by default. Specify the rotated convolution kernel as input to the imfilter
function. Then, display the correlation-filtered image.
filteredImageCorr = imfilter(I,rot90(convKernel,2));
imshow(filteredImageCorr,[])
title("Correlation with Rotated Convolution Kernel")
Extract the filtered 5-by-5 pixel region, and display the pixel values. These values are identical to the values when performing convolution with a convolution kernel.
filteredRegionCorr = imcrop(filteredImageCorr,cropRect)
filteredRegionCorr = 5×5
-3 -28 69 211 108
-43 0 169 178 3
-39 126 226 61 -36
97 252 125 -35 -26
249 170 -20 -41 -13
Confirm the equality of the convolution and correlation operations.
isequal(filteredImage,filteredImageCorr)
ans = logical
1
See Also
imfilter
| filter2
| conv2
| convn