I wrote this code for detecting the edge of the image but the result is different that the built in function result. what is the problem ?

3 Ansichten (letzte 30 Tage)
%Read the original RGB image.
hand = imread('images.jpeg');
figure,imshow(hand);
title('original image')
%extract the green channel
g=hand(:,:,2);
figure,imshow(g);
%Prewitt filter
px=[-1 0 1; -1 0 1; -1 0 1];
cx=filter2(px,g);
figure,imshow(cx/255)
title('horizontal');
py=px';
cy=filter2(py,g);
figure,imshow(cy/255);
title('vertical');
edd=sqrt(cx.^2+cy.^2);
figure,imshow(edd/255);
title('Prewitt filter')

Antworten (1)

DGM
DGM am 9 Okt. 2024
IPT edge() uses multiple different methods for finding edges, then it binarizes and skeletonizes the results. Since we still don't know how edge() was used, here's an attempt to demonstrate a similar result using basic filters.
% a grayscale image
inpict = imread('cameraman.tif');
inpict = im2double(inpict); % put it in unit-scale to begin with
% the reference edgemap
% who knows what threshold or other parameters were used
E = edge(inpict,'prewitt'); % just use defaults
% approximate the binary edgemap using basic tools
% calculate gradient magnitude from derivative estimates
fkx = [-1 0 1; -1 0 1; -1 0 1]; % prewitt
cx = imfilter(inpict,fkx,'replicate');
cy = imfilter(inpict,fkx.','replicate');
Gmag = hypot(cx,cy);
% binarize and skeletonize
Ep = imbinarize(mat2gray(Gmag));
Ep = bwskel(Ep);
% compare
imshow([E Ep])
That's hard to see on the forum, but maybe this is clearer.
imshow(double(cat(3,E,Ep,Ep)))
It's not exact, but for a guess it's close. Edge() does the prewitt/sobel methods in MEX, so it's anybody's guess how the binarization and thinning is performed.

Community Treasure Hunt

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

Start Hunting!

Translated by