How to implement helping functions for counting lego blocks in image

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

 Accepted Answer

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

Thank you, I have applied Color Thresholder function, setting values for Blue pixels in image,and supplied an image to the function. I am getting the pieces of Blue Lego blocks only right now. But how do I count these blocks? I need my main function to output number of blue lego blocks present in the image.
Sir, please see line # 7 of the code. I need to code this seg_area(L) function. Its sort of reverse engineering, I am provided with the base code, and I have to code the helper functions. Please help me out in this. Just give me a direction so I can work in that way.
You need several functions, like createMaskBlue(), createMaskYellow(), createMaskRed(), etc. Then apply one, like
[maskBlue, maskedRGBImage] = createMaskBlue(rgbImage);
% Get rid of noise - blobs smaller than, say, 100 pixels:
maskBlue = bwareaopen(maskBlue, 100);
% Count number of blue blobs
[labeledImage, numBlue] = bwlabel(maskBlue);
Same for the other colors.
Make sure you visit my File Exchange for other cool demos.
My Image Segmentation Tutorial is one of the most downloaded submissions ever. It goes over the basics of finding objects in an image by thresholding, then measuring a bunch of attributes of the objects (area, intensity, etc.).
Okay I will take it. Thank You. I have one more short question here. What if I want to extract blue blocks of specific size, for example in above image If I want to extract the small blue bricks and not the bigger ones?
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.
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.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!