How to implement helping functions for counting lego blocks in image
3 ビュー (過去 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
0 件のコメント
採用された回答
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
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.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Image Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



