版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2024年人民版高二生物上冊(cè)階段測(cè)試試卷含答案
- 2025年仁愛科普版三年級(jí)英語上冊(cè)階段測(cè)試試卷
- 2025年人教版九年級(jí)物理上冊(cè)階段測(cè)試試卷
- 2024年滬科版高一生物上冊(cè)階段測(cè)試試卷含答案
- 2025年冀教新版八年級(jí)科學(xué)上冊(cè)月考試卷
- 2025年滬科版九年級(jí)化學(xué)下冊(cè)階段測(cè)試試卷
- 2024年滬教新版九年級(jí)生物上冊(cè)月考試卷含答案
- 2025年學(xué)校熱水系統(tǒng)節(jié)能改造與運(yùn)行維護(hù)合同3篇
- 2025年滬科版八年級(jí)數(shù)學(xué)下冊(cè)階段測(cè)試試卷
- 2025江西建筑安全員-C證考試題庫
- 喉血管瘤護(hù)理查房
- 斷裂力學(xué)課件
- 員工賠償金保密協(xié)議
- 空靈鼓社團(tuán)教學(xué)計(jì)劃
- (法律資料)城管執(zhí)法操作實(shí)務(wù)
- 廣東省珠海市2022-2023六年級(jí)上學(xué)期數(shù)學(xué)期末試卷(含答案)
- 山東省青島市第二中學(xué)2023-2024學(xué)年化學(xué)高二第一學(xué)期期末綜合測(cè)試試題含解析
- 網(wǎng)上商城 售后服務(wù)范本
- 軍隊(duì)文職專用簡歷(2023年)
- 人教版四年級(jí)話說溫州(表格式)
- 《安的種子》幼兒故事
評(píng)論
0/150
提交評(píng)論