2017-04-07 9 views
0

赤い色を検出すると車が止まり緑色が検出されるとスマートカーのプロジェクトが始まります。私はカラー検出のために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値を色相、彩度、明度(HSL)またはHSVに変換してみてください。与えられたピクセルの色相が緑の色相に近いかどうかを確認します。それはピクセルの彩度と明度を無視するので、私のために働いています。これは大きく変化する2つの値です。

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

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

もいくつかの格安のカメラのために、高輝度の緑色が画面上ですべての緑のように見えないかもしれないことに注意。色相を正しく読み取るには、カメラの露出を手動で調整する必要があります。

関連する問題