[计算机软件及应用]LearningOpenCV课后答案_第1页
[计算机软件及应用]LearningOpenCV课后答案_第2页
[计算机软件及应用]LearningOpenCV课后答案_第3页
[计算机软件及应用]LearningOpenCV课后答案_第4页
[计算机软件及应用]LearningOpenCV课后答案_第5页
已阅读5页,还剩48页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、 the learning of opencvhere contains just the exercises in book learning opencv and of course i believe there is better solution for blems: 1. if you set cvsetimagecoi, dont forget to set it back with parameter 0, otherwise you may end up with single channel.2. if you get your iplimage from

2、cvcapture, then do not release it as it is not created by you!3. there is some memory allocation in cvsubimageheader, and do not forget to release them. 4. i tried to erase my drawing lines by xor it again, but it seems not working by using cvgetrow. maybe it is because i mess up with cvsetimageroi

3、without reset it. so, i just use cvcopy to overwrite it.5. when you draw your rectangle of some width of line, be careful that the rectangle is bigger than that width of line.6. the color sequence in bytes order is bgr. the following are exercises in chapter 4./#include #include #include #include #i

4、nclude #include #include #include #pragma comment(lib, cvd.lib)#pragma comment(lib, cxcored.lib)#pragma comment(lib, highguid.lib)cvfont myfont = cvfont(1, 2);char* szcombinename = combine image;bool bmousebuttondown = false;int x = 0, y = 0;void dodrawing(iplimage* pimage)char buffer256;int winx =

5、0, winy = 0;if (bmousebuttondown)winx = x;winy = y;sprintf(buffer, mouse click at %d,%d, x, y);cvputtext(pimage, buffer, cvpoint(winx, winy), &myfont, cvscalar(255,0,0);void mymousecallback(int event, int myx, int myy, int flag, void* puser)switch (event)case cv_event_lbuttondown:bmousebuttondown =

6、true;x = myx;y = myy;break;case cv_event_lbuttonup:bmousebuttondown = false;break;void exercise1()char* szvideoname = e:mytest.avi;char* szwindowname = my video;char* szgrayname = gray image;char* szcannyname = canny image;cvcapture* pcapture = null;iplimage* pimage = null;iplimage* pgrayimg = null;

7、iplimage* pcannyimg = null;iplimage* pcombineimg = null;iplimage* psub1= null;iplimage* psub2= null;iplimage* psub3= null;int nwidth = 0, nheight = 0;cvnamedwindow(szwindowname);cvnamedwindow(szgrayname);cvnamedwindow(szcannyname);cvnamedwindow(szcombinename);pcapture = cvcreatefilecapture(szvideona

8、me);if (pcapture)nwidth = (int) cvgetcaptureproperty(pcapture, cv_cap_prop_frame_width);nheight = (int) cvgetcaptureproperty(pcapture, cv_cap_prop_frame_height);pgrayimg = cvcreateimage(cvsize(nwidth, nheight), 8, 1);pcannyimg = cvcreateimage(cvsize(nwidth, nheight), 8, 1);pcombineimg = cvcreateimag

9、e(cvsize(nwidth, 3*nheight), 8, 1);psub1 = cvcreateimageheader(cvsize(nwidth, nheight), 8, 1);psub2 = cvcreateimageheader(cvsize(nwidth, nheight), 8, 1);psub3 = cvcreateimageheader(cvsize(nwidth, nheight), 8, 1);psub1-origin = pcombineimg-origin;psub2-origin = pcombineimg-origin;psub3-origin = pcomb

10、ineimg-origin;psub1-widthstep = pcombineimg-widthstep;psub2-widthstep = pcombineimg-widthstep;psub3-widthstep = pcombineimg-widthstep;psub1-imagedata = pcombineimg-imagedata;psub2-imagedata = pcombineimg-imagedata + nheight*pcombineimg-widthstep;psub3-imagedata = pcombineimg-imagedata + nheight*2*pc

11、ombineimg-widthstep;cvsetmousecallback(szcombinename, mymousecallback, pcombineimg);/cvresizewindow(szcombinename, pcombineimg-width, pcombineimg-height);doif (cvwaitkey(15) = 27)break;pimage = cvqueryframe(pcapture);if (pimage)cvconvertimage(pimage, pgrayimg, 0);cvcanny(pgrayimg, pcannyimg, 1, 2, 5

12、);/cvshowimage(szwindowname, pimage);/cvshowimage(szgrayname, pgrayimg);/cvshowimage(szcannyname, pcannyimg);cvsetimagecoi(pimage, 1);cvcopy(pimage, psub1, null);cvcopy(pgrayimg, psub2, null);cvcopy(pcannyimg, psub3, null);cvputtext(psub1, szwindowname, cvpoint(nwidth/2, nheight/2), &myfont, cvscala

13、r(255,0,0);cvputtext(psub2, szgrayname, cvpoint(nwidth/2, nheight/2), &myfont, cvscalar(255,0,0);cvputtext(psub3, szcannyname, cvpoint(nwidth/2, nheight/2), &myfont, cvscalar(255,0,0);dodrawing(pcombineimg);cvshowimage(szcombinename, pcombineimg);cvsetimagecoi(pimage, 0);while (true);/ do not releas

14、e pimage because it is only retrieved from capture/cvreleaseimage(&pimage);cvreleaseimageheader(&psub1);cvreleaseimageheader(&psub2);cvreleaseimageheader(&psub3);cvreleaseimage(&pgrayimg);cvreleaseimage(&pcannyimg);cvreleaseimage(&pcombineimg);cvreleasecapture(&pcapture);cvdestroyallwindows();void m

15、ymouseonclick(int event, int x, int y, int flag, void* puser)char buffer256;cvfont myfont = cvfont(2, 2);iplimage* pimage = (iplimage*)puser;char* ptr = null;unsigned char r=0, g=0, b=0;switch (event)case cv_event_lbuttondown:ptr = pimage-imagedata + y*pimage-widthstep + x * pimage-depth* pimage-nch

16、annels/8;r = ptr0;g = ptr1;b = ptr2;sprintf(buffer, rgb %d,%d,%d, r,g,b);cvputtext(pimage, buffer, cvpoint(x, y), &myfont, cvscalar(255,0,0);break;void exercise2()iplimage* pimage = null; /, *pcloneimage = null;pimage = cvloadimage(mytest.jpg);/pcloneimage = cvcloneimage(pimage);cvnamedwindow(mytest

17、);cvsetmousecallback(mytest, mymouseonclick, pimage);docvshowimage(mytest, pimage);if (cvwaitkey(1000)=27)break;while (true);cvreleaseimage(&pimage);cvdestroywindow(mytest);#define histogramwidth 48#define histogramheight 800iplimage* pimage = null, *pcloneimage = null, *phistimage= null;cvrect rect

18、 = cvrect(0,0,0,0);bool bstart = false;#if 0void eraseframe()cvmat vecsrc, vecdst;cvsetimageroi(pimage, rect);cvsetimageroi(pcloneimage, rect);cvgetrow(pimage, &vecsrc, 0);cvgetrow(pcloneimage, &vecdst, 0);cvcopy(&vecsrc, &vecdst);cvgetrow(pimage, &vecsrc, rect.height-1);cvgetrow(pcloneimage, &vecds

19、t, rect.height-1);cvcopy(&vecsrc, &vecdst);cvgetcol(pimage, &vecsrc, 0);cvgetcol(pcloneimage, &vecdst, 0);cvcopy(&vecsrc, &vecdst);cvgetcol(pimage, &vecsrc, rect.width-1);cvgetcol(pcloneimage, &vecdst, rect.width-1);cvcopy(&vecsrc, &vecdst);cvresetimageroi(pimage);cvresetimageroi(pcloneimage);#elsev

20、oid eraseframe()cvsetimageroi(pimage, rect);cvsetimageroi(pcloneimage, rect);cvcopy(pcloneimage, pimage);cvresetimageroi(pimage);cvresetimageroi(pcloneimage);#endif#define line_width 1void drawframe()cvrectangle(pimage, cvpoint(rect.x, rect.y), cvpoint(rect.x+rect.width-1, rect.y+rect.height-1), cvs

21、calarall(255), line_width);/cvline(pimage, cvpoint(rect.x, rect.y), cvpoint(rect.x+rect.width, rect.y+rect.height), cvscalarall(255), line_width);void drawhistogram()char* ptr = null, *prow = null, *pcol = null;char buffer32;int hist38= 0;int index = 0;int npixwidth = 0;int r, c, i, noffset = 0;int

22、x = 0 , y = 0;cvfont myfont = cvfont(1,1);int nmax = 0, nscale=1;npixwidth = pcloneimage-nchannels*pcloneimage-depth/8;ptr = pcloneimage-imagedata + rect.y*pcloneimage-widthstep + rect.x*npixwidth;prow = ptr;for (r = 0; r rect.height; r +)pcol = prow;for (c = 0; c rect.width; c +)for (i =0; i nmax)n

23、max = histiindex;pcol += npixwidth;prow += pcloneimage-widthstep;cvset(phistimage, cvscalarall(255);y = histogramheight;nscale = histogramheight / histogramheight;if (nscale = 0)nscale = 1;for (c = 0; c 8; c +)for (i = 0; i line_width & h line_width)if (w != rect.width | h != rect.height)if (rect.wi

24、dth 0 & rect.height 0)eraseframe();/rect.width = w;rect.height = h;/cvsetimageroi(pimage, rect);/cvsetimageroi(pcloneimage, rect);/cvset(pimage, cvscalarall(255);drawframe();/cvresetimageroi(pimage);/cvresetimageroi(pcloneimage);break;case cv_event_lbuttondown:if (rect.width 0 & rect.height 0)cvseti

25、mageroi(pimage, rect);cvsetimageroi(pcloneimage, rect);cvcopy(pcloneimage, pimage);cvresetimageroi(pimage);cvresetimageroi(pcloneimage);/rect.x = x;rect.y = y;rect.width = rect.height = 0;bstart = true;break;case cv_event_lbuttonup:if (rect.width 0 & rect.height 0)cvsetimageroi(pimage, rect);cvset(p

26、image, cvscalarall(255);cvresetimageroi(pimage);drawhistogram();/bstart = false;break;void exercise3()pimage = cvloadimage(mytest.jpg);pcloneimage = cvcloneimage(pimage);cvnamedwindow(histogram);cvnamedwindow(mytest);cvsetmousecallback(mytest, mymousecallback3, null);phistimage = cvcreateimage(cvsiz

27、e(8 * histogramwidth * 3, histogramheight+200), 8, 3);docvshowimage(mytest, pimage);if (cvwaitkey(100)=27)break;while (true);cvreleaseimage(&pimage);cvreleaseimage(&pcloneimage);cvdestroywindow(mytest);int apientry winmain(hinstance hinstance,hinstance hprevinstance,lpstr lpcmdline,int ncmdshow)exer

28、cise3();/mytest();return 0;/there is a problem that i dont know how to control the program flow. for example, i setup two cvwaitkey because i place one in my main program and another one in the while loop of playing video. you see, if you dont place a cvwaitkey inside the video playing loop, then th

29、ere is simply no redrawing of window. just image there is no overlapping threads to either decode video or draw window.the following is a simple program acting as an video player(exercise4)#include #include #include #include #include #include #include #include #pragma comment(lib, cvd.lib)#pragma co

30、mment(lib, cxcored.lib)#pragma comment(lib, highguid.lib)int g_nprogress = 0;int g_npause = 0;int g_nwait = 0;cvcapture* pcapture = null;int nvalue = -1;char* szwindowname = myplayer;void switchcallback4(int npos)double fpos = 0.0;fpos = (double)npos / (double)10;if (pcapture)cvsetcaptureproperty(pc

31、apture, cv_cap_prop_pos_avi_ratio, fpos);void myplayer()iplimage* pimage = null;doif (g_npause = 0)break;pimage = cvqueryframe(pcapture);if (pimage)cvshowimage(szwindowname, pimage);nvalue = cvwaitkey(50);while (nvalue = -1);void switchcallback5(int npos)if (npos = 1)myplayer();void exercise4()pcapt

32、ure = cvcreatefilecapture(mytest.avi);if (pcapture)cvnamedwindow(szwindowname);cvcreatetrackbar(progress, szwindowname, &g_nprogress, 10, switchcallback4);cvcreatetrackbar(pause, szwindowname, &g_npause, 1, switchcallback5);doif (cvwaitkey(0) = 27)break;while(true);cvreleasecapture(&pcapture);cvdest

33、roywindow(szwindowname);int apientry winmain(hinstance hinstance,hinstance hprevinstance,lpstr lpcmdline,int ncmdshow)exercise4();return 0;chapter 5 exercises:#include #include #include #include #include #include #include #include #pragma comment(lib, cvd.lib)#pragma comment(lib, cxcored.lib)#pragma

34、 comment(lib, highguid.lib)void exercise1()char* szwindowname = loadimage, simpleimage, simplenoscale, nomedian, guassian, bilateral;iplimage* pimage = null, *psimpleimg = null, *pnoscaleimg = null, *pmedianimg = null, *pgaussianimg = null, *pbilateralimg = null;int i = 0;for (i = 0; i width, pimage

35、-height), pimage-depth, pimage-nchannels);pnoscaleimg = cvcreateimage(cvsize(pimage-width, pimage-height), ipl_depth_16s, pimage-nchannels);pmedianimg = cvcreateimage(cvsize(pimage-width, pimage-height), pimage-depth, pimage-nchannels);pgaussianimg = cvcreateimage(cvsize(pimage-width, pimage-height)

36、, pimage-depth, pimage-nchannels);pbilateralimg = cvcreateimage(cvsize(pimage-width, pimage-height), pimage-depth, pimage-nchannels);cvsmooth(pimage, psimpleimg, cv_blur, 5,5, 5,5);cvsmooth(pimage, pnoscaleimg, cv_blur_no_scale,11,11, 32,0.5);cvsmooth(pimage, pmedianimg, cv_median,5,5, 5,5);cvsmooth

37、(pimage, pgaussianimg, cv_gaussian,5,5, 5,5);cvsmooth(pimage, pbilateralimg, cv_bilateral, 3,3,0.01, 0.003);cvshowimage(szwindowname0, pimage);cvshowimage(szwindowname1, psimpleimg);cvshowimage(szwindowname2, pnoscaleimg);cvshowimage(szwindowname3, pmedianimg);cvshowimage(szwindowname4, pgaussianimg

38、);cvshowimage(szwindowname5, pbilateralimg);cvwaitkey(0);cvreleaseimage(&pimage);cvreleaseimage(&psimpleimg);cvreleaseimage(&pnoscaleimg);cvreleaseimage(&pmedianimg);cvreleaseimage(&pgaussianimg);cvreleaseimage(&pbilateralimg);cvdestroyallwindows();void exercise2()char* szwindowname = original, 3x3,

39、 5x5, 5x5 second, 9x9, 11x11;iplimage* pimage = null, *pimage3 = null, *pimage5 = null, *pimage5_2 = null, *pimage7 = null, *pimage9 = null, *pimage11 = null;int i = 0;for (i = 0; i width, pimage-height), pimage-depth, pimage-nchannels);pimage5 = cvcreateimage(cvsize(pimage-width, pimage-height), pi

40、mage-depth, pimage-nchannels);pimage5_2 = cvcreateimage(cvsize(pimage-width, pimage-height), pimage-depth, pimage-nchannels);pimage7 = cvcreateimage(cvsize(pimage-width, pimage-height), pimage-depth, pimage-nchannels);pimage9 = cvcreateimage(cvsize(pimage-width, pimage-height), pimage-depth, pimage-

41、nchannels);pimage11 = cvcreateimage(cvsize(pimage-width, pimage-height), pimage-depth, pimage-nchannels);cvsmooth(pimage, pimage3, cv_gaussian, 3,3);cvsmooth(pimage, pimage5, cv_gaussian, 5,5);cvsmooth(pimage5, pimage5_2, cv_gaussian, 5,5);cvsmooth(pimage, pimage7, cv_gaussian,7,7);cvsmooth(pimage,

42、pimage9, cv_gaussian,9,9);cvsmooth(pimage, pimage11, cv_gaussian,11,11);cvshowimage(szwindowname0, pimage);cvshowimage(szwindowname1, pimage3);cvshowimage(szwindowname2, pimage5);cvshowimage(szwindowname3, pimage5_2);cvshowimage(szwindowname4, pimage7);cvshowimage(szwindowname5, pimage9);cvshowimage

43、(szwindowname6, pimage11);cvwaitkey(0);cvreleaseimage(&pimage);cvreleaseimage(&pimage3);cvreleaseimage(&pimage5);cvreleaseimage(&pimage5_2);cvreleaseimage(&pimage7);cvreleaseimage(&pimage9);cvreleaseimage(&pimage11);cvdestroyallwindows();void exercise3()iplimage* pimage = null, *pimage5 = null, *pimage9 = null, *pimage5_se

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论