Opencv Quick and Dirty Tutorial: Introduction To Perception and Robotics - Ipr 2011
Opencv Quick and Dirty Tutorial: Introduction To Perception and Robotics - Ipr 2011
Ana Huaman
Ana Huaman
You will learn what is OpenCV and to use it for your projects. Specically we will see: Create a window to display images and video Get image and video from your webcam Get video stream from a remote webcam via http (MiGIO) Use lters to manipulate your image Blob detection Basic face detection Pretty basic Motion tracking
Ana Huaman
Before beginning...
ACHTUNG! All material of this tutorial (slides and C++ code) are on the Wiki
Ana Huaman
What is OpenCV?
Open Source Computer Vision in C/C++ Intended for real-time applications OS independent
Ana Huaman
OpenCV Features
Motion analysis:
Tracking Motion segmentation...
OpenCV Modules
(aka Those headers you normally put on top of your le) cv Main OpenCV functions cvaux Auxiliary (experimental) functions cxcore Data structures and linear algebra support highgui Graphical User interface functions
Template #include #include #include #include <stdio.h> <cv.h> <cxcore.h> <highgui.h>
int main(int argc, char* argv[]) { /** Your code here */ return 0; }
Ana Huaman
OpenCV Basics
Ana Huaman
IplImage* img = 0; img = cvLoadImage("Lama1.png"); cvNamedWindow("Display Image"); cvWaitKey(0); cvReleaseImage(&img); cvDestroyWindow("Display Image");
Ana Huaman
cvNamedWindow("Display Video"); cvCapture* capture = cvCreateFileCapture(2); IplImage* frame; while(1) { frame = cvQueryFrame(capture); cvShowImage("Display Video", frame); char c = cvWaitKey(33); if(c == 27) break; } cvReleaseImage(&img); cvDestroyWindow("Display Video");
Ana Huaman
cvNamedWindow("Capture Video"); cvCapture* capture=cvCreateCameraCapture(2); IplImage* frame; while(1) { frame = cvQueryFrame(capture); cvShowImage("Capture Video", frame); char c = cvWaitKey(33); if(c == 27) break; } cvReleaseImage(&img); cvDestroyWindow("Capture Video");
Ana Huaman
Ana Huaman
int main(int argc, char* argv[]) { cvCreateTrackbar("Switch", name, &g_switch_value, 1, switch_callback); /** ... More code ... */ while(1) { if(colorInt == 0) {cvCircle(src1, pt2, radius, green,2,8);} else {cvCircle(src1, pt2, radius, orange,2,8);} cvShowImage(name, src1); /** ... More code ... */ } /** ... More code ... */ } void switch_callback(int position) { if(position == 0) {colorInt = 0;} else {colorInt = 1;} }
Ana Huaman OpenCV quick and dirty tutorial
int main(int argc, char* argv[]) { cvSetMouseCallback( name, my_mouse_callback, (void*) image); /** ... More code ... */ } void my_mouse_callback(int event, int x, int y, int flags, void* param) { switch(event){ case CV_EVENT_MOUSEMOVE: /** Do something */ case CV_EVENT_LBUTTONDOWN: /** Do something */ case CV_EVENT_LBUTTONUP /** Do something */ } }
Ana Huaman
Filtering
Ana Huaman
(a) Input
(b) Output
Ana Huaman
Ana Huaman
Blob Detection
A Blob is a 8-connected component of pixels, that is,a group of pixels connected for some common characteristic (color) There are libraries around to do this, we are using cvBlobsLib You are welcome to try whatever works for you. However, be aware that I can only help you with the above library. Be also aware: For some reason, your TA was not able to run the latest version of the library (v8.3) with her setup (OpenCV 2.2 + Visual Studio 2010). She is using the v6.1. with a small x (I put it online too, to make your life easier than hers). If you can make work the latest version...share your knowledge with us please!
Ana Huaman
Ana Huaman
There are a few steps you must follow in order to get your Blobs Visualize what you want to detect. Let us say it is a red ball. So, you want to detect a red object Filter your image: Prepare the image to be processed (use cvSmooth and transform it to HSV format with cvCvtColor(img, hsv img, CV RGB2HSV) Segmentate your image: Separate what you want, use a lter Detect the blobs: Use functions in cvBlobsLib:CBlobResult Filter the blobs according to your needs (size, area, perimeter..): Use CBlobs.lter(...) Track them (follow their movement): This is the fun part!
Ana Huaman
Ana Huaman
Ana Huaman
Ana Huaman
Ana Huaman
/** Create storage to save face info */ CvMemStorage* storage = cvCreateMemStorage(0); /** Give to OpenCV a file with "face characteristics" to detect */ CvHaarClassifierCascade* cascade = (CvHaarClassifierCascade*) cvLoad("AdditionalStuff/haarcascade_frontalface_alt2.xml"); double scale = 1.3; static CvScalar colors[] = {{{0,255,255}}, {{0,128,255}},{{0,255,255}},{{255,0,255}}}; /** Detect objects (faces) */ cvClearMemStorage(storage); /** DETECTION! Faces no smaller than 20x30..approx. */ CvSeq* objects = cvHaarDetectObjects(img, cascade, storage, 1.1, 4, 0, cvSize(20,30)); CvRect* r; /** Loop through objects and draw boxes around faces */ for(int i =0; i<(objects ? objects->total :0); i++) { r = (CvRect*) cvGetSeqElem(objects, i); cvRectangle( img, cvPoint(r->x, r->y), cvPoint( r->x + r->width, r->y + r->height), colors[i%8],2); }
Ana Huaman
Ana Huaman
Ana Huaman
Ana Huaman