How to find coordinates of corners in Matlab?

1.2k views Asked by At

How can I find the coordinates of the four corners in this "square" in Matlab? I tried with corner, regionprops('Extrema') and detectMinEigenFeatures with no luck. The problem is the blue square is not a perfect square, so I need some kind of sharpening edge technique or a more "intelligent" find-corner-algorithm to find them.

target-image

2

There are 2 answers

1
obchardon On BEST ANSWER

Because I'm a nice guy, I've translated the Tasos's explainations into code, check the comments :

%Open your image
I = imread('square.png');
I = im2bw(I(:,:,3));

%Compute the centroid
centroid = round(size(I)/2);

%Find the pixels that create the square
[x,y] = find(I);

%Change the origin
X = [y,x]-centroid;

%Sort the data
X = sortrows(X,[1 2]);

%Cartesian to polar coordinates
[theta,rho] = cart2pol(X(:,1),X(:,2));

%sort the polar coordinate according to the angle.
[POL,index] = sortrows([theta,rho],1);

%Smoothing, using a convolution
len = 15 %the smoothing factor
POL(:,2) = conv(POL(:,2),ones(len ,1),'same')./conv(ones(length(POL(:,2)),1),ones(len ,1),'same');

%Find the peaks
pfind = POL(:,2);
pfind(pfind<mean(pfind)) = 0;
[~,pos] = findpeaks(pfind);

%Change (again) the origin
X = X+centroid;


%Plot the result
plot(POL(:,1),POL(:,2))
hold on
plot(POL(pos,1),POL(pos,2),'ro')
figure
imshow(I)
hold on
plot(X(index(pos),1),X(index(pos),2),'ro')

RESULTS:

The distance (y-axis) vs angle (x-axis) plot: enter image description here

The final detection:

enter image description here

1
Tasos Papastylianou On

Assuming you're doing this in "image" mode, rather than analytically, calculate the distance from the centroid to each pixel on your shape, and then plot this as a function of the angle (i.e. as if you started from the centroid and a horizontal line and then you rotated that line and note the length of that 'radius' at each angle). Your graph should be a curve with 4 local peaks which you can then isolate and trace back to their coordinates.

Or, if the assumption holds that the corners are relatively guaranteed to be close to the image corners, restrict yourself to finding a shape corner in the respective image quadrant by performing the above procedure from the image corner and finding the minimum, and then repeat this 4 times (i.e. for each corner)