已阅读5页,还剩48页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 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 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 #include #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 = 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 = 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;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(szvideoname);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 = cvcreateimage(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 = pcombineimg-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*pcombineimg-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);/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, cvscalar(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 release 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 mymouseonclick(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-nchannels/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);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 = 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, &vecdst, 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);#elsevoid 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), cvscalarall(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 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)nmax = 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.width 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)cvsetimageroi(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(pimage, 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(cvsize(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)exercise3();/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 there 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 comment(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(pcapture, 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()pcapture = 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);cvdestroywindow(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 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-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), 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(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);cvshowimage(szwindowname5, pbilateralimg);cvwaitkey(0);cvreleaseimage(&pimage);cvreleaseimage(&psimpleimg);cvreleaseimage(&pnoscaleimg);cvreleaseimage(&pmedianimg);cvreleaseimage(&pgaussianimg);cvreleaseimage(&pbilateralimg);cvdestroyallwindows();void exercise2()char* szwindowname = original, 3x3, 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), pimage-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-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, 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(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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 化工实验设备项目可行性分析报告范文(总投资14000万元)
- 2026届四川省西昌市高一上化学期中综合测试模拟试题含解析
- 2026届新疆维吾尔自治区乌鲁木齐市新市区第七十中学生物高一第一学期期末联考模拟试题含解析
- 广东省深圳市康桥书院2025-2026学年物理高一上期末经典模拟试题含解析
- 陕西省西安市第25中学2026届高一上生物期末联考模拟试题含解析
- 桂林航天工业学院《医学沟通学》2024-2025学年第一学期期末试卷
- 法律服务市场需求与趋势分析
- 2025年眼科视力检测技能考试题及答案
- 2025年药品知识单招题库及答案
- 2025年玉林理科二模试卷及答案
- 树立正确就业观课件
- 钢厂采购员设备管理制度
- 交流轮岗挂职管理办法
- 2025年社区工作者考试真题库附答案
- 亚投行贷款项目管理办法
- 全国大学生职业规划大赛《电子信息工程》专业生涯发展展示
- 学校意识形态领域风险防控机制意识形态领域风险
- 严重精神障碍患者家庭护理-培训课件
- 承插型盘扣式钢管脚手架体设备设施风险分级管控清单
- 脑出血患者营养治疗讲课件
- 十五五学校发展规划中期自评报告
评论
0/150
提交评论