How do I find the corner points of an mask
31 views (last 30 days)
Show older comments
kunal
on 15 Apr 2025 at 11:49
Answered: Image Analyst
on 9 May 2025 at 13:25
I have various mask and i want to find the exact four courner coordinates, but when some mask objects with an rotation and little odd shape comes i find it difficult to find the corner points.
I have attached the mask and pointed the points i want to find

0 Comments
Accepted Answer
Mathieu NOE
on 15 Apr 2025 at 14:27
hello
let's try with detectHarrisFeatures : not really super efficient I admit , I will propose alternatives ...
im = imread('image.png'); % Load your binary mask image
% Convert the image to grayscale
gray_img = rgb2gray(im);
% Apply the Harris corner detector
corners = detectHarrisFeatures(gray_img,'MinQuality',0.5);
% Display the image with the detected corners
imshow(im); hold on;
plot(corners.selectStrongest(10));
2 Comments
Mathieu NOE
on 16 Apr 2025 at 10:23
hello again
this is a (better) alternative based on boundary and its curvature computation
you may need to smooth the boundary curve , I used this for the job : smoothn - File Exchange - MATLAB Central
hope it helps
my result so far :

code :
A = imread('image.png');
% a bit of gym to convert to grayscale , that we will "binarize" with
% logical operation
A = double(A);
% rgb2gray converts RGB values to grayscale values by forming a weighted sum of the R, G, and B components:
% 0.2989 * R + 0.5870 * G + 0.1140 * B
A = 0.299 * A(:,:,1) + 0.587 * A(:,:,2) + 0.114 * A(:,:,3);
A = flipud(A); % to have image displayed with correct y direction
[y,x] = find(A>0.5*256); % find whte dots coordinates : threshold is set at 50% of 256
k = boundary(x,y,1); % find boundary
x_selec = x(k);
y_selec = y(k);
% smooth a bit the contour
z = smoothn({x_selec,y_selec},20);
x_selec = z{1};
y_selec = z{2};
xx_centroid = mean(x_selec);
yy_centroid = mean(y_selec);
% Plot curvature.
curvature = my_curvature(x_selec,y_selec);
[pks,locs] = findpeaks(curvature,'MinPeakHeight',max(curvature)/10);
xc = x_selec(locs);
yc = y_selec(locs);
% select the 4 points according to their x distance vs centroid
% dist = (xc-xx_centroid).^2+(yc-yy_centroid).^2;
dist = (xc-xx_centroid).^2;
[dist,ind] = sort(dist,'descend');
xc = xc(ind(1:4));
yc = yc(ind(1:4));
figure;
imagesc(A);
colormap('gray')
hold on
set(gca,'YDir','normal');
plot(x_selec,y_selec,'g');
plot(xc,yc,'dr');
grid on; axis equal
xlabel x
ylabel y
hold off
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function curvature = my_curvature(x,y)
%=====================================================
% Now run along the (x, y) soft star curve
% and find the radius of curvature at each location.
numberOfPoints = length(x);
curvature = zeros(1, numberOfPoints);
for t = 1 : numberOfPoints
if t == 1
index1 = numberOfPoints;
index2 = t;
index3 = t + 1;
elseif t >= numberOfPoints
index1 = t-1;
index2 = t;
index3 = 1;
else
index1 = t-1;
index2 = t;
index3 = t + 1;
end
% Get the 3 points.
x1 = x(index1);
y1 = y(index1);
x2 = x(index2);
y2 = y(index2);
x3 = x(index3);
y3 = y(index3);
% Now call Roger's formula:
% http://www.mathworks.com/matlabcentral/answers/57194#answer_69185
curvature(t) = 2*((x2-x1).*(y3-y1)-(x3-x1).*(y2-y1)) ./ ...
sqrt(((x2-x1).^2+(y2-y1).^2)*((x3-x1).^2+(y3-y1).^2)*((x3-x2).^2+(y3-y2).^2));
end
end
More Answers (3)
Matt J
on 17 Apr 2025 at 3:37
You could download pgonCorners,
BW = imbinarize( im2gray(imread('image.png')) );
V=pgonCorners(BW,4);
imshow(BW,[]); hold on
plot(V(:,2),V(:,1),'r.',MarkerSize=30); hold off
Image Analyst
on 9 May 2025 at 13:25
See my Answer in your duplicate question: https://www.mathworks.com/matlabcentral/answers/2177033-help-me-get-the-points-of-the-mask#answer_1564977
0 Comments
See Also
Categories
Find more on Computer Vision with Simulink in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!