OpenCV 4.1 b and 4.1.c

来源:互联网 发布:跳跳软件官方下载 编辑:程序博客网 时间:2024/06/03 10:36

chap 4.1 b and 4.1.c

#include<opencv2/opencv.hpp>#include<iostream>// chap 4.1.b and c// gray and canny in one big picturevoid main(){using namespace std;CvCapture* capture=cvCreateFileCapture("D:\\video\\case.mp4");IplImage* frame=NULL;IplImage* temp2=NULL;IplImage* dst=NULL;IplImage* temp1=NULL;IplImage* gray=NULL;IplImage* canny_pic=NULL;while(capture){frame=cvQueryFrame(capture);if(frame==NULL){cout<<"frame error!"<<endl;// display the frame errorbreak;}gray=cvCreateImage(cvGetSize(frame),8,1);// gray pic 8UC1canny_pic=cvCreateImage(cvGetSize(frame),8,1);// canny_pic  8UC1cvCvtColor(frame,gray,CV_BGR2GRAY);// pay attention to BGR2GRAY ( not RGB2GRAY )cvCanny(gray,canny_pic,100,200,3);// the last parameter must be 3..dst=cvCreateImage(cvSize(frame->width*2,frame->height),8,1); // 行排列 dst =( 2*width, height )temp1=cvCreateImage(cvGetSize(frame),8,1); // temp1 指针temp2=cvCreateImage(cvGetSize(frame),8,1); // temp2 指针temp1->widthStep=dst->widthStep;// important to rowtemp2->widthStep=dst->widthStep;temp1->origin=dst->origin; // it's ok to neglecttemp2->origin=dst->origin;temp1->imageData=dst->imageData; // temp1 指向 dst 的开始cvCopy(gray,temp1); // 灰度图temp2->imageData=dst->imageData+dst->width/2;// temp2 指向 dst 宽度一半处    cvCopy(canny_pic,temp2); // 边缘图//------------- PutText() pp:96----------------------//CvFont font=cvFont(20.0,1);cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX,1,1,0,1,8);cvPutText(dst,"huohuxingxing",cvPoint(50,50),&font,cvScalar(0,0,0)); // 黑色cvPutText(dst,"huohuxingxing",cvPoint(frame->width+50,50),&font,cvScalar(255,255,255)); // 白色cvNamedWindow("dst");cvShowImage("dst",dst);if(cvWaitKey(30)>0)  // not cvWaitKey(0);break;   // any keys pushed down, exit the loop}cvReleaseCapture(&capture);// Don't  releaseImage(&frame)cvReleaseImage(&gray);cvReleaseImage(&canny_pic);cvReleaseImage(&temp1);cvReleaseImage(&temp2);cvReleaseImage(&dst);}


按照习题提示,temp 1 指向 dst 的开始,拷贝gray 图; temp2 指向 dst + width / 2 处 , 拷贝 canny_pic 图。

这由 temp1-> imageData =  dst -> imageData ; 等实现。

由于 gray and canny_pic 都是8UC1 , dst 也是8UC1,其大小为 width*2, height 。

类似,dst 为8UC3时,需要将 temp1 and temp2 修改成C3图, 拷贝操作改为 cvCvt( gray, temp1,CV_GRAY2BGR) 。

字体显示不需多讲,看教程96页。

程序中的 temp 1 -> widthStep = dst -> widthStep ;  等,不可省。

省略显示结果不对。

而 temp 1 -> origin = dst -> origin ; 等 可以省略。

因为默认 origin 都是左上角。

 

思考:

如果要是将2副图排成一列显示,如何操作?

我记得之前,只需要修改2个地方:

 1,dst 图大小 修改为( width, height* 2) 

2,temp 2 指向位置:

temp2 - > imageData = dst - > imageData + dst -> height / 2; //  ( 2 副图像)

原来好像这样就是正确的,刚才调试了一下,结果不对,一时想不起来错哪里了。

有正确的意见,请留言。相互交流。。