版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、最近看到有人用 flash 和摄像头做了一个互动玩耍觉得很好玩,就利用 opencv 的库函数和 vc 仿照也做了个类似的呵呵。效果不是很好,拿出来献丑了。下面介绍一下,电脑上连接一个摄像头,启动程序后在视频窗口中就会有两个跳动的黄色小球作为你攻击的目标(小球搬来是要做成漂移的泡泡的,没找到动力学方程,就任凭设计了一个),还有一个白色的十字圈作为你的准星。拥有一个红的物体在摄像头前晃动来操纵你的准星,当准星击中黄色小球的时候小球就会消逝。(这个玩耍需要 opencv beta5 的动态连接库的支持。还要在当前名目下放置一副 320×240 的命名为 gun.bmp 的图,我用的就是图
2、中的这张卡,通过它来打算你能用什么颜色的物体操纵你的准星)下面是源代码:1./ auto.cpp : defines the entry point for the console applicati on.2./3. #include “stdafx.h“4. #include “cv.h“5. #include “highgui.h“6. #include “stdio.h“7. #include “ctype.h“8. #include “math.h“ 9.iplimage *image = 0, *hsv = 0, *hue = 0, *mask = 0, *backprojec t
3、 = 0, *histimg = 0;10. cvhistogram *hist = 0;11. int backproject_mode = 0;12. int select_object = 0;13. int track_object = 0;14. int show_hist = 1;15. int xn=0;16. int yn=0;17. int xn2=0;18. int yn2=0;19. cvpoint origin;20. cvpoint trackpoint;21. cvrect selection;22. cvrect track_window;23. cvbox2d
4、track_box;24. cvconnectedcomp track_comp;25. int hdims = 16;26. float hranges_arr = 0,180;27. float* hranges = hranges_arr;28. int vmin = 90, vmax = 256, smin = 90;29. cvscalar hsv2rgb( float hue );30. /加载预定图像31. void loadtemplateimage()32.33. iplimage *tempimage = cvloadimage(“gun.bmp“,1);34. cvcvt
5、color( tempimage, hsv, cv_bgr2hsv );35. int _vmin = vmin, _vmax = vmax; 36.cvinranges( hsv, cvscalar(0,smin,min(_vmin,_vmax),0),37. cvscalar(180,256,max(_vmin,_vmax),0), mask );38. cvsplit( hsv, hue, 0, 0, 0 );39. selection.x = 1;40. selection.y = 1; 41.42. selection.width = 320-1;43. selection.heig
6、ht= 240-1;44. cvsetimageroi( hue, selection );45. cvsetimageroi( mask, selection );46. cvcalchist( &hue, hist, 0, mask );47. float max_val = 0.f;48. cvgetminmaxhistvalue( hist, 0, &max_val, 0, 0 ); 49.cvconvertscale( hist->bins, hist->bins, max_val ? 255. / max_val :0.,0);50.cvresetima
7、geroi( hue );51.cvresetimageroi( mask );52.track_window = selection;53.track_object = 1;54.cvzero( histimg );55.int bin_w = histimg->width / hdims;56.for(int i = 0; i < hdims; i+ )57.58.intval=cvround( cvgetreal1d(hist->bins,i)*histimg->height/255 );59.cvscalar color = hsv2rgb(i*180.f/hd
8、ims);60.cvrectangle( histimg, cvpoint(i*bin_w,histimg->height),61.cvpoint(i+1)*bin_w,histimg->height - val),62.color, -1, 8, 0 );63.64.cvreleaseimage(&tempimage);65.66./鼠标大事回调函数67.voidon_mouse( int event, int x, int y, int flags, void*param)68.69.if( !image )70.return;71.if( image->orig
9、in )72.y = image->height - y;73.if( select_object )74.75.selection.x = min(x,origin.x);76.selection.y = min(y,origin.y);77.selection.width = selection.x + cv_iabs(x - origin.x); 78.selection.height = selection.y + cv_iabs(y - origin.y);79. selection.x = max( selection.x, 0 );80. selection.y = max
10、( selection.y, 0 ); 81.selection.width = min( selection.width, image->width ); 82.selection.height = min( selection.height, image->height );83. selection.width -= selection.x;84. selection.height -= selection.y;85.86.switch( event )87.88. case cv_event_lbuttondown:89. origin = cvpoint(x,y);90.
11、 selection = cvrect(x,y,0,0);91. select_object = 1; 92.93. break;94. case cv_event_lbuttonup:95. select_object = 0; 96.if( selection.width > 0 && selection.height > 0 )97. track_object = -1;98. break;99.100.101. /颜色空间102. cvscalar hsv2rgb( float hue )103.104.int rgb3, p, sector;105.sta
12、tic const int sector_data3=106.0,2,1,1,2,0, 1,0,2, 2,0,1, 2,1,0, 0,1,2;107.hue *= 0.033333333333333333333333333333333f;108.sector = cvfloor(hue);109.p = cvround(255*(hue - sector);110.p = sector & 1 ? 255 : 0;111.rgbsector_datasector0 = 255;112.rgbsector_datasector1 = 0;113.rgbsector_datasector2
13、 = p;114.return cvscalar(rgb2, rgb1, rgb0,0);115.116./划 popo117.void drawcorssmark(iplimage *dst, cvpoint pt)118.119.const int cross_len = 10;120.cvpoint pt1,pt2,pt3,pt4;121.pt1.x = pt.x;122.pt1.y = pt.y - cross_len;123.pt2.x = pt.x;124.pt2.y = pt.y + cross_len;125.pt3.x = pt.x - cross_len;126.pt3.y
14、 = pt.y;127.pt4.x = pt.x + cross_len;128.pt4.y = pt.y;129.cvcircle( dst, pt, 10, cv_rgb(255,255,255), 2,cv_aa,0); 130.cvline(dst,pt1,pt2,cv_rgb(255,255,255),2,cv_aa, 0 );131.cvline(dst,pt3,pt4,cv_rgb(255,255,255),2,cv_aa, 0 );132.133./点的移动134.void movepopo(iplimage *dst,int & xn,int & yn)135
15、.136.int xn1= xn+1;137.int i=-1;138.int yn1=(int)( (xn)+(20*sin(xn) );139.if (xn1 >= (dst->width)140.141.xn1=0;142.yn1=0;143. /* image width in pixels */144. if (yn1 >=(dst->height) 145.146.147.xn1=0;148.yn1=0;149.150.cvpoint pt=cvpoint(xn1, yn1); 151.cvcircle(dst, pt, 4, cv_rgb(230,255,
16、0), 2,cv_aa,0);152.xn=xn1;153.yn=yn1;154.155.void movepopo1(iplimage *dst,int & xn,int & yn)156.157.int xn1= xn+2;158.int yn1=(int)( (xn)+(10*sin(xn) );159.if (xn1 >= (dst->width)160.161.xn1=0;162.yn1=0;163. /* image width in pixels */164.if (yn1 >=(dst->height)165.166.xn1=0;167.
17、yn1=0;168.169.cvpoint pt=cvpoint(xn1, yn1);170.cvcircle(dst, pt, 4, cv_rgb(230,255,0), 2,cv_aa,0);171. xn=xn1;172. yn=yn1;173.174.int main( int argc, char* argv )175.176.cvcapture* capture = 0; 177.if( argc = 1 | (argc = 2 && strlen(argv1) = 1 && isdigit( argv10)178.capture = cvcaptu
18、refromcam( argc = 2 ? argv10 - ”0” : 0 );179. else if( argc = 2 )180. capture = cvcapturefromavi( argv1 );181. if( !capture )182.183.fprintf(stderr,“could not initialize capturing.n“);184.return -1;185.186. printf( “hot keys: n“187. “tesc - quit the programn“188. “tc - stop the trackingn“189. “tb -
19、switch to/from backprojection viewn“190. “th - show/hide object histogramn“ 191.“to initialize tracking, select the object with mousen“ );192. / cvnamedwindow( “histogram“, 0 );193. cvnamedwindow( “interactivecamgame“, 0 ); 194.cvsetmousecallback( “interactivecamgame“, on_mouse, 0 ); 195.196./ cvcre
20、atetrackbar( “vmin“, “camshiftdemo“, &vmin, 256, 0 );197./ cvcreatetrackbar( “vmax“, “camshiftdemo“, &vmax, 256, 0 );198./ cvcreatetrackbar( “smin“, “camshiftdemo“, &smin, 256, 0 );199.for(;)200.201. iplimage* frame = 0;202. /int i, bin_w, c;203. int c;204. frame = cvqueryframe( capture
21、);205. if( !frame )206. break;207. if( !image )208.209./* allocate all the buffers */ 210.image = cvcreateimage( cvgetsize(frame), 8, 3 );211.image->origin = frame->origin; 212.hsv = cvcreateimage( cvgetsize(frame), 8, 3 );213.hue = cvcreateimage( cvgetsize(frame), 8, 1 ); 214.mask = cvcreatei
22、mage( cvgetsize(frame), 8, 1 ); 215.backproject = cvcreateimage( cvgetsize(frame), 8, 1 ); 216.hist = cvcreatehist( 1, &hdims, cv_hist_array, &hranges, 1 );217.histimg = cvcreateimage( cvsize(320,200), 8, 3 );218. cvzero( histimg );219. / cvsaveimage(“a.bmp“,image);220. loadtemplateimage();2
23、21.222. cvcopy( frame, image, 0 );223. cvsaveimage(“a.bmp“,image);224. cvcvtcolor( image, hsv, cv_bgr2hsv );225. if( track_object )226.227.int _vmin = vmin, _vmax = vmax; 228.cvinranges( hsv, cvscalar(0,smin,min(_vmin,_vmax),0), 229.cvscalar(180,256,max(_vmin,_vmax),0), mask );230.cvsplit( hsv, hue,
24、 0, 0, 0 ); 231.cvcalcbackproject( &hue, backproject, hist ); 232.cvand( backproject, mask, backproject, 0 );233.cvcamshift( backproject, track_window, 234.cvtermcriteria( cv_termcrit_eps | cv_termcrit_iter, 10, 1 ),235. &track_comp, &track_box );236. track_window = track_comp.rect;237.
25、if( backproject_mode ) 238.cvcvtcolor( backproject, image, cv_gray2bgr );239. if( image->origin )240. track_box.angle = -track_box.angle; 241./ cvellipsebox( image, track_box, cv_rgb(255,0,0), 3, cv_aa, 0);242.drawcorssmark(image, cvpointfrom32f(track_box.center);243.movepopo(image,xn,yn);244.movepopo1(image, xn2, yn2);245./movepopo(image,image->width,image->height); 246.247.trackpoint= cvpointfrom32f(track_box.center); 248.if ( sqrt( (trackpoint.x-xn)*(trackpoint.x-xn) +249. (trackpoint.y-yn)*(trackpoint.y-yn) )<10 )250.251.xn=0;2
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论