How to automatically obtain shape coordinates

4 ビュー (過去 30 日間)
Jacob Ebilane
Jacob Ebilane 2022 年 1 月 24 日
回答済み: Image Analyst 2022 年 1 月 25 日
I have an image (attached) that I want to crop. But to crop them I always need to manually take the center "coordinates" (index) of the center of each black box in the red circle. I need to automate it but I don't know where to start.

回答 (3 件)

Matt J
Matt J 2022 年 1 月 24 日
編集済み: Matt J 2022 年 1 月 24 日
Perhaps as follows
load Image
B=medfilt2(A,[5,5])<60;
B=bwareafilt(B,5) & ~bwareafilt(B,1);
T=regionprops('table',B,'Centroid'); %square centroids
LT=min(T.Centroid); %%left top corner
SZ=max(T.Centroid)-LT+1; %size fo box
A=imcrop(A,[LT,SZ]); %ignore projective warping
imshow(A,[])
  2 件のコメント
Jacob Ebilane
Jacob Ebilane 2022 年 1 月 24 日
Kind of close to what I need, but I need 4 points because I have to straighten it using a program I found. I could use LT to be set as my first point, but I'd need the location of the other 3 boxes.
Matt J
Matt J 2022 年 1 月 25 日
You have the 4 points in T.Centroid.

サインインしてコメントする。


yanqi liu
yanqi liu 2022 年 1 月 25 日
clc; clear all; close all;
img = imread('https://ww2.mathworks.cn/matlabcentral/answers/uploaded_files/871715/uno.png');
if ndims(img) == 3
img = rgb2gray(img);
end
bw = imbinarize(img,'adaptive','ForegroundPolarity','dark','Sensitivity',0.4);
bw2 = ~bw;
bw2 = imopen(bw2, strel('square', 5));
bw3 = imclose(bw2, strel('line', size(bw2,1), 90));
bw4 = imclose(bw2, strel('line', size(bw2,2), 0));
% find left and right
[L,num] = bwlabel(bw3);
stats = regionprops(L);
rects = cat(1, stats.BoundingBox);
ind1 = find(rects(:,4)>size(bw2,1)*0.8);
[~,ind2] = min(rects(ind1,1));
[~,ind3] = max(rects(ind1,1));
bw3 = L==ind1(ind2) | L == ind1(ind3);
% find top and bottom
[L,num] = bwlabel(bw4);
stats = regionprops(L);
rects = cat(1, stats.BoundingBox);
ind1 = find(rects(:,3)>size(bw2,2)*0.8);
[~,ind2] = min(rects(ind1,2));
[~,ind3] = max(rects(ind1,2));
bw4 = L==ind1(ind2) | L == ind1(ind3);
% make square
bw5 = logical(bw3 + bw4);
bw5 = imfill(bw5, 'holes');
[r,c] = find(bw5);
rect = [min(c) min(r) max(c)-min(c) max(r)-min(r)];
% get 4 square
figure; imshow(img);
hold on; rectangle('position', rect, 'EdgeColor', 'g', 'LineWidth', 2)

Image Analyst
Image Analyst 2022 年 1 月 25 日
Here is yet another way:
grayImage = imread('uno.png');
subplot(2, 2, 1);
imshow(grayImage, []);
title('Original Image.')
if ndims(grayImage) == 3
grayImage = rgb2gray(grayImage);
end
topHatImage = imbothat(grayImage, true(51));
subplot(2, 2, 2);
imshow(topHatImage, [])
title('Top Hat Filtered Image.')
impixelinfo;
mask = topHatImage > 60; %~imbinarize(grayImage,'adaptive','ForegroundPolarity','dark','Sensitivity',0.4);
mask = imfill(mask, 'holes');
props = regionprops(mask, 'Area')
allAreas = sort([props.Area])
mask = bwareafilt(mask,[400, 7000]);
mask = bwconvhull(mask);
subplot(2, 2, 3);
imshow(mask, []);
title('Mask.')
props = regionprops(mask, 'BoundingBox')
croppedImage = imcrop(grayImage, props.BoundingBox);
subplot(2, 2, 4);
imshow(croppedImage, []);
title('Cropped Image.')
It could be made faster if you started with a good image, like one from a scanner instead of a poorly lit paper and a mobile phone camera.

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by