image processing - Issue regarding detection of red and green colour using Matlab and Arduino -
this question has answer here:
i'm working on smartcar project in on detecting red color car should stop , on detecting green color should start running. using matlab color detection , arduino run car. problem m not able detect green color, code detect red color , stop car. not able figure out problem.
my code is:
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 loop stop after 100 frames of aquisition i=1:100 imred = getsnapshot(vid); % snapshot of current frame diff_im = imsubtract(imred(:,:,1), rgb2gray(imred)); % subtract red component grayscale image extract red component in image. gr=graythresh(diff_im); diff_im1 = imsubtract(imred(:,:,2), rgb2gray(imred)); %subtract green component grayscale image extract green component in image. gr1=graythresh(diff_im1); diff_im = medfilt2(diff_im, [3 3]); % median filter filter noise. diff_im1 = medfilt2(diff_im1, [3 3]); % convert resulting grayscale image binary image. diff_im = im2bw(diff_im,.18); diff_im1 = im2bw(diff_im1,.05); % remove pixels less 300px diff_im = bwareaopen(diff_im,300); diff_im1 = bwareaopen(diff_im1,300); % label connected components in 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 car while (bw1>=1); fprintf(s,101); end while(~(bw2>=1)) % start 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
i'm getting such output:
i guess trying find pixels have high green values,
i tried in past , didn't work because of difficulty of deciding threshold under changing lighting , exposure conditions.
instead, try converting rgb values hue, saturation , lightness (hsl) or hsv; , see if given pixel's hue close hue of green. has worked me ignores saturation , lightness of pixel, 2 values can change wildly.
https://en.wikipedia.org/wiki/hsl_and_hsv
and
https://uk.mathworks.com/help/matlab/ref/rgb2hsv.html
also note, cheap cameras, high intensity green might not green @ on screen. might need adjust camera's exposure manually correct read-out of hue.
Comments
Post a Comment