31
OpenCV Detect Candle

Detect Candle. Open VC++ Directories configuration: Tools > Options > Projects and Solutions > VC++ Directories Choose "Show directories for: Include

Embed Size (px)

Citation preview

OpenCV

OpenCVDetect CandleOpenCV Part IConfigure Visual StudioOpen VC++ Directories configuration: Tools > Options > Projects and Solutions > VC++ Directories Choose "Show directories for: Include files" Add "Folder OpenCV>\include\opencv" Configure Visual Studio (2)Choose "Show directories for: Library files" Add "\lib" Choose "Show directories for: Source files" Add "\src\cv "Add "\src\cvaux "Add "\src\cxcore "Add "\src\highgui"Configure your ProjectOpen Linker Input properties: Configuration Properties > Linker > Input edit "Additional Dependencies"Add "cv210.lib"Add "cxcore210.lib" Add "highgui210.lib"IplImageIplImagecvQueryFrame(CvCapture* capture)cvCreateImage(CvSize size, int depth, int channels)cvLoadImage(const char* filename, int iscolor=CV_LOAD_IMAGE_COLOR)cvReleaseImage(IplImage** image)

HighGuiHighGuicvNamedWindow(const char* name, int flags)cvShowImage(const char* name, const CvArr* image)cvWaitKey(int delay=0)cvDestroyWindow(const char* name)cvDestroyAllWindows()

Sample #1#include #include #include

int main( int argc, char** argv ) {IplImage* img = cvLoadImage( photo.jpg);// photo.jpgcvNamedWindow( Example1, CV_WINDOW_AUTOSIZE );// cvShowImage( Example1, img );// cvWaitKey(0); // Wait for any keycvReleaseImage( &img ); // Clear MemorycvDestroyWindow( Example1 ); // }Sample #1

DrawingWrite StringcvInitFont(CvFont*font, intfontFace, doublehscale, doublevscale, doubleshear=0, intthickness=1, intlineType=8)cvPutText(CvArr*img, const char*text,CvPointorg, const CvFont*font,CvScalarcolor)Draw CirclecvCircle(CvArr*img,CvPointcenter, intradius,CvScalarcolor, intthickness=1, intlineType=8, intshift=0)Draw RectanglecvRectangle(CvArr*img,CvPointpt1,CvPointpt2,CvScalarcolor, intthickness=1, intlineType=8, intshift=0)Draw LinecvLine(CvArr*img,CvPointpt1,CvPointpt2,CvScalarcolor, intthickness=1, intlineType=8, intshift=0)Sample #2//Print stringCvFont font;cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 1.0, 1.0, 0, 1, CV_AA);cvPutText(img,"Hello", cvPoint(20,20), &font, cvScalar(255, 255, 255, 0));

//Draw x-markcvLine(img,cvPoint(0,0),cvPoint(10,10),cvScalar(0,0,255,0))cvLine(img,cvPoint(10,0),cvPoint(0,10),cvScalar(0,0,255,0))

Sample #2

Color FilterAccess image data RGB imageImgHeight = x , ImgWidth = yImage[x][y] = (img->imageData + x * img->widthStep)+ (3*y)Red Channel = Image[x][y] + 2Green Channel = Image[x][y] + 1Blue Channel = Image[x][y]

Sample #3//Convert image to red channelvoid cutoff_gb ( IplImage* img ) {for( int x=0; xheight; x++ ) {uchar* ptr = (uchar*) (img->imageData + x * img->widthStep);for( int y=0; ywidth; y++ ) {ptr[3*y] = 0;ptr[3*y+1] = 0;}}}Sample #3

Color Filter(2)Convert color spacecvCvtColor(CvArr*src,CvArr*dst, intcode)code = X2Y (such asRGB2GRAY , RGB2HSV)Blur imagecvSmooth(CvArr*src,CvArr*dst, intsmoothtype=CV_GAUSSIAN, intparam1=3, intparam2=0)Cut thresholdcvThreshold(CvArr*src,CvArr*dst, doublethreshold, doublemaxValue, intthresholdType)

Threshold type

Threshold type(2)

OpenCV Part IICameraGet Capture from cameraCvCapture* capture = cvCreateCameraCapture( int index );Get Image from captureIplImage *img = cvQueryFrame( capture );Sample #4//Capture from cameraCvCapture* capture = cvCreateCameraCapture( 0 );IplImage* img = cvQueryFrame( capture );

cvNamedWindow( "Input", CV_WINDOW_AUTOSIZE );

while(cvWaitKey(5) != 27) // wait 5 ms for press ESC{cvShowImage( "Input", img );img = cvQueryFrame( capture );}cvDestroyAllWindows();Detect CircleIplImage* temp = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1);cvCvtColor(img,temp,CV_RGB2GRAY);cvSmooth(temp,temp,CV_GAUSSIAN,7,7);CvMemStorage* MemStorage = cvCreateMemStorage(0);CvSeq* Circles = cvHoughCircles(temp,MemStorage,CV_HOUGH_GRADIENT,DP,MIN_DIST,200,100);for (int i = 0; i < Circles->total; i++) {float* c = (float*)cvGetSeqElem( Circles, i );if( cvRound(c[2]) < MIN_RADIUS || cvRound(c[2]) > MAX_RADIUS)continue;

//Draw cvCircle( result, cvPoint(cvRound(c[0]),cvRound(c[1])),3, CV_RGB(0,255,0), -1, 8, 0 );cvCircle( result, cvPoint(cvRound(c[0]),cvRound(c[1])),cvRound(c[2]), CV_RGB(255,0,0), 3, 8, 0 );//char radius_word[100];sprintf(radius_word,"%d",cvRound(c[2]));cvPutText(result,radius_word, cvPoint(cvRound(c[0]),cvRound(c[1])), &font, cvScalar(255, 0, 0, 0));}Detect Circle

ROICvRectcvRect( int x, int y, int width, int height ) ROI (Region Of Interest)cvSetImageROI(IplImage*image,CvRectrect)cvResetImageROI(IplImage*image)cvCopy(CvArr*src,CvArr*dst, CvArr*mask=NULL)MissionMissionCapture from camera

cvCvtColor

cvSmooth

cvHoughCircles

Cut ROI