Find location of cross in image.
3 次查看(过去 30 天)
显示 更早的评论
I have an image that contains a cross in it. It also also has circles, squares and other features.
How can I detect the location of the cross to sub pixel accuracy?
Matrox MIL has a Geometric Model Finder which works very well. Does Matlab have anything similar?
回答(2 个)
Rajani Mishra
2020-1-8
I have tried below code on an image containing a cross in it, it detects the center location of the cross.
Please refer below for the code:
- Convert image to grayscale image and then binarization
img = imread('test.jpg');
I = rgb2gray(img);
BW = imbinarize(I,0.25);
2. As the result may have missing parts dilate the image to join the parts
se = strel('line',11,90);
BW2 = imdilate(BW,se);
3. Use morphological ‘shrink’ operation to shrink image to a point
BW3 = bwmorph(BW2,'shrink',Inf);
4. Find the position of the point
[row,col] = find(BW3);
Hope this helps.
0 个评论
Daniel
2025-3-25
Hello Hans,
this might be an answer to a long forgotten question. But there may be others out there who are looking for a solution.
Depending on how your image looks this may be complicated to accomplish. However, if you managed to narrow it down to something similar like the 'blobs.png' example, a similar approach like mine might work.
It works because the cross has a big bounding box compared to its filled connected area.
load blobs example image
BW = imread('blobs.png');
Use regionprops to evaluate each individual region using 'FilledArea'
imgprops = regionprops('table',BW,'FilledArea','BoundingBox','Centroid');
The cross is assumed to have a minimum in the filled area to bounding box size ratio (for this image). A diagonal line would have an even lower ratio and the cross will not be the minimum.
[~,k] = min(imgprops.FilledArea ./ prod(imgprops.BoundingBox(:,[3 4]),2));
Plot the result:
figure()
imshow(BW)
hold on
scatter(imgprops.Centroid(k,1),imgprops.Centroid(k,2),'r*');
2 个评论
DGM
2025-3-27
编辑:DGM
2025-3-27
Even if we assume all the blobs are skeletal, the ratio of the filled area to the bb area is scale-dependent. If we discard objects with an Euler characteristic ~=1, then we don't strictly need the filled area for the added discrimination. We can just use 'extent', which is (unfilled blob area)/(bb area). Either way, the ratio is scale-dependent.
inpict = imread('crosses.png');
% some properties
S = regionprops(inpict,'extent','centroid');
% show it
imshow(inpict)
os = [10 10];
for k = 1:numel(S)
text(S(k).Centroid(1)+os(1),S(k).Centroid(2)+os(2), ...
num2str(S(k).Extent,2),'color','y')
end

This means that crosses, L-shapes, T-shapes, and straight lines can't be distinguished based on this ratio alone. If we're dealing strictly with skeletal shapes, perhaps we could count endpoints and branchpoints, but that can get tricky, as things don't always skeletonize cleanly.
If we're not dealing with skeletal shapes, then the area ratio is dependent on the thickness of the cross.
Daniel
2025-4-15
Hello @DGM
Yes, you are absolutely right. Thank you for your answer!
But as there was no image uploaded from Hans, we don't know how accurate and robust the algorithm needs to be. I actually ended up using the "extent" property for my use case as well.
Best regards.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Image Processing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!