views:

448

answers:

1

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

A: 

Here's a piece of code that does what you want to do. For every corner, it finds the closest boundary pixel, and remembers the index of the coordinate.

For this to work, you have to get the boundary as 4-connected boundary (i.e. write Boundary = bwboundaries(bw,4);. If you cannot use 4-connected boundaries for some reason, the corner extraction becomes a bit more complicated, since in that case, concave corners are not part of the boundary (note that with 8-connected boundaries, the inside corners are 'round' when you look at your green line).

EDIT

Here's the full code that works for me

%# 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;

%# alternative shape
bw = zeros(100); 
bw(40:80,40:80) = 1;


%# Boundary Finding Section
[Boundary] = bwboundaries(bw,4); %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(size(bw),corners_1) % Convert subscripts

%% Here comes the new part

%# count corners
nCorners = length(Xcorners);
%# corner2boundaryIdx will contain the index of the boundary pixel
%# corresponding to a given corner
corner2boundaryIdx = zeros(nCorners,1);

%# remove last entry of Coordmat, because the first and the last pixel are
%# repeated
Coordmat(end,:) = [];

%# loop since we need to tread convex and concave corners differently
for i = 1:nCorners
    %# find distance of corner to boundary
    dist = sqrt((Coordmat(:,1)-Xcorners(i)).^2 + (Coordmat(:,2)-Ycorners(i)).^2);
    %# find index of closest boundary pixel. Use find
    %# instead of 2nd output of min, because we need to know how
    %# many closest indices there are in order to distinguish
    %# concave and convex corners. Convex corners are directly
    %# on the boundary, thus there is one minimum distance (0).
    %# Concave corners are just outside the boundary, thus, there
    %# are two minimum distances (1)
    minDistIdx = find(dist == min(dist));

    %# count how many minimum distances we have
    switch length(minDistIdx)
        case 1
            %# convex corners. Everything is simple
            corner2boundaryIdx(i) = minDistIdx;
        case 2
            %# concave corners. Take the index right in the middle. Note
            %# for this to work, you need to have 4-connected boundaries,
            %# otherwise, the concave corner is not part of the boundary
            %# becasue the boundary jumps along the diagonal. 
            %# If you have to use 8-connected boundaries, the 'good'
            %# difference is 1, and you need to calculate the coordinate of
            %# the concave corner from the "corner" and the two adjacent 
            %# boundary pixels.
            if diff(minDistIdx) == 2
            corner2boundaryIdx(i) = mean(minDistIdx);
            else
                error('boundary starts/stops at concave corner - case not handled yet')
            end
        otherwise
            error('%i minDist found for corner # %i',length(minDistIdx),i)
    end
end

%# All that is left to do is read the boundary pixel coordinates in the 
%# right order
corner2boundaryIdx = sort(corner2boundaryIdx);
orderedCorners = Coordmat(corner2boundaryIdx,:);

%# plot corner numbers for confirmation
hold on
for i = 1:nCorners
    text(orderedCorners(i,2),orderedCorners(i,1),num2str(i),'Color','r')
end
Jonas
Thanks for the response, but I noticed it doesn't quite work properly in some cases where the shape is convex. For example, if I set bw to a rectangle: bw = zeros(100) bw(40:80,40:80) = 1It identifies the same corner four times. Do you know why it does this?
James
In my hands, the code works perfectly fine with your additional example. Note that you should always run your full script (i.e. your script with my code added in). Also, you may want to replace `ind2sub(200,Corners_1)` with `ind2sub(size(bw),Corners_1)`
Jonas
I have now posted the full code that works fine for me. Can you check, please? If my code works fine and yours doensn't, you can use `visdiff(file1,file2)` to identify the differences.
Jonas
Thanks, it does work. I'm not sure why it isn't in the other case then.
James