How to count the amount of small squares in this picture?

5 ビュー (過去 30 日間)
xie
xie 2024 年 12 月 15 日
編集済み: Catalytic 2024 年 12 月 17 日
  1 件のコメント
xie
xie 2024 年 12 月 15 日
移動済み: KALYAN ACHARJYA 2024 年 12 月 15 日
I mean I want to count units quantity as below:

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

採用された回答

DGM
DGM 2024 年 12 月 17 日
The given answers have problems with accuracy. One way to help with discrimination is to do some sort of template matching.
% the image
inpict = imread('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1821039/image.png');
inpict = im2gray(inpict);
% the sub-image template
template = imcrop(inpict,[22.51 22.51 14.98 13.98]);
% get a correlation map describing
% where copies of the template are
C = normxcorr2(template,inpict);
mask = C > 0.5;
mask = bwareaopen(mask,9); % get rid of any specks
imshow(mask)
% count the peaks
N0 = bwconncomp(mask).NumObjects
N0 = 2925
Is that number plausible? The image isn't that large. We can just manually verify that there are 45x65 = 2925 copies of the template object. We can also do that programmatically:
% number of rows of objects
nv = smooth(max(C,[],2)) > 0.5;
nv = bwconncomp(nv).NumObjects
nv = 45
% number of columns of objects
nh = smooth(max(C,[],1)) > 0.5;
nh = bwconncomp(nh).NumObjects
nh = 65
% total
N = nv*nh
N = 2925
As is, the other examples produce an overestimate due to the occasional blob between objects, or content along the image boundary. When I tested them, the estimates were higher by about 100 or so.
  1 件のコメント
xie
xie 2024 年 12 月 17 日
Great!
This way get rid of the noise. I can get true result!

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

その他の回答 (3 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2024 年 12 月 15 日
Approximate Way:
image_input=imread('image file name');
image_bw=imbinarize(rgb2gray(image_input));
cc=bwconncomp(image_bw);
number=cc.NumObjects;
However, you can obtain the exact value by incorporating additional morphological and logical statements.

Image Analyst
Image Analyst 2024 年 12 月 15 日
It's a generic, general purpose demo of how to threshold an image to find blobs, and then measure things about the blobs, and extract certain blobs based on their areas or diameters.

Catalytic
Catalytic 2024 年 12 月 17 日
編集済み: Catalytic 2024 年 12 月 17 日
BW=imbinarize(im2gray(imread('image.png')));
BW([1,end],:)=0;
BW(:,[1,end])=0;
BW=imopen(BW,ones(2));
nv=numAlong(BW,1)
nv =
45
nh=numAlong(BW,2)
nh =
65
total=nv*nh
total =
2925
function out=numAlong(BW,dim)
s=reshape( sum(BW,3-dim),[],1);
m=bwareaopen(s>max(s)/3,8);
out=bwconncomp(m).NumObjects;
end

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by