Convolving 2D image with 1D filter

Hi,
Say I have an gray image file with size 256 x 256 and I want to filter this image where the filter response is dirac(n-3). I tried the following code but MATLAB plots the empty image. Can you please tell me what I do incorrectly? Any help would be appreciated.
load image.mat
h = [0 0 0 1];
filteredImage = conv2(img,h,'same');
figure(1)
colormap(gray)
image(filteredImage)

Answers (2)

That works for imagesc of doubles:
I2 = peaks(123);
h = [0 0 0 1];
imagesc(I2)
pause(0.5)
imagesc(conv2(I2,h,'same'))
Did you expect something different?
HTH

1 Comment

You have to think about what it means to convolve something with a shifted Dirac. How will that smooth the image, and why. What happens you look at the convolution with h, what happens when you run my example? Does the image shift, smoothen, etc? If so why?

Sign in to comment.

No, Once I tried the same, I have get the resultant image, I have shown the result on same size gray Image
img=rgb2gray(imread('Baboon.jpeg'));
h = [0 0 0 1];
filteredImage = conv2(img,h,'same');
figure(1)
colormap(gray)
image(filteredImage)
Ensure that, once you load the mat file having the variable name as "img"
or share the whos img?

2 Comments

Sir, img is the variable in that .mat file, I have a cameraman picture (cameraman.tif). I expect that filter will blur the image but the original and the filtered image are the same? Is it what we expect or is there any difference if we convolve the image row-by-row?
"I expect that filter will blur the image but the original and the filtered image are the same?"
No, the both images are not same, img and filteredImage, see the Maths data, but yes it less affect on the original image.
Please consider the small example of image say 5x5 or 7x7 magic(7) and apply the same, the original and result are not same. You also check easily in small data sample to "How does conv2 operation works"
>> magic(7)
ans =
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
>> filteredImage = conv2(ans,h,'same')
filteredImage =
0 30 39 48 1 10 19
0 38 47 7 9 18 27
0 46 6 8 17 26 35
0 5 14 16 25 34 36
0 13 15 24 33 42 44
0 21 23 32 41 43 3
0 22 31 40 49 2 11
Hope it helps!

Sign in to comment.

Asked:

on 8 Feb 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!