Domanda

So I am applying a Gaussian kernel to an ultrasound image, made up of integers values in the range of 0 to 255, like this

filteredImage = imfilter(image,kernel,'conv','same')

using imshow(filteredImage) I get a nicely blurred image:

enter image description here

Then, instead using

convImage = conv2(image,kernel,'same')

I get the following image

enter image description here

Aren't these two functions, used this way, supposed to produce the same kind of output?

È stato utile?

Soluzione

imfilter and conv2 aren't exactly the same (imfilter is like conv2 with the filter flipped). If you use 'conv' then they are the same.

First you can check that you are using the correct image types using imfilter in the other way. Check this:

out1=conv2(double(image),kernel,'same');  
out_conv=uint8(out1); 
old_imfilter=imfilter(image,kernel,'same');
new_imfilter=imfilter(image,kernel(end:-1:1,end:-1:1),'same');

new_imfilter and out_conv should be the same. If so, you can do it with your method:

convImage = uint8(conv2(double(image),kernel,'same'));
filteredImage = imfilter(image,kernel,'conv','same');

Now filteredImage and convImage should be the same.

Altri suggerimenti

The difference between imfilter and conv2 when used in this way is that conv2 performs a conversion to double...

varargin{k} = double(varargin{k});

...while imfilter does not.

Images of type uint8 have a range of [0,255] while those of type double typically have range [0,1]. imshow works under these assumptions and so, if passed a double image will display any value greater than 1 as white.

As your original image was uint8, the result after calling conv2 will still have values in the range [0,255] (despite the conversion to double) and so will not be displayed correctly by imshow. A number of fixes (some suggested already) are:

  • Allow imshow to detect the range: imshow(convImage,[])
  • Normalize your result: imshow(convImage/255)
  • Convert your result to uint8 before display: imshow(uint8(convImage))
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top