How to implement helping functions for counting lego blocks in image
19 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Image Analyst
on 4 Dec 2020
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 Comments
Image Analyst
on 4 Dec 2020
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.
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!