图像的边缘检测可以通过梯度算子来实现,诸如,Roberts,sobel,prewitt,canny等,它的本质也就是对一幅图像求其梯度图像,那么在matlab中通过edge函数求得的边缘和我们直接使用梯度算子求得一幅图像的梯度图像会不会有区别呢?在matlab环境下,通过这样一个实验来看一下结果:
Step1: read an image into workspace.
rgb = imread(pears.png);
imshow(rgb);
I = rgb2gray(rgb);
figure,imshow(I)
step2:Detect the edge of the gray image,I,using edge() function.Here we use 'Sobel' as the parameter.
BW = edge(I,'sobel');
figure,imshow(BW),title('sobel edge detection')
step3:Now we calculate the gradient of the image through sobel method.
hy = fspecial('sobel');
hx = hy';
Iy = imfilter(double(I),hy,'replicate');
Ix = imfilter(double(I),hx,'replicate');
gradmag = sqrt(Ix.^2+Iy.^2);
figure,imshow(gradmag,[]),title('gradmag')
OK,so now,have u found what's the difference?
很明显的一点是:edge()函数的输出为二值图像,而我们计算所的梯度图像仍为灰度图像。