How to create auto polyline in the image
Show older comments
Hello,
I am so new to MATLAB coding and for my original code I need to draw auto polyline for my figure. I have written a code but it is not following the original area, so i am wondering if any one can help me to fix it. I have attached the image below (gray image) and I want to learn the x and y coordinate of the area below red line but my code gives something else like in results of bwperim. Can someone help me to solve this? problem? I have also added the original image and my script.
Thank you
clear
close all
clc
fontSize = 16;
skip = 10;
%directory for matlab.
% Load Image
I = imread('Band Contrast 1-1.jpg');
size(I);
% Workaround to get back to a binary image
% I(:,[1:86 403:end],:) = 0;
% I([1:31 500:end],:,:) = 0;
level=graythresh(I)
%%
B = imbinarize(I,0.5);
figure()
imshowpair(I,B,'montage')
%% remove features.
clc
Bfilled=imfill(B,'holes');
Bcc=bwconncomp(Bfilled);
voids=regionprops(Bcc,'centroid','Area','Circularity','MajorAxisLength','MinorAxisLength','Eccentricity','Orientation','EquivDiameter','Extent','MaxFeretProperties','MinFeretProperties');
%
CCC=voids.Centroid;
CENTROID=zeros(size(voids,1),2);
icc=0;
for ii=1:2:size(CCC,2)
icc=icc+1;
CENTROID(icc,:)=CCC(ii,ii+1);
end
%%
clc
Bvoids=ismember(labelmatrix(Bcc),find([voids.Area]>1));
% condition=CENTROID(:,1)<=560 & CENTROID(:,2)<=40 & CENTROID(:,1)>=500 & CENTROID(:,2)>=0;
condition=[voids.Area]>1000 & [voids.Area]<2000;
BVOID=ismember(labelmatrix(Bcc),find(~condition));
sum(condition)
figure()
imshowpair(Bvoids,BVOID,'montage')
%%
% Show Initial Image
hf = figure('Units','Normalized','OuterPosition',[0 0 1 1]);
subplot(1,2,1), imshow(BVOID)
title('Initial Binary Image','fontSize',fontSize)
% This step is not required, but speeds up the use of boundary later.
Bperimeter = bwperim(BVOID,26);
% Bperimeter = imdilate(BVOID,offsetstrel('ball',100,90));
subplot(1,2,2), imshow(Bperimeter)
title('Result of bwperim','fontSize',fontSize)
% Get x,y coordinates of perimeter (column index and row index,
% respectively)
[y,x] = find(Bperimeter);
k = boundary(x,y,1); %use boundary with shrink factor of 1 to find vertices
% Back to the Initial Binary image, add the polyline using recently
% obtained vertices
idx = [k(1:skip:end);k(1)];
drawpolygon('Position',[x(idx) y(idx)])
%%
clc
figure()
plot(x(idx),-y(idx)+(max(y(idx))),'k.-','MarkerSize',20)


Accepted Answer
More Answers (0)
Categories
Find more on Images in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



