![[计算机软件及应用]LearningOpenCV课后答案.doc_第1页](http://file.renrendoc.com/FileRoot1/2019-1/12/476cbede-41d1-423f-b1d7-50abddded905/476cbede-41d1-423f-b1d7-50abddded9051.gif)
![[计算机软件及应用]LearningOpenCV课后答案.doc_第2页](http://file.renrendoc.com/FileRoot1/2019-1/12/476cbede-41d1-423f-b1d7-50abddded905/476cbede-41d1-423f-b1d7-50abddded9052.gif)
![[计算机软件及应用]LearningOpenCV课后答案.doc_第3页](http://file.renrendoc.com/FileRoot1/2019-1/12/476cbede-41d1-423f-b1d7-50abddded905/476cbede-41d1-423f-b1d7-50abddded9053.gif)
![[计算机软件及应用]LearningOpenCV课后答案.doc_第4页](http://file.renrendoc.com/FileRoot1/2019-1/12/476cbede-41d1-423f-b1d7-50abddded905/476cbede-41d1-423f-b1d7-50abddded9054.gif)
![[计算机软件及应用]LearningOpenCV课后答案.doc_第5页](http://file.renrendoc.com/FileRoot1/2019-1/12/476cbede-41d1-423f-b1d7-50abddded905/476cbede-41d1-423f-b1d7-50abddded9055.gif)
已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025商业大厦租赁合同
- 2025借款合同成立的认定及责任承担
- 2025【合同范本】电子产品供应合同范本
- 2025物业租赁合同书样本
- 2024秋七年级英语下册 Module 6 Around town Unit 3 Language in use说课稿 (新版)外研版
- 2025水泥购销合同范本
- Module 7 Unit 3 说课稿 海南省海口市 2024-2025学年外研版九年级英语上册
- 2.3.2气体摩尔体积(讲义)-2024-2025学年高一化学同步教学教学设计+讲义(人教版2019必修第一册)
- 电池厂后勤保障管理制度
- 2025个体土地交易合同
- 2024年骑电动车撞伤人私了协议书范文
- 教科版四年级上册科学期末复习全册知识点总结
- 中建企业定额2023版
- 22G101三维彩色立体图集
- 《计算机网络实验教程》全套教学课件
- 人教版小学英语单词表(完整版)
- 间隔记录数据表
- 工抵房转让协议
- DB32T4064-2021江苏省城镇燃气安全检查标准
- JJG 162-2019饮用冷水水表
- 山西省煤矿安全生产管理人员培训考试题库(浓缩500题)
评论
0/150
提交评论