How to implement helping functions for counting lego blocks in image

조회 수: 19 (최근 30일)
Sorry in advance for making you read for long. I have the main function for counting Yellow,Blue and Green lego blocks in images. I need help in making the helper functions from scratch. The functions are ColorGreen(), ColorYellow(),seg_area() which are called within the code. There are other ways to implement the same task, but here I need to make this specific code work that I have provided. Please go through the code for better understanding.
Thank you in anticipation
Image
Code:
function [numA,numB] = count_lego(I)
I = im2double(I);
% Ig = rgb2gray(I);
L=colorGreen(I);
% select green bricks % select bricks by bounder shape
m1 = zeros(size(L));
[L3,num3,aa3,~,~,da3,ec3,~] = seg_area(L);
for j = 1:num3
idx = find(L3==j);
if ec3(j)>0.81&&ec3(j)<0.95&&da3(j)<0.21&&aa3(j)>3000
m1(idx)=1;
end
end
m_a = m1;
[~,numA] = bwlabel(m1,8)
disp(numA)
% %% Counting Yellow B %
LL=colorYellow(I);% select Yellow Bricks
m4 = zeros(size(LL));m3 = m4;
m4(1,:)=1; m4(end,:)=1;m4(:,1)=1;m4(:,end)=1;
L3,num3,aa3,~,~,da3,ec3,~] = seg_area(LL);
for j = 1:num3
idx = find(L3==j);
m5 = zeros(size(LL));
m5(idx) = 1;
m2 = m4&m5;
idy = find(m2>0);
if length(idy)>50||aa3(j)<8000
continue
end
if (ec3(j)<0.8&&da3(j)<0.2&&aa3(j)>10000&&aa3(j)<35000)||(ec3(j)<0.9&&da3(j)<0.25&&aa3(j)<15000)||(ec3(j)<0.5&&da3(j)<0.02
m3(idx)=1;
end
end
m_b = m3;
[~,numB] = bwlabel(m3,8);
disp(numB) m_AB = m_b-m_a;
figure,imagesc(m_AB) end

채택된 답변

Image Analyst
Image Analyst 2020년 12월 4일
The easiest thing to do is to just use the Color Thresholder app on the apps tab of the tool ribbon. For each color, set your thresholds and export a function for that color. Then just call the various functions (one for each color).
  댓글 수: 8
Image Analyst
Image Analyst 2020년 12월 4일
There is a function you can use to extract out only the areas you want before calling regionprops(). it's called bwareafilt():
mask = bwareafilt(mask, [minArea, maxArea]);
props = regionprops(mask, 'All');
You can use 0 or 1 or inf (infinity) or any other numbers you want in there to define the size range you want.
Adnan Khan
Adnan Khan 2020년 12월 4일
Yes I tried that before, but the problem with this is that it calculates the number of color pixels as area i guess, it doesn't take shape into account. Let me elaborate my question,
I have set the color threesholder for red color as below image, which separates red bricks, I am interested in separating the smallest 2x2 brick.
After that, I used the following area values for the same image, which calcultes the smaller brick pretty well
maskRed = bwareafilt(maskRed, [12000 20000]);
output: Please scroll right side in image
Now when I try it for other image with same area parameters for example for the following;
It gives me the following output:
Please pay close attention, here it has considered the two bricks as one because they are not apart, On other hand it has ignored the smaller brick which is placed adjacent to the two big bricks, since area of those all three bricks exceed the limit. I hope you get my point.
Is there any solution to it? I want to extract these small bricks separately.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by