Finding edge and corner values of an image in matlab
- by James
Hi, this problem links to two other questions i've asked on here.
I am tracing the outline of an image and plotting this to a dxf file. I would like to use the bwboundaries function to find the coordinates of the edges of the image, find the corner coordinates using the cornermetric function and then remove any edge coordinates that are not a corner.
The important thing I need to be able to do is keep the order of the corner elements obtained from bwboundaries, so that the section traces properly. The dxf function I have that draws from the coordinates draws lines between coordinates that are next to each other, so the line has to be drawn "around" the section rather than straight between the corner points.
The reason I am doing this is because there are less coordinates obtained this way, so it is easier to amend the dxf file (as there are less points to manipulate).
The code I have so far is:
%# Shape to be traced
bw = zeros(200);
bw(20:40,20:180) = 1;
bw(20:180,90:110) = 1;
bw(140:180,20:185) = 1;
%# Boundary Finding Section
[Boundary] = bwboundaries(bw); %Traces the boundary of each section
figure, imshow(bw); hold on;
colors=['b' 'g' 'r' 'c' 'm' 'y'];
for k=1:length(Boundary)
perim = Boundary{k}; %Obtains perimeter coordinates (as a 2D matrix) from the cell array
cidx = mod(k,length(colors))+1;% Obtains colours for the plot
plot(perim(:,2), perim(:,1),...
colors(cidx),'LineWidth',2);
end
Coordmat = cell2mat(Boundary) %Converts the traced regions to a matrix
X = Coordmat(:,1)
Y = Coordmat(:,2) % This gives the edge coordinates in matrix form
%% Corner Finding Section (from Jonas' answer to a previous question
%# get corners
cornerProbability = cornermetric(bw);
cornerIdx = find(cornerProbability==max(cornerProbability(:)));
%# Label the image. bwlabel puts 1 for the first feature, 2 for the second, etc.
%# Since concave corners are placed just outside the feature, grow the features
%# a little before labeling
bw2 = imdilate(bw,ones(3));
labeledImage = bwlabel(bw2);
%# read the feature number associated with the corner
cornerLabels = labeledImage(cornerIdx);
%# find all corners that are associated with feature 1
corners_1 = cornerIdx(cornerLabels==1)
[Xcorners, Ycorners] = ind2sub(200,corners_1) % Convert subscripts
The code I have is, to give a matrix Xfin for the final x coordinates (which are on the edge AND at a corner.
Xfin = zeros(length(X),1)
for i = Xcorners
XFin(i) = Xcorners
if i~= Xcorners
XFin(i) = []
end
end
However, this does not work correctly, because the values in the solution are sorted into order, and only one of each value remains. As I said, I would like the corner elements to be in the same order as obtained from bwboundaries, to allow the image to trace properly.
Thanks