欢迎来到装配图网! | 帮助中心 装配图网zhuangpeitu.com!
装配图网
ImageVerifierCode 换一换
首页 装配图网 > 资源分类 > DOC文档下载
 

LearningOpenCV课后答案

  • 资源ID:144064619       资源大小:155.02KB        全文页数:53页
  • 资源格式: DOC        下载积分:9.9积分
快捷下载 游客一键下载
会员登录下载
微信登录下载
三方登录下载: 微信开放平台登录 支付宝登录   QQ登录   微博登录  
二维码
微信扫一扫登录
下载资源需要9.9积分
邮箱/手机:
温馨提示:
用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)
支付方式: 支付宝    微信支付   
验证码:   换一换

 
账号:
密码:
验证码:   换一换
  忘记密码?
    
友情提示
2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

LearningOpenCV课后答案

The Learning of OpenCVHere contains just the exercises in book "Learning OpenCV" and of course I believe there is better solution for them.problems: 1. If you set cvSetImageCOI, don't 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 <cv.h>#include <highgui.h>#include <stdio.h>#include <math.h>#include <string.h>#include <windows.h>#include <tchar.h>#include <time.h>#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 < 3; i +)index = pColi / 32;histiindex +;if (histiindex > 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 < 3; i +)unsigned char color3=0;x = nOffset;colori = 255;if (histic / nScale != 0)cvSetImageROI(pHistImage, cvRect(x+i*HistogramWidth, y - histic/nScale, HistogramWidth, histic/nScale);cvSet(pHistImage, cvScalar(color0, color1, color2);cvResetImageROI(pHistImage);sprintf(buffer, "%d", histic);cvPutText(pHistImage, buffer, cvPoint(x+i*HistogramWidth, y-100), &myFont, cvScalarAll(0);nOffset += HistogramWidth*3;cvShowImage("histogram", pHistImage);void myMouseCallback3(int event, int x, int y, int flag, void* pUser)int w = 0, h = 0;switch (event)case CV_EVENT_MOUSEMOVE:if (!bStart)break;w = x - rect.x;h = y - rect.y;if (w > 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 don't 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 don't 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 <cv.h>#include <highgui.h>#include <stdio.h>#include <math.h>#include <string.h>#include <windows.h>#include <tchar.h>#include <time.h>#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 <cv.h>#include <highgui.h>#include <stdio.h>#include <math.h>#include <string.h>#include <windows.h>#include <tchar.h>#include <time.h>#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 < sizeof(szWindowName)/sizeof(char*); i +)cvNamedWindow(szWindowNamei);pImage = cvLoadImage("mytest.jpg");if (pImage)pSimpleImg = cvCreateImage(cvSize(pImage->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 < sizeof(szWindowName)/sizeof(char*); i +)cvNamedWindow(szWindowNamei);pImage = cvLoadImage("mytest.jpg");if (pImage)pImage3 = cvCreateImage(cvSize(pImage->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_second = NULL;char* ptr = NULL;int i = 0;char* szWindowName = "original", "5x5", "9x9", "5x5 second"pImage = cvCreateImage(cvSize(100, 100), 8, 1);pImage5 = cvCreateImage(cvSize(100, 100), 8, 1);pImage5_second = cvCreateImage(cvSize(100, 100), 8, 1);pImage9 = cvCreateImage(cvSize(100, 100), 8, 1);cvSetZero(pImage);cvSetZero(pImage5);cvSetZero(pImage9);ptr = pImage->imageData + pImage->widthStep * 49 + 50 * pImage->depth * pImage->nChannels/8;*ptr = 255;for (i = 0; i < sizeof(szWindowName)/sizeof(char*); i +)cvNamedWindow(szWindowNamei);cvSmooth(pImage, pImage5, CV_GAUSSIAN, 5,5);cvSmooth(pImage5, pImage5_second, CV_GAUSSIAN, 5,5);cvSmooth(pImage, pImage9, CV_GAUSSIAN, 9,9);/cvSmooth(pImage, pImage5);/cvSmooth(pImage, pImage9);/cvSmooth(pImage5, pImage5_second);cvMoveWindow(szWindowName1, 100, 200);cvShowImage(szWindowName0, pImage);cvShowImage(szWindowName1, pImage5);cvShowImage(szWindowName2, pImage9);cvShowImage(szWindowName3, pImage5_second);cvWaitKey(0);cvReleaseImage(&pImage);cvReleaseImage(&pImage5);cvReleaseImage(&pImage9);cvReleaseImage(&pImage5_second);cvDestroyAllWindows();void exercise4()IplImage* pImage8= NULL;int i = 0;double fSize3 = 1,4,6;char* szWindowName = "original", "9-9-x", "0-0-x", "0-0-1-9", "0-0-9-1", "0-0-=>1-9=>9-1", "0-0-9-9","9-9-0-0"for (i = 0; i < sizeof(szWindowName)/sizeof(char*); i +)if (i = 0)pImage0 = cvLoadImage("mytest.jpg");elsepImagei = cvCreateImage(cvSize(pImage0->width, pImage0->height), pImage0->depth, pImage0->nChannels);cvNamedWindow(szWindowNamei);for (i = 0; i < 3; i +)cvSmooth(pImage0, pImage1, CV_GAUSSIAN, 9,9,fSizei);cvSmooth(pImage0, pImage2, CV_GAUSSIAN, 0,0,fSizei);cvShowImage(szWindowName0, pImage0);cvShowImage(szWindowName1, pImage1);cvShowImage(szWindowName2, pImage2);cvWaitKey(0);/ c.cvSmooth(pImage0, pImage3, CV_GAUSSIAN, 0,0,1,9);cvShowImage(szWindowName3, pImage3);/ d.cvSmooth(pImage0, pImage4, CV_GAUSSIAN, 0,0,9,1);cvShowImage(szWindowName4, pImage4);/ ecvSmooth(pImage3, pImage5, CV_GAUSSIAN, 0,0,9,1);cvShowImage(szWindowName5, pImage5);/ f.cvSmooth(pImage3, pImage6, CV_GAUSSIAN, 0,0,9,9);cvShowImage(szWindowName6, pImage6);cvSmooth(pImage3, pImage7, CV_GAUSSIAN, 9,9,0,0);cvShowImage(szWindowName7, pImage7);cvShowImage(szWindowName0, pImage0);/cvWaitKey(0);for (i = 0; i < sizeof(szWindowName)/sizeof(char*); i +)cvReleaseImage(&pImagei);cvDestroyAllWindows();int APIENTRY Wi

注意事项

本文(LearningOpenCV课后答案)为本站会员(tia****g98)主动上传,装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知装配图网(点击联系客服),我们立即给予删除!

温馨提示:如果因为网速或其他原因下载失败请重新下载,重复下载不扣分。




关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!