Why is Matlab detecting only one color

2 views (last 30 days)
Jonathan
Jonathan on 23 Oct 2019
Commented: Guillaume on 23 Oct 2019
I am trying to detect two colors using matlab, red and blue. I want to detect either a red or blue object so that the robot will come and pick the object and place it in the appropriate bin according to its color. However, the code I wrote is only detecting one color. In place of "for" I tried to use "if" and "elseif" statement but still it was detecting only one of the two colors. What could be the problem? Shown below is my code:
%Initialization
redThresh = 0.24; % Threshold for red detection
blueThresh = 0.15; % Threshold for blue detection
vid = videoinput('winvideo',1);
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 10;
nFrame = 0; % Frame number initialization
start(vid)
% Processing loop
while(nFrame < 10)
s = getsnapshot(vid); % Acquire single frame
for redThresh=0.24 %&& redThresh <= 0.26
% s=rgbFrame();
red = s(:,:,1);
diffFrameRed = imsubtract(s(:,:,1), rgb2gray(s));
diffFrameRed = medfilt2(diffFrameRed, [3 3]);
binFrameRed = imbinarize(diffFrameRed, redThresh);
binFrameRed = bwareaopen(binFrameRed,300);
bw = bwlabel(binFrameRed, 8);
stats = regionprops((bw), 'BoundingBox', 'Centroid');
centroids = cat(1, stats.Centroid);
imshow(s);
hold on
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','r','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
X = bc(1); Y = bc(2);
end
hold off
end
for blueThresh = 0.13% && blueThresh <= 0.16
blue = s(:,:,3);
diffFrameBlue = imsubtract(s(:,:,3), rgb2gray(s));
diffFrameBlue = medfilt2(diffFrameBlue, [3 3]);
binFrameBlue = imbinarize(diffFrameBlue, blueThresh);
binFrameBlue = bwareaopen(binFrameBlue,300);
bw = bwlabel(binFrameBlue, 8);
stats = regionprops((bw), 'BoundingBox', 'Centroid');
centroids = cat(1, stats.Centroid);
imshow(s);
hold on
for object = 1:length(stats)
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
rectangle('Position',bb,'EdgeColor','b','LineWidth',2)
plot(bc(1),bc(2), '-m+')
a=text(bc(1)+15,bc(2), strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'blue');
X = bc(1); Y = bc(2);
end
hold off
end
pause(5)
nFrame = nFrame+1;
end
  4 Comments
Jonathan
Jonathan on 23 Oct 2019
@Rik and @Guillaume
Thank you so much for your responses.
I want that when the colored components are moving on a conveyor, when the red one is in the field of view then it is detected, then picked and placed to a respective location. Then when a blue one is in view, in the same way, it is detected and then picked by the robot to a respective location. How am I supposed to code for that to happen?
Your help is greatly appreciated
Guillaume
Guillaume on 23 Oct 2019
It seems to me that if you remove the two lines
for redThresh=0.24 %&& redThresh <= 0.26
for blueThresh = 0.13% && blueThresh <= 0.16
your code should work as designed. First detect the red objects and highlight them, then detect the blue objects and highlight them. If an object is detected as both red and blue then it'd be highlighted as blue since that the last thing done.
I assume you've read up on colour detection and selected your algorithm accordingly. Your algorithm won't work on generic images but perhaps for your use case, 'red' and 'blue' are sufficiently separated in the RGB colour space.
This is the result of your algorithm on an artificial image:
separation.png

Sign in to comment.

Answers (0)

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Tags

No tags entered yet.

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!