|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2012-8-29 17:01:27
|
显示全部楼层
本帖最后由 yirenonege 于 2012-8-29 17:36 编辑
- #ifdef _CH_
- #pragma package <opencv>
- #endif
- #ifndef _EiC
- #include "cv.h"
- #include "highgui.h"
- #include <stdio.h>
- #include <ctype.h>
- #endif
- IplImage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *backproject = 0, *histimg = 0;
- CvHistogram *hist = 0;
- int backproject_mode = 0;
- int select_object = 0;
- int track_object = 0;
- int show_hist = 1;
- CvPoint origin;
- CvRect selection;
- CvRect track_window;
- CvBox2D track_box;
- CvConnectedComp track_comp;
- int hdims = 16;
- float hranges_arr[] = {0,180};
- float* hranges = hranges_arr;
- int vmin = 10, vmax = 256, smin = 30;
- void on_mouse( int event, int x, int y, int flags, void* param )
- {
- if( !image )
- return;
- if( image->origin )
- y = image->height - y;
- if( select_object )
- {
- selection.x = MIN(x,origin.x);
- selection.y = MIN(y,origin.y);
- selection.width = selection.x + CV_IABS(x - origin.x);
- selection.height = selection.y + CV_IABS(y - origin.y);
-
- selection.x = MAX( selection.x, 0 );
- selection.y = MAX( selection.y, 0 );
- selection.width = MIN( selection.width, image->width );
- selection.height = MIN( selection.height, image->height );
- selection.width -= selection.x;
- selection.height -= selection.y;
- }
- switch( event )
- {
- case CV_EVENT_LBUTTONDOWN:
- origin = cvPoint(x,y);
- selection = cvRect(x,y,0,0);
- select_object = 1;
- break;
- case CV_EVENT_LBUTTONUP:
- select_object = 0;
- if( selection.width > 0 && selection.height > 0 )
- track_object = -1;
- break;
- }
- }
- CvScalar hsv2rgb( float hue )
- {
- int rgb[3], p, sector;
- static const int sector_data[][3]=
- {{0,2,1}, {1,2,0}, {1,0,2}, {2,0,1}, {2,1,0}, {0,1,2}};
- hue *= 0.033333333333333333333333333333333f;
- sector = cvFloor(hue);
- p = cvRound(255*(hue - sector));
- p ^= sector & 1 ? 255 : 0;
- rgb[sector_data[sector][0]] = 255;
- rgb[sector_data[sector][1]] = 0;
- rgb[sector_data[sector][2]] = p;
- return cvScalar(rgb[2], rgb[1], rgb[0],0);
- }
- int main( int argc, char** argv )
- {
- CvCapture* capture = 0;
-
- if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
- capture = cvCaptureFromCAM( argc == 2 ? argv[1][0] - '0' : 0 );
- else if( argc == 2 )
- capture = cvCaptureFromAVI( argv[1] );
- if( !capture )
- {
- fprintf(stderr,"Could not initialize capturing...\n");
- return -1;
- }
- printf( "Hot keys: \n"
- "\tESC - quit the program\n"
- "\tc - stop the tracking\n"
- "\tb - switch to/from backprojection view\n"
- "\th - show/hide object histogram\n"
- "To initialize tracking, select the object with mouse\n" );
- cvNamedWindow( "Histogram", 1 );
- cvNamedWindow( "CamShiftDemo", 1 );
- cvSetMouseCallback( "CamShiftDemo", on_mouse, 0 );
- cvCreateTrackbar( "Vmin", "CamShiftDemo", &vmin, 256, 0 );
- cvCreateTrackbar( "Vmax", "CamShiftDemo", &vmax, 256, 0 );
- cvCreateTrackbar( "Smin", "CamShiftDemo", &smin, 256, 0 );
- for(;;)
- {
- IplImage* frame = 0;
- int i, bin_w, c;
- frame = cvQueryFrame( capture );
- if( !frame )
- break;
- if( !image )
- {
- /* allocate all the buffers */
- image = cvCreateImage( cvGetSize(frame), 8, 3 );
- image->origin = frame->origin;
- hsv = cvCreateImage( cvGetSize(frame), 8, 3 );
- hue = cvCreateImage( cvGetSize(frame), 8, 1 );
- mask = cvCreateImage( cvGetSize(frame), 8, 1 );
- backproject = cvCreateImage( cvGetSize(frame), 8, 1 );
- hist = cvCreateHist( 1, &hdims, CV_HIST_ARRAY, &hranges, 1 );
- histimg = cvCreateImage( cvSize(320,200), 8, 3 );
- cvZero( histimg );
- }
- cvCopy( frame, image, 0 );
- cvCvtColor( image, hsv, CV_BGR2HSV );
- if( track_object )
- {
- int _vmin = vmin, _vmax = vmax;
- cvInRangeS( hsv, cvScalar(0,smin,MIN(_vmin,_vmax),0),
- cvScalar(180,256,MAX(_vmin,_vmax),0), mask );
- cvSplit( hsv, hue, 0, 0, 0 );
- if( track_object < 0 )
- {
- float max_val = 0.f;
- cvSetImageROI( hue, selection );
- cvSetImageROI( mask, selection );
- cvCalcHist( &hue, hist, 0, mask );
- cvGetMinMaxHistValue( hist, 0, &max_val, 0, 0 );
- cvConvertScale( hist->bins, hist->bins, max_val ? 255. / max_val : 0., 0 );
- cvResetImageROI( hue );
- cvResetImageROI( mask );
- track_window = selection;
- track_object = 1;
- cvZero( histimg );
- bin_w = histimg->width / hdims;
- for( i = 0; i < hdims; i++ )
- {
- int val = cvRound( cvGetReal1D(hist->bins,i)*histimg->height/255 );
- CvScalar color = hsv2rgb(i*180.f/hdims);
- cvRectangle( histimg, cvPoint(i*bin_w,histimg->height),
- cvPoint((i+1)*bin_w,histimg->height - val),
- color, -1, 8, 0 );
- }
- }
- cvCalcBackProject( &hue, backproject, hist );
- cvAnd( backproject, mask, backproject, 0 );
- cvCamShift( backproject, track_window,
- cvTermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ),
- &track_comp, &track_box );
- track_window = track_comp.rect;
-
- if( backproject_mode )
- cvCvtColor( backproject, image, CV_GRAY2BGR );
- if( image->origin )
- track_box.angle = -track_box.angle;
- cvEllipseBox( image, track_box, CV_RGB(255,0,0), 3, CV_AA, 0 ); // cvEllipseBox是画一个椭圆的函数,我记得应该有中心点的位置,要看opencv函数的原型
- }
-
- if( select_object && selection.width > 0 && selection.height > 0 )
- {
- cvSetImageROI( image, selection );
- cvXorS( image, cvScalarAll(255), image, 0 );
- cvResetImageROI( image );
- }
- cvShowImage( "CamShiftDemo", image );
- cvShowImage( "Histogram", histimg );
- c = cvWaitKey(10);
- if( (char) c == 27 )
- break;
- switch( (char) c )
- {
- case 'b':
- backproject_mode ^= 1;
- break;
- case 'c':
- track_object = 0;
- cvZero( histimg );
- break;
- case 'h':
- show_hist ^= 1;
- if( !show_hist )
- cvDestroyWindow( "Histogram" );
- else
- cvNamedWindow( "Histogram", 1 );
- break;
- default:
- ;
- }
- }
- cvReleaseCapture( &capture );
- cvDestroyWindow("CamShiftDemo");
- return 0;
- }
- #ifdef _EiC
- main(1,"camshiftdemo.c");
- #endif
复制代码 我安装的是1.0版本的opencv 在C:\Program Files\OpenCV\samples\c里有一个可执行的程序camshiftdemo.exe,你要有摄像头就能运行,里面还有它的源代码。当年吵得沸沸扬扬的绿坝里,骗取人民大量税收的大骗子用的是这个开源的视觉处理库。。。另外串口部分,用windows 或者是linux下的串口程序就好了,反正都是大家熟悉的c语言。 至于控制部分,我是把图像分成9个区,跟踪的物体的坐标落在哪个区域里了判断出来发给单片机就行了
// cvEllipseBox是画一个椭圆的函数,我记得应该有中心点的位置的参数,要看opencv官方提供的函数的原型的解释。
图像处理识别不是电子工程师不可触及的雷区,理解了他们的含义调用起来。希望有志于此的本科生学好数学,考研继续研究,也就是好多人疑问要不要读研究生,工作经验不是更好。像这种相对专业的东西,基本上都是得花时间专门搞的,那时候就是写库给别人用的层次了。
此处是当时的效果图,http://www.amobbs.com/thread-4626646-1-1.html,论坛改了之后地址变了,可惜不能更新到楼主位置了。提示一下可以用来做太阳能电池板跟踪吆。读书的时候拓展一下知识面,挺好的。毕业了也就没时间了,基本上此贴要说的也就这么多了 |
|