Find the boundary of any edge in an image .

1 次查看(过去 30 天)
i need to find the edge of an image.

回答(1 个)

Thiago Henrique Gomes Lobato
编辑:Thiago Henrique Gomes Lobato 2019-11-3
Your input is just the image, so I can't check the solution in your exact data, but this should either way solve your problem or at least guide you to a solution, I tried to let the code as commented as possible for you to be able to follow it:
% Here I generate a contour data, which you already have
t = 0:0.001:1;
x = cos(2*pi*t);
y = 2*sin(2*pi*t);
Angle = 2*pi*t*180/pi;
DistanceFromCenter = sqrt(x.^2+y.^2);
% From here on is the reconstruction process
% Get back x and y contour coordinates
xValues = DistanceFromCenter.*cos(Angle/180*pi);
yValues = DistanceFromCenter.*sin(Angle/180*pi);
% Create Image Template
SizeImg = 400;
Img = zeros(SizeImg);
% Map numerical x-y Values to an index
xMapped = xValues-min(xValues)+1; %% Shift image to minimum 1 so index is never 0
yMapped = yValues-min(yValues)+1;
NormFactor = max(max(abs(xMapped),max(abs(yMapped))));
xMapped = round(xMapped/NormFactor*SizeImg); % Scale to maximum
yMapped = round(yMapped/NormFactor*SizeImg);
% Create Binary Contourn substituing the discrete boundary points for 1
for idx=1:length(xMapped)
Img(yMapped(idx),xMapped(idx)) = 1;
end
% If you want the image filled, fill the holes (This will only work if you
% have enough angle information, otherwise the contour will have too many holes for
% a reconstruction)
ImgFilled = imfill(Img==1,'holes'); % Img==1 is to transform the image in a binary
% Plots
figure
% Real Contour
subplot(1,4,1)
plot(xValues,yValues)
title('Real Contour')
axis square
% Discrete Contour
subplot(1,4,2)
plot(xMapped,yMapped)
title('Discrete Reconstructed Contour')
axis square
% Generate Image Contour
subplot(1,4,3)
imshow(Img)
title('Image Contour')
% Filled Image
subplot(1,4,4)
imshow(ImgFilled)
title('Filled Image Contour')

Community Treasure Hunt

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

Start Hunting!

Translated by