Coordinates of corners of quadrilateral in binary mask

I'm doing a project on computer vision in which I have to compute some homographies and perform image stitching. I also have to provide the coordinates of the corners of each image in the final panorama.
Essentially, what I need is to determine the [x,y] coordinates (or i,j indices) of the four corners of a quadrilateral in a binary mask (example in the image below).
I coded a pretty computationally inefficient function to do it. But it misses one of the corners whenever the quadrilateral is a trapezoid (which is most of the time). I tried fixing it but haven't managed to get it quite right. I have tried to detect edges and lines using the hough algorithm and then getting the intersections of those lines but the results aren't precise and looks like a complicated way of solving a somewhat simple problem. Any matlab functions or implementation ideas for this task?

 Accepted Answer

The Computer Vision Toolbox has some ready-made corner detection functions, e.g.,
You could also try this improvised algorithm:
N=360;
theta=linspace(0,360,N);
[I,J]=find(Image);
IJ=[I,J];
c=nan(size(theta));
for i=1:N
[~,c(i)]=max(IJ*[cosd(theta(i));sind(theta(i))]);
end
H=histcounts(c,1:numel(I)+1);
[~,k] = maxk(H,4);
corners=IJ(k,:)

5 Comments

Hi, Matt. Thank you for your answer. I tried all your suggestions but none gave particularly good results. None of the matlab functions work super well... Whenever the quadrilateral is somewhat regular (opposite sides are parallel) it works pretty nicely. But with these non-regular trapezoids, it detects almost the entirety of the edge points as corners (Maybe I could use this information in some way? Maybe by fitting lines with these points and getting the intersections of those). Your own improvised function is able to detect only four corners, but is strangely misfitted. It looks like all detect points are a translation away from the correct corners (the same translation for all corner/point pairs, that is).
Your own improvised function is able to detect only four corners, but is strangely misfitted.
Note that my code routine returns the corners in matrix (i,j) coordinates. If you assumed them to be in intrinsic or world coordinates, that would explain the displacements you see in your plots. Here is what I get with your example above.
Thank you for noticing it, I was in fact displaying the points the wrong way it. It works perfectly! I already accepted the answer, thank you very much for your help
Dear Matt,
could you please explain what your improvised algorithm exactly does? Especially the formula in the for - loop, what´s the mathematical background?

Sign in to comment.

More Answers (0)

Categories

Find more on Read, Write, and Modify Image 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!