Counting the number of object based on the object colour

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

 Respuesta aceptada

Image Analyst
Image Analyst el 25 de Dic. de 2012
Editada: Image Analyst el 25 de Dic. de 2012
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 comentarios

fiq
fiq el 25 de Dic. de 2012
Editada: fiq el 25 de Dic. de 2012
thank for your reply..what is the function of performing this step and can you guild me on how to perform this function...i am new user to matlab.
Image Analyst
Image Analyst el 25 de Dic. de 2012
Editada: Image Analyst el 25 de Dic. de 2012
It just extracts the red, green, and blue channels and shows them to you along with the histograms to make it easy for you to see if and where you can set a threshold. Not sure how much I'm expected to do for you, but I added some code to the original code to mask out each colored block. Feel free to adapt as necessary.
fiq
fiq el 25 de Dic. de 2012
Editada: fiq el 26 de Dic. de 2012
sir, so far i come out with this code but seems it is not right cause the system not give right value for the box counting. it detect 2 instead of 3 box. One more things, how do i make it detect the color of the box and give the actual value for each box. if you mind look at my code :
% code
rgbImage = imread ('image13.jpg');
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
imbinaryR = im2bw(redChannel, 0.7); %0.55
imwrite(imbinaryR, 'image1(binaryR).jpg');
imbinaryG = im2bw(greenChannel, 0.6); %0.6
imwrite(imbinaryG, 'image1(binaryG).jpg');
imbinaryB = im2bw(blueChannel , 0.6); %0.45
imwrite(imbinaryB, 'image1(binaryB).jpg');
imbinary = imcomplement (imbinaryR&imbinaryG&imbinaryB);
imwrite(imbinary, 'image1(binary).jpg');
STATSR = regionprops(imbinaryR , 'centroid')
STATSG = regionprops(imbinaryG , 'centroid')
STATSB = regionprops(imbinaryB , 'centroid')
imbinary1 =~imbinary; %complement the image (change black to white)
se = strel('square', 10); %40 20
imClean = imopen (imbinary1,se);
imClean = imfill(imClean,'holes');
%imClean = imclearborder (imClean);
imwrite(imClean, 'image1(clean).jpg');
[labels,numlabels] = bwlabel (imClean);
disp({'num of object:' num2str(numlabels)});
%figure ; imshow (imbinary1);
B = bwboundaries (imbinary1); %find boundaries of object
figure ; imshow (imbinary1);
text (10,10,strcat('\color{green}vision System : Objects Found:',num2str(length(B))))
hold on
for k = 1: length (B) %draw boundaries detect by bwboundaries
boundary = B {k};
plot(boundary (:,2),boundary(:,1),'g','LineWidth',0.2)
end
hold off
imwrite(imbinary1, 'image1(count).jpg');
Image Analyst
Image Analyst el 25 de Dic. de 2012
Editada: Image Analyst el 25 de Dic. de 2012
Your code doesn't run. I started fixing it but after about 8 fixes I just gave up. Most errors seem to be because you don't know how to format your code properly so everything is just run together on one line.
(By the way did you see my edit)? Like I said initially, RGB space is not good. If you don't know in advance how many colors there will be, try converting to HSV space using rgb2hsv, and getting a histogram of the H channel. The number of humps in the h channel histogram will be the number of colors. Here, run this code. Note that the red has both high and low hue so you need to take that into account.
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]);
% Compute the HSV image
hsv = rgb2hsv(rgbImage);
h = hsv(:, :, 1);
s = hsv(:, :, 2);
v = hsv(:, :, 3);
% Display them
subplot(2, 4, 2);
imshow(h);
title('Hue Channel Image', 'FontSize', fontSize);
subplot(2, 4, 3);
imshow(s);
title('Saturation Channel Image', 'FontSize', fontSize);
subplot(2, 4, 4);
imshow(v);
title('Value Channel Image', 'FontSize', fontSize);
% Let's compute and display the histogram.
[pixelCount grayLevels] = hist(h(:), 500);
subplot(2, 4, 6);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of hue image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Let's compute and display the histogram.
[pixelCount grayLevels] = hist(s(:), 500);
subplot(2, 4, 7);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of saturation image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
% Let's compute and display the histogram.
[pixelCount grayLevels] = hist(v(:), 500);
subplot(2, 4, 8);
bar(grayLevels, pixelCount);
grid on;
title('Histogram of value image', 'FontSize', fontSize);
xlim([0 grayLevels(end)]); % Scale x axis manually.
fiq
fiq el 26 de Dic. de 2012
sorry sir due to my lack of knowledge on this things...i am still new and in my learning process of image processing in matlab. i will try to look at this things first. really appreciated what you had help me so far. thank you
fiq
fiq el 26 de Dic. de 2012
Editada: fiq el 26 de Dic. de 2012
Sir one more question, is it possible for matlab to automatically calculate the value for RGB mask. I am developing an automation system for my project which require a webcam to acquire image and the system will process it to identify the number of color block inside the image. I try to play with several different image containing different set of color block but seems all give different result. i attach one of the image acquire by the camera with different block color http://i1343.photobucket.com/albums/o794/sparktzm/image1_zps0d082eb1.jpg
It is difficult to impossible to automatically calculate the values for the RGB mask. The masks depend upon humans perceive color, which is not completely understood. It is known, though, that color perception is not just based upon absolute measurable properties of a given location, and instead is dependent partly on surrounding areas and patterns in that area. Color perception is also dependent upon which portion of the eye is being used to look at the particular portion of the scene: the various color perception cones are not arranged evenly around the eye.
You should also look in the File Exchange for John D'Errico's Fuzzy Color Detection, which tries to take into account the fact that what humans name as being particular colors often correspond to irregular shapes in theoretical color spaces.
fiq
fiq el 26 de Dic. de 2012
thanks for your reply...i try to find the example which you mention but unfortunately i cannot find it. mind share the link to the example? sir can suggest what is the best method when dealing with multiple image to make the analysis?
Image Analyst
Image Analyst el 26 de Dic. de 2012
Editada: Image Analyst el 26 de Dic. de 2012
Since you seem to have known colors - they will be just the lego blocks - you can just dom very simple color classification like finding the Euclidean distance in RGB space to the centroid of each reference Lego block and take the closest one. Alternatively you can calculate delta E images from your test image to each of the known colors and take the closest one (smallest delta E).
I do not know about modern Lego blocks, but the Lego blocks I used to own had a problem of fading with age (possibly due to exposure to light.) Still, I never had a Lego block change color to (e.g.) Gold, so possibly this factor could be reasonably accounted for.
Those blocks in the picture don't look like genuine Legos to me - more like some cheap knock-off. Regardless, if you just pick 4 colors almost anywhere in the nearby region, it should work. Gold, goldenrod, sunflower, lemon, Singapore sunset saffron or whatever color you may have is going to be closer to whatever yellow you pick than to whatever red, green, or blue you picked.
You can also put in a threshold distance if you want to call things "unknown". For example if you present white - it might be sort of equidistant from your 4 reference colors, or maybe slightly closer to one, but you can say if the distance is greater than, say 100 then it's not close enough to any of them and you call it "unknown", so you'd have 4 valid classes (red, green, blue, yellow), and one "unknown" class.
Looks like modern Lego uses different hues than when I was younger.
By the way, Legos are very useful as positioning devices for imaging. Say you had some object that you wanted to take a before and after picture of. Like, say you wanted to paint, sandblast, etch, clean, carve, stain, or somehow otherwise change the appearance of an object. And you wanted to take a photo of it in exactly the same spot before and after your treatment. You could glue or screw it onto a Lego piece, and have a base Lego piece firmly attached to your table. Snap them together (the Lego on the base of your object onto the Lego glued to the table), take your photo, then make your treatment of the object, and snap it back on to the base Lego piece on the table, and take your "after" snapshot. It will be perfectly repositioned because the tolerances on Lego's are pretty precise. Yes, we have actually done this for a real world situation.
fiq
fiq el 27 de Dic. de 2012
Editada: fiq el 27 de Dic. de 2012
sir i try to look at your example DeltaE in file exchange...is it possible if we automatically set the region of interest, ROI (as we know the position of each of the lego blocks) when making the outline region over rgb image apart of manually draw the ROI?
If your blocks are known to be in the same position every time, then yes, it's absolutely possible, and in fact the easiest way to go. Just look at some rectangular chunk in the middle of the block. A little in from the edge to account for possible different position.
I don't think the blocks are in the same position every time, since there are not necessarily the same number of blocks.
Threshold, label, regionprops -- though with the poor illumination, some of the blocks might come out merged in the simplest form of this.
Image Analyst
Image Analyst el 27 de Dic. de 2012
Editada: Image Analyst el 27 de Dic. de 2012
You don't need the same number of blocks. You can have a missing block. The algorithm would just see the background in the location of the missing block and recognize that color as the background and not count a block as being present in that location. See this demo:
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, 2, 1);
imshow(rgbImage);
axis on;
hold on;
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, 2, 2);
imshow(redChannel);
hold on;
title('RedChannel Image', 'FontSize', fontSize);
subplot(2, 2, 3);
imshow(greenChannel);
hold on;
title('Green Channel Image', 'FontSize', fontSize);
subplot(2, 2, 4);
imshow(blueChannel);
hold on;
title('Blue Channel Image', 'FontSize', fontSize);
% Define out reference block colors.
referenceRed = [227 42 48]
referenceGreen = [110 208 110];
referenceBlue = [74 91 142];
% Define our regions of interest
topRow = 100;
bottomRow = 350;
leftColumns = [30 250 460]; % For left, middle, and right locations.
rightColumns = [150 390 600]; % For left, middle, and right locations.
for location = 1 : 3
% Get the mean color in the ROI for that location.
meanRed = mean2(redChannel(topRow:bottomRow, leftColumns(location):rightColumns(location)))
meanGreen = mean2(greenChannel(topRow:bottomRow, leftColumns(location):rightColumns(location)))
meanBlue = mean2(blueChannel(topRow:bottomRow, leftColumns(location):rightColumns(location)))
% Plot box over regions
boxX = [leftColumns(location), rightColumns(location), rightColumns(location), leftColumns(location), leftColumns(location)];
boxY = [topRow, topRow, bottomRow, bottomRow, topRow];
subplot(2, 2, 1);
plot(boxX, boxY, 'w-', 'LineWidth', 2);
subplot(2, 2, 2);
plot(boxX, boxY, 'r-', 'LineWidth', 2);
subplot(2, 2, 3);
plot(boxX, boxY, 'g-', 'LineWidth', 2);
subplot(2, 2, 4);
plot(boxX, boxY, 'b-', 'LineWidth', 2);
% Calculate the distance from this to the reference red color in RGB space
distanceToRefRed = sqrt((meanRed - referenceRed(1))^2 + (meanGreen - referenceRed(2))^2 + (meanBlue - referenceRed(3))^2)
% Calculate the distance from this to the reference green color in RGB space
distanceToRefGreen = sqrt((meanRed - referenceGreen(1))^2 + (meanGreen - referenceGreen(2))^2 + (meanBlue - referenceGreen(3))^2)
% Calculate the distance from this to the reference blue color in RGB space
distanceToRefBlue = sqrt((meanRed - referenceBlue(1))^2 + (meanGreen - referenceBlue(2))^2 + (meanBlue - referenceBlue(3))^2)
% Determine which color it is.
[minDistance(location) closestRefColor] = min([distanceToRefRed, distanceToRefGreen, distanceToRefBlue])
% It's an unknown color if the min color is more than 100 away.
if minDistance(location) > 50
closestRefColor = 0;
end
% Prepare message.
message = sprintf('Block %d actual colors are:\nRed = %.1f\nGreen = %.1f\nBlue = %1.f',...
meanRed, meanGreen, meanBlue);
message = sprintf('%s\n\nThe distances to the reference colors are:\nRed = %.1f away\nGreen = %.1f away\nBlue = %1.f away',...
message, distanceToRefRed, distanceToRefGreen, distanceToRefBlue);
if closestRefColor == 1
message = sprintf('%s\n\nSo block %d is Red', message, location);
elseif closestRefColor == 2
message = sprintf('%s\n\nSo block %d is Green', message,location);
elseif closestRefColor == 3
message = sprintf('%s\n\nSo block %d is Blue', message, location);
elseif closestRefColor == 0
message = sprintf('%s\n\nSo this block is undefined', message);
end
uiwait(helpdlg(message));
end
How do you store anything? You make a variable for it, like occurrenceOfColors.
occurrenceOfColors(closestRefColor) = occurrenceOfColors(closestRefColor) + 1;
fiq
fiq el 28 de Dic. de 2012
Editada: fiq el 28 de Dic. de 2012
sir i try to use this command but the problem happen if there is multiple occurance happen but matlab still detect it as 1. there are 2 occurance of red box but matlab detect it only as 1. i attach the code that i use.
if closestRefColor == 1
subplot(2, 3, 5);
plot(boxX, boxY, 'r-', 'LineWidth', 2);
redbox(closestRefColor == 1) = + 1;
else closestRefColor < 1
redbox(closestRefColor < 1) = + 0;
end
if closestRefColor == 2
subplot(2, 3, 5);
plot(boxX, boxY, 'g-', 'LineWidth', 2);
greenbox(closestRefColor == 2) = + 1;
else closestRefColor > 2
greenbox(closestRefColor > 2) = + 0;
end
if closestRefColor == 3
subplot(2, 3, 5);
bluebox(closestRefColor == 3) = + 1;
plot(boxX, boxY, 'b-', 'LineWidth', 2);
else closestRefColor > 3
bluebox(closestRefColor > 3) = + 0;
end
end
R = redbox;
G = greenbox;
B = bluebox;
C = [R,G,B];
K = sum(C);
disp({'num of object:' (K)});
Well yes, because you totally changed the code around so that you ruined it. Your if statement looks nothing like mine. As just one example of how your code doesn't work, what do you think this will do:
greenbox(closestRefColor ~= 2) = + 0;
it just sets the element to zero every time. But wait, it gets worse. What is the value of (closestRefColor ~= 2)? It's a logical value of true or false. Why are you doing that? I didn't do that. If this is false, it won't do anything - it won't set anything to zero. But if it's true, it will set the first element to zero. Review logical indexing in the help for an explanation.
You need to do it like this:
if closestRefColor >= 1
occurrenceOfColors(closestRefColor) = occurrenceOfColors(closestRefColor) + 1;
end
occurrenceOfColors will be an array of 1 with occurrenceOfColors(1) being the red count, occurrenceOfColors(2) being the green count, and occurrenceOfColors(3) being the blue count.
fiq
fiq el 28 de Dic. de 2012
Editada: fiq el 28 de Dic. de 2012
sir did you mean i should write it like this? seem i still getting the same result as previous one. maybe i wrote it wrongly
occurrenceOfColors(1) = closestRefColor == 1;
occurrenceOfColors(2) = closestRefColor == 2;
occurrenceOfColors(3) = closestRefColor == 3;
if closestRefColor == 1
occurrenceOfColors(closestRefColor == 1) = occurrenceOfColors(closestRefColor == 1) +1;
elseif closestRefColor ~= 1
occurrenceOfColors(closestRefColor >= 1) = occurrenceOfColors(closestRefColor >= 1) + 0;
end
if closestRefColor == 2
occurrenceOfColors(closestRefColor == 2) = occurrenceOfColors(closestRefColor == 2) +1;
elseif closestRefColor ~= 2
occurrenceOfColors(closestRefColor >= 2) = occurrenceOfColors(closestRefColor >= 2) + 0;
end
if closestRefColor == 3
occurrenceOfColors(closestRefColor == 3) = occurrenceOfColors(closestRefColor == 3) +1;
elseif closestRefColor ~= 3
occurrenceOfColors(closestRefColor >= 3) = occurrenceOfColors(closestRefColor >= 3) + 0;
end
end
K = sum(occurrenceOfColors);
disp({'num of object:' K});
I don't see any of that in my original code, except the variable name. You just kept the bad code I told you not to use, and all you did was to rename your variable. read my last response again. Again it says to do it this way:
if closestRefColor >= 1
occurrenceOfColors(closestRefColor) = occurrenceOfColors(closestRefColor) + 1;
end
It does NOT say to use those 30 lines of bad code you invented. Note, in my code, the index is closestRefColor, it is not a logical expression where I compare closestRefColor to some number. Please read that last sentence again.
fiq
fiq el 28 de Dic. de 2012
Editada: fiq el 28 de Dic. de 2012
sir when i use this code i get an error that said ??? Undefined function or method 'occurrenceOfColors' for input arguments of type 'double'.
Sorry - I thought you knew you had to initialize arrays before you use them. You need to put this code before you start accumulating anything into the array:
occurrenceOfColors = [0 0 0];
fiq
fiq el 28 de Dic. de 2012
Editada: fiq el 28 de Dic. de 2012
sir so it should be like this? so for the next 2 block i just repeat this code. is that right sir?
occurrenceOfColors = [0 0 0];
if closestRefColor >= 1
occurrenceOfColors(closestRefColor) = occurrenceOfColors(closestRefColor) + 1;
end
fiq
fiq el 28 de Dic. de 2012
Editada: fiq el 28 de Dic. de 2012
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
Image Analyst
Image Analyst el 28 de Dic. de 2012
Editada: Image Analyst el 28 de Dic. de 2012
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?
fiq
fiq el 28 de Dic. de 2012
Editada: fiq el 28 de Dic. de 2012
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

Iniciar sesión para comentar.

Más respuestas (1)

Sadid
Sadid el 25 de Dic. de 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 comentarios

fiq
fiq el 25 de Dic. de 2012
Editada: fiq el 25 de Dic. de 2012
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.

Iniciar sesión para comentar.

Categorías

Preguntada:

fiq
el 25 de Dic. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by