Moving on to object detection, you will find this post very interesting, provided you understand it.
In this tutorial, I will show you how to detect an object based on difference in color of the object and that of the surroundings.
I hope you've read 'A piece of advice', my previous post.
Try it in this tutorial. See if you understand the program in one shot.
----------------------------------------------------------
Screenshot of the input video:
Output:
e.g. Red color detector
In this tutorial, I will show you how to detect an object based on difference in color of the object and that of the surroundings.
I hope you've read 'A piece of advice', my previous post.
Try it in this tutorial. See if you understand the program in one shot.
#include#include #include #include using namespace std; using namespace cv; Mat frame , frame_threshold; //Refer Main int rgb_slider=0; int low_slider=30, high_slider=100; int low_r=30, low_g=30, low_b=30, high_r=100, high_g=100, high_b=100; //r g b r g b void on_rgb_trackbar(int ,void *){ switch(rgb_slider){ case 0: setTrackbarPos("Low threshold","Segmentation",low_r); setTrackbarPos("High threshold","Segmentation",high_r); break; case 1: setTrackbarPos("Low threshold", "Segmentation", low_g); setTrackbarPos("High threshold","Segmentation", high_g); break; case 2: setTrackbarPos("Low threshold","Segmentation",low_b); setTrackbarPos("High threshold", "Segmentation",high_b); break; } } //go to Main again void on_low_thresh_trackbar(int, void*){ switch(rgb_slider){ case 0: low_r=min(high_slider-1, low_slider); setTrackbarPos("Low threshold","Segmentation",low_r); break; case 1: low_g=min(high_slider-1,low_slider); setTrackbarPos("Low threshold","Segmentation",low_g); break; case 2: low_b=min(high_slider-1,low_slider); setTrackbarPos("Low threshold","Segmentation",low_b); break; } } //go to Main again void on_high_thresh_trackbar(int, void*){ switch(rgb_slider){ case 0: high_r=max(high_slider, low_slider+1); setTrackbarPos("High threshold","Segmentation",high_r); break; case 1: high_g=max(high_slider,low_slider+1); setTrackbarPos("High threshold","Segmentation",high_g); break; case 2: high_b=max(high_slider,low_slider+1); setTrackbarPos("High threshold","Segmentation",high_b); break; } } int main() { VideoCapture cap(0); namedWindow("Video"); namedWindow("Segmentation"); createTrackbar("0.R\n1.G\n2.B","Segmentation",&rgb_slider, 2 ,on_rgb_trackbar); //observe the variables and callback function. // go back to where you've left. createTrackbar("Low threshold","Segmentation",&low_slider,255,on_low_thresh_trackbar); //observe the variables and callback function. // go back to where you've left. createTrackbar("High threshold","Segmentation",&high_slider,255,on_high_thresh_trackbar); //observe the variables and callback function. // go back to where you've left. while(char(waitKey(1)) != 'q') { cap>>frame; if(frame.empty()) break; inRange(frame,Scalar(low_b,low_g,low_r),Scalar(high_b,high_g,high_r),frame_threshold); //inRange function, refer text. imshow("Video",frame); imshow("Segmentation",frame_threshold); } return 0; }
So, inRange function basically filters out an image based on the color channels.
- The color channel sequence in opencv is B, G ,R and not R,G,B.
- Scalar(B,G,R) defines the strength of an image in terms of B, G, R.\
- If you remember, we had done a tutorial on increasing brightness of an image using TrackBar(Tutorial #6). The same function was used. I hope you got the idea.
- If you understood Scalar(), you've understood inRange.
- If not, refer DOCUMENTATION.
Screenshot of the input video:
(My room is completely messed up. So I cropped the screenshot. Ubuntu doesn't have a cropping utility, but, we know OpenCV now! First Tutorial, Cropping app!)
Output:
e.g. Red color detector
Wonderful, isn't it?


No comments:
Post a Comment