![[計算機軟件及應用]LearningOpenCV課后答案_第1頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/11/69af3f56-2b34-498a-9630-f97dccb073ed/69af3f56-2b34-498a-9630-f97dccb073ed1.gif)
![[計算機軟件及應用]LearningOpenCV課后答案_第2頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/11/69af3f56-2b34-498a-9630-f97dccb073ed/69af3f56-2b34-498a-9630-f97dccb073ed2.gif)
![[計算機軟件及應用]LearningOpenCV課后答案_第3頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/11/69af3f56-2b34-498a-9630-f97dccb073ed/69af3f56-2b34-498a-9630-f97dccb073ed3.gif)
![[計算機軟件及應用]LearningOpenCV課后答案_第4頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/11/69af3f56-2b34-498a-9630-f97dccb073ed/69af3f56-2b34-498a-9630-f97dccb073ed4.gif)
![[計算機軟件及應用]LearningOpenCV課后答案_第5頁](http://file2.renrendoc.com/fileroot_temp3/2021-7/11/69af3f56-2b34-498a-9630-f97dccb073ed/69af3f56-2b34-498a-9630-f97dccb073ed5.gif)
版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 灶具項目安全評估報告
- 2025-2030年中國油潤滑裝置項目投資可行性研究分析報告
- 中國船舶控制電器行業(yè)市場發(fā)展前景及發(fā)展趨勢與投資戰(zhàn)略研究報告(2024-2030)
- 2025年中國圓角不銹鋼化妝箱行業(yè)市場發(fā)展前景及發(fā)展趨勢與投資戰(zhàn)略研究報告
- 房地產市場報告 -2025年二季度沈陽寫字樓和零售物業(yè)市場報告
- 菏澤市城市房屋拆遷管理辦法
- 蒙城市投資項目管理辦法
- 虹口區(qū)鍋爐監(jiān)測管理辦法
- 醫(yī)療健康管理課件
- 西安市公司投資管理辦法
- 高考語文備考之考場中的韓愈詩歌文言文匯編
- 學校廚房設備投標方案(技術標)
- 養(yǎng)老院護理服務質量提升手冊
- 一例下肢靜脈血栓疑難病例護理討論
- DB37T 5281-2024 地源熱泵系統(tǒng)工程技術規(guī)程
- 物聯(lián)網行業(yè)技術崗位總結
- 康復心理學與心理干預
- LED照明有關國家標準及對應國際標準
- 文創(chuàng)產品定制合同范本
- 科普版四年級上冊英語全冊同步練習
- 2024年巴西血液透析膜市場機會及渠道調研報告
評論
0/150
提交評論