How do I find the corner points of an mask

조회 수: 20 (최근 30일)
kunal
kunal 2025년 4월 15일
답변: Image Analyst 2025년 5월 9일
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

채택된 답변

Mathieu NOE
Mathieu NOE 2025년 4월 15일
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
Mathieu NOE
Mathieu NOE 2025년 4월 16일
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
kunal
kunal 2025년 4월 23일
Oh thankyou, I will try it and see if it works for my use cases.

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Matt J
Matt J 2025년 4월 17일
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
Image Analyst 2025년 4월 17일
Depending on the shapes you're dealing with, convhull may work for you.

Image Analyst
Image Analyst 2025년 5월 9일

카테고리

Help CenterFile Exchange에서 Computer Vision with Simulink에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by