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:
![separation.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/244370/separation.png)
답변 (0개)
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!