2017-04-07 1 views
0

빨강 색 감지시 자동차가 멈추고 녹색 색상 감지시 작동이 시작되어야하는 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 

:

내 코드는

code output data

+0

코드에 주석이 없다는 것을 알고 계셨습니까? –

+0

복제본은 나머지 픽셀을 무시하면서 특정 색상을 격리합니다. 문제 해결에 도움이됩니다. – rayryeng

답변

0

난 당신이 높은 녹색 값이 픽셀을 찾기 위해 노력하고 추측,

나는 조명과 노출 조건을 변화시키면서 임계 값을 결정하기가 어렵 기 때문에 과거에 시도해 보았습니다.

대신 rgb 값을 HSM (hue, saturation and lightness) 또는 HSV로 변환 해보십시오. 지정된 픽셀의 색조가 녹색의 색조에 가까운 지 확인합니다. 그것은 저에게 효과적입니다. 픽셀의 채도와 밝기, 즉 격렬하게 변할 수있는 두 값을 무시하기 때문입니다. 싸구려 카메라를 들어, 높은 강도의 녹색 화면에 모든 녹색처럼 보이지 않을 수도 그

https://en.wikipedia.org/wiki/HSL_and_HSV

또한

https://uk.mathworks.com/help/matlab/ref/rgb2hsv.html

있습니다. 색조를 정확하게 판독하려면 카메라의 노출을 수동으로 조정해야 할 수도 있습니다.

관련 문제