From: Graham on
Hi there,

I am trying to make low and high filters using the code below. I'm starting with a 253x238 grayscale image I which I converted to type double -- very similar to this one: http://www.scientificamerican.com/media/inline/blog/Image/MRI_head_side.jpg

The low pass and high pass kernals are shown...

% Low pass filter
LowKernel = [ 1 1 1; 1 1 1; 1 1 1 ];
Conv_low = conv2(LowKernel, I);
Scaled_low = imagesc(Conv_low);
figure, imshow(Scaled_low);

% High pass filter
HighKernel = [ -1 -1 -1; -1 4 -1; -1 -1 -1 ];
Conv_high = conv2(HighKernel, I);
Scaled_high = imagesc(Conv_high);
figure, imshow(Scaled_high);

This give me very strange results: a color image (fig1), a greyscale image (fig2) and a white dot (fig3)!

How do I implement these filters using conv2 and imagesc properly? Please help!
From: David Young on
Are you sure you are converting the image to grey scale? I get sensible-looking figures with the image downloaded from your link, using this:

>> im = imread('MRI_head_side.jpg');
>> I = double(rgb2gray(im))/256;
>> LowKernel = [ 1 1 1; 1 1 1; 1 1 1 ];
>> Conv_low = conv2(LowKernel, I);
>> imshow(Conv_low, []);
>> HighKernel = [ -1 -1 -1; -1 4 -1; -1 -1 -1 ];
>> Conv_high = conv2(HighKernel, I);
>> imshow(Conv_high, []);

I've never used imagesc so I can't comment on that - but since imshow(I, []) does its own scaling I'm not sure it's needed.

Other points:

Your high-pass kernel doesn't sum to zero, so it adds the high frequency components to the original image, multiplied by a negative constant. If you just want high spatial frequencies, you need an 8 in the centre position.

You may want to use the 'same' option with conv2 to preserve the size of the arrays if you are going to process them further (but if you do this, put the kernel after the image in the arguments).

If you start using bigger kernels and the convolution takes longer than you would like, it might be worth doing some timing tests with convolve2, available from
the file exchange at:

http://www.mathworks.com/matlabcentral/fileexchange/22619-fast-2-d-convolution

but again note that this function should be called as convolve2(Image, Kernel), not with the kernel as first argument.
From: ImageAnalyst on
Just to add to what David said, from the help:
"h = imagesc(...) returns the handle for an image graphics object."
So with imshow, you're simply showing the double scalar value which is
the handle to the axes. You're not showing the image itself. You
don't need both of these. imshow will scale the image (for display
only - it doesn't actually change image values) by doing it this way
(like you already did but you need to pass it an image)

imshow(imageArray, []);

and like I said, get rid of imagesc().

If you want to scale an image (change the pixel values) you can use
regular math, or you can use this function: imadjust().
From: Graham on
Many thanks for these replies :) Problem solved.