Reputation: 27
I have one image. I want to get the coordinates in order so that the image can be redrawn. The image may be complex. I want to get sampled corner coordinates to redraw the outline of the image.
Attempt 1: corner
function
I tried to detect the corner using corner()
in matlab but it is not giving exact point. The corners are lying outside. The original image is attached below.
My code for corner detection is:
It=rgb2gray(I);
Itt=im2bw(It);
Itt1=corner(Itt);
imshow(Itt);
hold on
plot(Itt1(:,1), Itt2(:,2), 'r*');
The output of corner detection is attached below:
Problem with corner
: If you zoom the image, you will find that the some corners don't lie on boundaries. So, please suggest some efficient and good working method.
Attempt 2: bwtraceboundaries
I also tried using bwtraceboundaries
and corners to order the corner in terms of bwtraceboundaries
output but the corner is not being exactly detected.
Question: Can you please suggest how can I detect the corner or is there any other method to extract the sampled corner points from the image so that the outline can be redrawn?
Upvotes: 1
Views: 515
Reputation: 11072
You can use bwboundaries
to achieve what you want. It returns all the sorted boundary positions as a cell array:
B = bwboundaries(Itt);
imshow(Itt);
hold on
B = B{2}; % select the desired boundary (the second in this case)
plot(B(:,2), B(:,1), 'b-'); % draw a connected line
If you want to obtain only the corner points, you can filter them out manually by checking if the point lies in the middle of the next and previous point as follows:
iBremove = []; % redundant indexes in B
for k=2:length(B)-1 % loop over all the points, except the first and last one
Bk = (B(k-1, :) + B(k+1, :))/2; % interpolate the previous and next point
if all(Bk == B(k, :)) % check if point k is redundant
iBremove = [iBremove k]; % add to the list of indexes to remove
end
end
B(iBremove, :) = []; % remove the redundant points out of B
plot(B(:,2), B(:,1), 'r*-'); % draw a connected line
Upvotes: 0