When using fspecial my figure/picture just ends up as a blue square?

3 views (last 30 days)
Kristin Aldridge
Kristin Aldridge on 6 Nov 2021
Commented: DGM on 8 Nov 2021
I have a picture (.tif) that I'm trying to filter to see the nuclei in since it is very faint and in black and white. However, when I use the code below I am just getting a blue figure and I'm not sure if I'm using the code correctly. I also want to use the sobel filter, which I have just been putting in place of the 'average' text but it gives the same thing. Any help is appreciated. Is it supposed to be put in a for loop?
uiopen(location of picture)
The code I was given to use is this:
hy = fspecial('average'); %average is the "type" of fspecial
hx = hy';
Iy = imfilter(double(pic), hy, 'replicate');
Ix = imfilter(double(pic), hx, 'replicate');
img = sqrt(Ix.^2 + Iy.^2);
figure, imshow(img,[]), title(Filtered')
img = img./max(max(img));
DGM on 7 Nov 2021
I had originally remarked on the redundant filtering steps, but it makes sense if it's an edge filter instead of an average filter. I'm assuming the use of 'average' here was just an artifact of debugging attempts.

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 7 Nov 2021
Edited: Image Analyst on 7 Nov 2021
@Kristin Aldridge you say "I have just been putting in place of the 'average' text but it gives the same thing." Note that in the last line of your code you are displaying the badly-named "I" instead of the processed image "img". img was displayed but you immediately blasted over it by displaying I.
imshow(img,[]), title(Filtered') % Show img, briefly
img = img./max(max(img));
imshow(I) % Replace img with I so now only I is showing
So of course your two displayed images look the same -- they are the same!
DGM on 8 Nov 2021
OP ran the which command without the -, so it returned the location of all()

Sign in to comment.

More Answers (1)

DGM on 6 Nov 2021
Edited: DGM on 6 Nov 2021
You're probably starting out with a uint8-scaled image and casting it as double. At that point, you can do whatever you want with the data, but you can't expect all the tools to know what to do with it. Several tools (including imshow) expect floating point images to be scaled [0 1]. Using the implicit normalization syntax like imshow(A,[]) doesn't work if the image is RGB.
The solution is simple. Use im2double() instead of double() unless you want to make sure that you take care to handle all the issues that might otherwise arise from using an improperly-scaled image.
pic = im2double(imread('peppers.png')); % use im2double once
I = rgb2gray(pic); % this isn't used
hy = fspecial('sobel');
hx = hy';
Iy = imfilter(pic, hy, 'replicate');
Ix = imfilter(pic, hx, 'replicate');
img = mat2gray(sqrt(Ix.^2 + Iy.^2));
imshow(img), title('Filtered')
Alternatively, you could use a prenormalized gradient filter and then skip the mat2gray() call at the end.
% Scharr filter prenormalized for gradient calcs
hy = [47 162 47; 0 0 0; -47 -162 -47]/sqrt(91780);
Image Analyst
Image Analyst on 7 Nov 2021
No need to do that. Did you see my Answer below?
Also, attach your picture and current version of your code.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!