Search code examples
matlabedge-detection

How to find coordinates of corners in Matlab?


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


Solution

  • 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