How to detect corners of irregular binary shape?

I have a lot of binary images similar like this:
Now I want to detect the 4 corners of this (almost) rectangle. It is however important these corners are detected always at the same place and do not change when my image changes a little bit. (I am detecting a sample that undergoes some forces so the images change slightly over time). So for example at the top right, I do not want my detection to switch constantly between these two possible edge points. I just want to detect the highest possible edge point at the 4 corners.
Methods I have tried:
  1. Calculate all edges with function edge and then calculate the points closest to the corners of the entire image. This will however give the problem of not having a constant corner point, like I already explained.
  2. Take the corners of the uppermost (and lowermost) row. This will give points somewhere in the middle because the shape is a little curved at the top.
I am honestly really stuck and help would be greatly appreciated!

 Accepted Answer

You might try pgonCorners from the File Exchange:
figure
runIt(load('BWimage1').BW);
figure
runIt(load('BWimage400').BW);
function [X,Y]=runIt(BW)
V=pgonCorners(BW,8);
[I,J]=deal(V(:,1), V(:,2));
[~,top2]=mink(I,2);
[~,bottom2]=maxk(I,2);
X=J([top2;bottom2]); Y=I([top2;bottom2]);
imshow(BW); hold on
scatter(X,Y,'filled','MarkerFaceColor','r','SizeData',80); hold off
end

4 Comments

You will have to round to obtain integer pixel coordinates,
I=round(Y);
J=round(X);
Thank you very much!
An example of a problem I have with this code now:
sometimes the code will detect 2 corners at the top left, which are both higher than the corners detected at the top right. This causes to take 2 points at the top left instead of one at the left and one on the right.
Do you have a clever way of solving this?
I was trying something like this:
% look for more points
[~,top2]=mink(x_corners,4);
% check if they are not too close to each other (mask is my picture)
if abs(x_corners(top2(1)) - x_corners(top2(2))) < 0.2 * size(mask,1)
top2(2) = top2(3);
% ...
but that is not working well
I think uniquetol should fix that, e.g.,
V=uniquetol( pgonCorners(BW,8) ,3,'DataScale',1,'ByRows',true) ;
This works perfectly, thank you!

Sign in to comment.

More Answers (1)

If the points are always supposed to be at the same place, just define them. Then, if the image is not true there, then find the distance of all points in the blob to the defined location and select the blob point that is closest to the defined reference point.
% Define expected corner point locations.
refRow = 20;
refCol = 50; % Whatever.
% Now the blob may not exist at that location so find out what locations it exists at.
[r, c] = find(mask);
% Find distances from reference point to all points in the blob.
distances = sqrt((refRow - r) .^ 2 + (refCol - c) .^ 2);
% Find the minimum distance, which will be the distance of the ref point to
% the closest point that is actually in the blob.
[minDistance, index] = min(distances)
% Get the location of the point on the blob closest to the reference point.
actualRow = r(index)
actualCol = c(index)

5 Comments

Thank you for your answer!
I should have been a bit more clear about the point not moving. In the image processing I am doing, I will have give or take 10 000 images where the sample always changes just a couple of pixels. (This will become like 10% of the image over time.) The detected point therefore should change, but just not to another possible edge point. For example, at the top right of the image, you could imagine there are 2 possible edge points (see image below red arrows). The method should always detect the same point relative to the sample, this choice between these 2 options should never change, the exact location of the point will however change.
I realise this might be a challenging and vague task and appreciate your help so far.
Attach both images with arrows pointing to the points you want in both images.
This is for example image number 1 and image number 400
I had a similar situation and to solve it I had to manually mark about a hundred images and then use Deep Learning to predict where the points should be.
Thank you for your answer, I am very close to solving the problem with Matt J his answer.
However, I am very interested in learning things about deep learning for similar problems regarding image detection. Since you seem an expert, do you have any suggestions where to learn working with deep learning software? Do you do this in MATLAB?

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!