MATLAB Answers

fiq
0

Counting the number of object based on the object colour

Asked by fiq
on 25 Dec 2012
hi all, i want to ask for help, i have a picture as had been upload in the link provided below. from the picture, there are 3 lego blocks with different colour. i need to count the total object inside the picture with the amount of volume per each colour of lego block. i am new to Matlab and still learning about this image processing. hopefully can get feedback from you all. thank you link to the image: http://i1343.photobucket.com/albums/o794/sparktzm/image1_zpse86566bd.jpg

2 Answers

Answer by Image Analyst
on 25 Dec 2012
Edited by Image Analyst
on 25 Dec 2012
 Accepted Answer

There are dark shadows in between the blocks where the color is not well defined. Look at the code below where I extract each color and plot its histogram. You can see overlap in the histograms - it's not nicely bimodal where you need it to be. You might have to do color segmentation in another color space. See my File Exchange for examples: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Read in a standard MATLAB color demo image.
folder = 'C:\Users\fiq\Documents\Temporary';
baseFileName = 'image1_zpse86566bd.jpg';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 4, 1);
imshow(rgbImage);
title('Original Color Image', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Display them
subplot(2, 4, 2);
imshow(redChannel);
title('RedChannel Image', 'FontSize', fontSize);
subplot(2, 4, 3);
imshow(greenChannel);
title('Green Channel Image', 'FontSize', fontSize);
subplot(2, 4, 4);
imshow(blueChannel);
title('Blue Channel Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(redChannel);
subplot(2, 4, 6);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(greenChannel);
subplot(2, 4, 7);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Let's compute and display the histogram.
[pixelCount grayLevels] = imhist(blueChannel);
subplot(2, 4, 8);
bar(pixelCount);
grid on;
title('Histogram of original image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
[EDIT]
% Get the masks
redMask = (redChannel > 190) & (greenChannel < 150) & (blueChannel < 90);
redMask = imfill(redMask, 'holes'); % Fill in holes.
greenMask = (redChannel < 167) & (greenChannel > 160) & (blueChannel < 150);
greenMask = imfill(greenMask, 'holes'); % Fill in holes.
blueMask = (redChannel < 120) & (greenChannel > 60) & (greenChannel < 130) & (blueChannel> 100);
blueMask = imfill(blueMask, 'holes'); % Fill in holes.
figure;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Display the images.
subplot(2, 3, 1);
imshow(redMask);
title('Red Mask', 'FontSize', fontSize);
subplot(2, 3, 2);
imshow(greenMask);
title('Green Mask', 'FontSize', fontSize);
subplot(2, 3, 3);
imshow(blueMask);
title('Blue Mask', 'FontSize', fontSize);
% Mask out the single color from the rest of the image.
% Mask the image.
maskedRgbImage = bsxfun(@times, rgbImage, cast(redMask,class(rgbImage)));
subplot(2, 3, 4);
imshow(maskedRgbImage);
maskedRgbImage = bsxfun(@times, rgbImage, cast(greenMask,class(rgbImage)));
subplot(2, 3, 5);
imshow(maskedRgbImage);
maskedRgbImage = bsxfun(@times, rgbImage, cast(blueMask,class(rgbImage)));
subplot(2, 3, 6);
imshow(maskedRgbImage);

  29 Comments

sir i try to write the code like this but the answer still the same like previous result
occurrenceOfColors = [0 0 0];
if closestRefColor >= 1
occurrenceOfColors(closestRefColor) = occurrenceOfColors (closestRefColor) + 1;
elseif closestRefColor >= 2
occurrenceOfColors(closestRefColor) = occurrenceOfColors(closestRefColor) + 1;
elseif closestRefColor >= 3
occurrenceOfColors(closestRefColor) = occurrenceOfColors (closestRefColor) + 1;
end
Yes, but that's not where you should put it. Like I said, put it BEFORE you start accumulating anything into the array. You put it right there where you do the accumulation, blowing away any prior results that you have saved. If this is in a loop, then it needs to be before the loop starts, otherwise each iteration of the loop will look at the three locations, and, because you zeroed out the array, the most any element of the array could be would be 3 (if all 3 blocks were the same color), even on the second, third, fourth iteration.
Secondly, why did you change this:
if closestRefColor == 1
to this:
if closestRefColor >= 1
????? Now, 1, 2, and 3 will all go into the first if block instead of going into the if block where they should go. Why did you do that? Make that change?
I'm beginning to think that it would just be easier to do it for you myself, but I don't know how much of it I'm allowed to complete for you. Is this your college project that you're supposed to turn in?
sorry sir for my bad knowledge on image processing. i in my process of learning it...by the way i really appreciated the help that you have gave. thank you

Sign in to comment.


Answer by Sadid
on 25 Dec 2012

for discriminant between each color you can use RGB color to emphasize your desired color. I mean you can perform the analysis 3 times each on one of the R,G or B color plane.
for counting the objects your can use simple to complicated methods based on edge detection or morphological processing. Matlab has its predefined algorithm for object detection. For example you can check "Cell Counting" demo of machine vision toolbox or "Count Objects in an image"

  2 Comments

Sadid thank for your reply..i had a problem in thresholding the image. somehow i get a lot of noise when thresholding the image. this cause me unable to get the desired binary image of the edge of the box. could anybody teach me how to perform that..i had try many method but all seems not give me the correct answer. the last threshold image i get shown in this picture http://i1343.photobucket.com/albums/o794/sparktzm/image1binary_zps09c3095d.jpg
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
Then threshold each with different thresholds. Then call regionprops() for each one.

Sign in to comment.