Why is Matlab detecting only one color
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Guillaume
2019-10-23
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:
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!