빨강 색 감지시 자동차가 멈추고 녹색 색상 감지시 작동이 시작되어야하는 smartcar 프로젝트를 진행 중입니다. MATLAB을 사용하여 색상을 감지하고 Arduino를 사용하여 자동차를 주행합니다. 하지만 문제는 녹색 색상을 감지 할 수 없어 코드가 빨간색 만 감지하고 차를 멈출 수 있다는 것입니다. 나는 문제를 알아낼 수 없다. 나는 그런 출력을 얻고있다Matlab과 Arduino를 사용하여 빨강 및 녹색 색상 감지 관련 문제
vid = videoinput('winvideo',1 ,'YUY2_320x240');
s=serial('COM9','BAUD',9600);
fopen(s); %open serial port
set(vid, 'FramesPerTrigger', Inf);
set(vid, 'ReturnedColorspace', 'rgb')
vid.FrameGrabInterval = 10;
start(vid)
%set a loop that stop after 100 frames of aquisition
for i=1:100
IMRED = getsnapshot(vid); % get the snapshot of the current frame
diff_im = imsubtract(IMRED(:,:,1), rgb2gray(IMRED)); % subtract red component from the grayscale image to extract the red component in image.
gr=graythresh(diff_im);
diff_im1 = imsubtract(IMRED(:,:,2), rgb2gray(IMRED)); %subtract green component from the grayscale image to extract the green component in image.
gr1=graythresh(diff_im1);
diff_im = medfilt2(diff_im, [3 3]); % median filter to filter the noise.
diff_im1 = medfilt2(diff_im1, [3 3]);
% convert the resulting grayscale image into a binary image.
diff_im = im2bw(diff_im,.18);
diff_im1 = im2bw(diff_im1,.05);
% Remove all those pixels less than 300px
diff_im = bwareaopen(diff_im,300);
diff_im1 = bwareaopen(diff_im1,300);
% Label all the connected components in the image
[bw bw1] = bwlabel(diff_im, 8);
[L bw2] = bwlabel(diff_im1, 8);
if (bw1<=0 && bw2 <=0) % if no color detected run forward
fprintf(s,100);
elseif (bw1>=1) % if red detected stop the car
while (bw1>=1);
fprintf(s,101);
end
while(~(bw2>=1)) % start the car if green detected
fprintf(s,101);
end
fprintf(s,100);
if (bw2>=1)
fprintf(s,100);
else
fprintf(s,101);
end
else
fprintf(s,100);
end
imshow(IMRED)
hold on
hold off
end
stop(vid);
flushdata(vid);
delete(vid);
clear vid;
fclose(s);
clear all;
clc
:
내 코드는
코드에 주석이 없다는 것을 알고 계셨습니까? –
복제본은 나머지 픽셀을 무시하면서 특정 색상을 격리합니다. 문제 해결에 도움이됩니다. – rayryeng