Learning OpenCV课后答案.docx
- 文档编号:7329471
- 上传时间:2023-01-23
- 格式:DOCX
- 页数:60
- 大小:29.24KB
Learning OpenCV课后答案.docx
《Learning OpenCV课后答案.docx》由会员分享,可在线阅读,更多相关《Learning OpenCV课后答案.docx(60页珍藏版)》请在冰豆网上搜索。
LearningOpenCV课后答案
TheLearningofOpenCV
Herecontainsjusttheexercisesinbook"LearningOpenCV"andofcourseIbelievethereisbettersolutionforthem.
problems:
1.IfyousetcvSetImageCOI,don'tforgettosetitbackwithparameter"0",otherwiseyoumayendupwithsinglechannel.
2.IfyougetyourIplImagefrom"cvCapture",thendoNOTreleaseitasitisnotcreatedbyyou!
3.Thereissomememoryallocationin"cvSubImageHeader,anddoNOTforgettoreleasethem.
4.Itriedtoerasemydrawinglinesby"xor"itagain,butitseemsnotworkingbyusing"cvGetRow".MaybeitisbecauseImessupwith"cvSetImageROIwithoutresetit.So,IjustusecvCopyto
overwriteit.
5.Whenyoudrawyourrectangleofsomewidthofline,becarefulthattherectangleisbiggerthanthatwidthofline.
6.Thecolorsequenceinbytesorderis"BGR".
Thefollowingareexercisesinchapter4.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include
#include
#include
#include
#include
#include
#include
#include
#pragmacomment(lib,"cvd.lib")
#pragmacomment(lib,"cxcored.lib")
#pragmacomment(lib,"highguid.lib")
CvFontmyfont=cvFont(1,2);
char*szCombineName="combineimage";
boolbMouseButtonDown=false;
intx=0,y=0;
voiddoDrawing(IplImage*pImage)
{
charbuffer[256];
intwinX=0,winY=0;
if(bMouseButtonDown)
{
winX=x;
winY=y;
sprintf(buffer,"mouseclickat[%d,%d]",x,y);
cvPutText(pImage,buffer,cvPoint(winX,winY),&myfont,cvScalar(255,0,0));
}
}
voidmyMouseCallback(intevent,intmyx,intmyy,intflag,void*pUser)
{
switch(event)
{
caseCV_EVENT_LBUTTONDOWN:
bMouseButtonDown=true;
x=myx;
y=myy;
break;
caseCV_EVENT_LBUTTONUP:
bMouseButtonDown=false;
break;
}
}
voidexercise1()
{
char*szVideoName="e:
\\mytest.avi";
char*szWindowName="myvideo";
char*szGrayName="grayimage";
char*szCannyName="cannyimage";
CvCapture*pCapture=NULL;
IplImage*pImage=NULL;
IplImage*pGrayImg=NULL;
IplImage*pCannyImg=NULL;
IplImage*pCombineImg=NULL;
IplImage*pSub1=NULL;
IplImage*pSub2=NULL;
IplImage*pSub3=NULL;
intnWidth=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);
do
{
if(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);
//doNOTreleasepImagebecauseitisonlyretrievedfromcapture
//cvReleaseImage(&pImage);
cvReleaseImageHeader(&pSub1);
cvReleaseImageHeader(&pSub2);
cvReleaseImageHeader(&pSub3);
cvReleaseImage(&pGrayImg);
cvReleaseImage(&pCannyImg);
cvReleaseImage(&pCombineImg);
cvReleaseCapture(&pCapture);
}
cvDestroyAllWindows();
}
voidmyMouseOnClick(intevent,intx,inty,intflag,void*pUser)
{
charbuffer[256];
CvFontmyFont=cvFont(2,2);
IplImage*pImage=(IplImage*)pUser;
char*ptr=NULL;
unsignedcharr=0,g=0,b=0;
switch(event)
{
caseCV_EVENT_LBUTTONDOWN:
ptr=pImage->imageData+y*pImage->widthStep+x*pImage->depth*pImage->nChannels/8;
r=ptr[0];
g=ptr[1];
b=ptr[2];
sprintf(buffer,"rgb[%d,%d,%d]",r,g,b);
cvPutText(pImage,buffer,cvPoint(x,y),&myFont,cvScalar(255,0,0));
break;
}
}
voidexercise2()
{
IplImage*pImage=NULL;//,*pCloneImage=NULL;
pImage=cvLoadImage("mytest.jpg");
//pCloneImage=cvCloneImage(pImage);
cvNamedWindow("mytest");
cvSetMouseCallback("mytest",myMouseOnClick,pImage);
do
{
cvShowImage("mytest",pImage);
if(cvWaitKey(1000)==27)
{
break;
}
}
while(true);
cvReleaseImage(&pImage);
cvDestroyWindow("mytest");
}
#defineHistogramWidth48
#defineHistogramHeight800
IplImage*pImage=NULL,*pCloneImage=NULL,*pHistImage=NULL;
CvRectrect=cvRect(0,0,0,0);
boolbStart=false;
#if0
voideraseFrame()
{
CvMatvecSrc,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);
}
#else
voideraseFrame()
{
cvSetImageROI(pImage,rect);
cvSetImageROI(pCloneImage,rect);
cvCopy(pCloneImage,pImage);
cvResetImageROI(pImage);
cvResetImageROI(pCloneImage);
}
#endif
#defineLINE_WIDTH1
voiddrawFrame()
{
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);
}
voiddrawHistogram()
{
char*ptr=NULL,*pRow=NULL,*pCol=NULL;
charbuffer[32];
inthist[3][8]={0};
intindex=0;
intnPixWidth=0;
intr,c,i,nOffset=0;
intx=0,y=0;
CvFontmyFont=cvFont(1,1);
intnMax=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 { pCol=pRow; for(c=0;c { for(i=0;i<3;i++) { index=pCol[i]/32; hist[i][index]++; if(hist[i][index]>nMax) { nMax=hist[i][index]; } } 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++) { unsignedcharcolor[3]={0}; x=nOffset; color[i]=255; if(hist[i][c]/nScale! =0) { cvSetImageROI(pHistImage,cvRect(x+i*HistogramWidth,y-hist[i][c]/nScale,HistogramWidth,hist[i][c]/nScale)); cvSet(pHistImage,cvScalar(color[0],color[1],color[2])); cvResetImageROI(pHistImage); } sprintf(buffer,"%d",hist[i][c]); cvPutText(pHistImage,buffer,cvPoint(x+i*HistogramWidth,y-100),&myFont,cvScalarAll(0)); } nOffset+=HistogramWidth*3; } cvShowImage("histogram",pHistImage); } voidmyMouseCallback3(intevent,intx,inty,intflag,void*pUser) { intw=0,h=0; switch(event) { caseCV_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; caseCV_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; caseCV_EVENT_LBUTTONUP: if(rect.width>0&&rect.height>0) { cvSetImageROI(pImage,rect); cvSe
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Learning OpenCV课后答案 OpenCV 课后 答案
![提示](https://static.bdocx.com/images/bang_tan.gif)