How to automatically obtain shape coordinates

7 vues (au cours des 30 derniers jours)
Jacob Ebilane
Jacob Ebilane le 24 Jan 2022
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.

Réponses (3)

Matt J
Matt J le 24 Jan 2022
Modifié(e) : Matt J le 24 Jan 2022
Perhaps as follows
load Image
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
  2 commentaires
Jacob Ebilane
Jacob Ebilane le 24 Jan 2022
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 le 25 Jan 2022
You have the 4 points in T.Centroid.

Connectez-vous pour commenter.

yanqi liu
yanqi liu le 25 Jan 2022
clc; clear all; close all;
img = imread('');
if ndims(img) == 3
img = rgb2gray(img);
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 le 25 Jan 2022
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);
topHatImage = imbothat(grayImage, true(51));
subplot(2, 2, 2);
imshow(topHatImage, [])
title('Top Hat Filtered Image.')
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, []);
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.


En savoir plus sur Image Processing Toolbox dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by