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

4 ビュー (過去 30 日間)
Kristin Aldridge
Kristin Aldridge 2021 年 11 月 6 日
コメント済み: DGM 2021 年 11 月 8 日
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)
pic=drosophila_embryo_wild_type;
I=rgb2gray(pic);
imshow(I)
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));
imshow(I)
  2 件のコメント
Image Analyst
Image Analyst 2021 年 11 月 7 日
編集済み: Image Analyst 2021 年 11 月 7 日
You forgot to attach your image. Also, I doubt you need Sobel filter. I'd say 90% of the time people use edge detection, they really don't need to. We'll know once you show us your image after reading this:
Anyway, in the code above you're just going through a bunch of lines to blur the image, which can be done in a single line of code with a call to imfilter()
blurredImage = imfilter(img, ones(3)/9);
DGM
DGM 2021 年 11 月 7 日
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.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2021 年 11 月 7 日
編集済み: Image Analyst 2021 年 11 月 7 日
@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!
  12 件のコメント
Image Analyst
Image Analyst 2021 年 11 月 7 日
You should never move toolbox functions. Period. Do not move files such as imfilter.m to your code's folder. That said, it should have worked since it's the same imfilter(), but in general it's a very bad idea to move or modify toolbox functions.
And you should also not create your own functions or variables that have the same name as built-in functions. You can check by calling the which command.
The thing I'm confused about is you said "I had the imfilter.m function in the same folder as my code" yet when you did which imfilter you got this:
built-in (/Applications/MATLAB_R2021a.app/toolbox/matlab/ops/all)
which meant that the imfilter function WAS NOT in the same folder as your code, unless you put your code actually inside the toolbox folder (also a very bad idea).
And if you had your own imfilter in your own folder, then which would have shown two imfilters:
>> which -all imfilter
built-in (/Applications/MATLAB_R2021a.app/toolbox/matlab/ops/all)
c:\users\kristin\her code\some project\imfilter.m
DGM
DGM 2021 年 11 月 8 日
OP ran the which command without the -, so it returned the location of all()

サインインしてコメントする。

その他の回答 (1 件)

DGM
DGM 2021 年 11 月 6 日
編集済み: DGM 2021 年 11 月 6 日
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);
  4 件のコメント
Kristin Aldridge
Kristin Aldridge 2021 年 11 月 7 日
imfilter has not worked for me before. I have the Image Processing Toolbox installed as well, so I'm not sure where it is going wrong I just wanted to make sure it wasn't in my code before I start uninstalling and reinstalling everything.
Image Analyst
Image Analyst 2021 年 11 月 7 日
No need to do that. Did you see my Answer below?
Also, attach your picture and current version of your code.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by