Detecting edges of an image doesn't work in Matlab

匿名 (未验证) 提交于 2019-12-03 01:03:01

问题:

I'm working on a script detecting edges of an image.

Here is the script:

clear all; close all; clc;  c = rgb2gray(imread('image_S004_I0004.jpg')); c = double(c); k = imnoise(c, 'salt & pepper', 0.01);   gg = [-1 0 1;-2 0 2; -1 0 1]; gh = gg';  grad_g = conv2(k, gg); grad_h = conv2(k, gh); grad = sqrt(grad_g.^2 + grad_h.^2);  [r s] = size(grad); T = 80; for ii = 1:r     for jj = 1:s          if grad(ii, jj) < T             thresh_grad(ii, jj) = 0;         else              thresh_grad(ii, jj) = 1;         end     end end  figure() subplot(121); imshow(uint8(c)); subplot(122); imshow(thresh_grad); 

Here is what I always get:

On the left is an original image, on the right should be an image with detected edges (as you can see in the script, I have implemented some noise on the image - has to be there). But I get literally nothing, not matter what the value of threshold T is.

Could you please help me to find my mistake?

回答1:

The problem in your code is right before you apply the noise. You are casting the image to double prior to calling imnoise. By doing this, double precision images are assumed to have a dynamic range of [0,1] and so the output of imnoise would be clipped to the [0,1] range. This means that your threshold of 80 would therefore be unsuitable because there will never be any gradient values that would exceed the value of 80 so everything is visualized as black.

In addition, thresh_grad is not defined and it's recommended you pre-allocate the image prior to using it. Simply do thresh_grad = zeros(size(grad)); prior to the double for loop.

As such, call double after you make the call to imnoise which would make the image still be in uint8 and then convert to double for the purposes of convolution. By doing this I managed to get output. I don't have access to your image, but I used the cameraman.tif image that's built-into MATLAB's image processing toolbox.

Therefore:

c = imread('cameraman.tif'); k = imnoise(c, 'salt & pepper', 0.01);  k = double(k); % Change  gg = [-1 0 1;-2 0 2; -1 0 1]; gh = gg';  grad_g = conv2(k, gg); grad_h = conv2(k, gh); grad = sqrt(grad_g.^2 + grad_h.^2);  [r, s] = size(grad); thresh_grad = zeros(size(grad)); % Added T = 80; for ii = 1:r     for jj = 1:s          if grad(ii, jj) < T             thresh_grad(ii, jj) = 0;         else              thresh_grad(ii, jj) = 1;         end     end end  figure() subplot(121); imshow(uint8(c)); subplot(122); imshow(thresh_grad); 

I get:


As for future development, I recommend you use im2double to actually convert the images to double precision, which would also convert the data into a [0,1] range. You would thus need to change the threshold from 80 to 80/255 as the threshold of 80 was originally designed for uint8 images.

Finally, when you show the original image you can get rid of the uint8 casting.

For completeness:

c = imread('cameraman.tif'); c = im2double(c); % Change k = imnoise(c, 'salt & pepper', 0.01);   gg = [-1 0 1;-2 0 2; -1 0 1]; gh = gg';  grad_g = conv2(k, gg); grad_h = conv2(k, gh); grad = sqrt(grad_g.^2 + grad_h.^2);  [r, s] = size(grad); thresh_grad = zeros(size(grad)); % Added T = 80 / 255; % Change for ii = 1:r     for jj = 1:s          if grad(ii, jj) < T             thresh_grad(ii, jj) = 0;         else              thresh_grad(ii, jj) = 1;         end     end end  figure() subplot(121); imshow(c); subplot(122); imshow(thresh_grad); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!