《学习opencv》第四章课后习题1

本人自己做的:




#include "stdafx.h"
#include<opencv/cv.h>
#include<opencv/highgui.h>

int main()
{
 //a.1从视频中趣图并显示。
 CvCapture* capture = cvCreateFileCapture("C:\\test.avi");
    IplImage* frame= cvQueryFrame( capture );
    if( !frame ) return -1;
 cvNamedWindow("imagefromviedo", CV_WINDOW_AUTOSIZE );
    cvShowImage("imagefromviedo", frame );

 //a.2将图转换为灰度图
    IplImage* gray=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
    cvCvtColor(frame,gray,CV_BGR2GRAY);
 cvNamedWindow("convert2gray", CV_WINDOW_AUTOSIZE );
    cvShowImage("convert2gray", gray );

 //a.3对图像做canny边缘检测
    IplImage* toCanny=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
    cvCanny(gray,toCanny,50,150,3);
 cvNamedWindow("Canny",CV_WINDOW_AUTOSIZE);
    cvShowImage("Canny", toCanny);
  
 

 //b.三合一
 IplImage* merge=cvCreateImage(cvSize(3*frame->width,frame->height),IPL_DEPTH_8U,3);
    cvSetZero(merge);

 CvRect rect1=cvRect(0,0,frame->width,frame->height);
    cvSetImageROI(merge,rect1);
 frame->nChannels =3;
    cvCopy(frame,merge,NULL);
    cvResetImageROI(merge);

 CvRect rect2=cvRect(frame->width,0,frame->width,frame->height);
    cvSetImageROI(merge,rect2);
 gray->nChannels =3;
    cvCopy(gray,merge,NULL);
    cvResetImageROI(merge);

 CvRect rect3=cvRect(2*frame->width,0,frame->width,frame->height);
    cvSetImageROI(merge,rect3);
 toCanny->nChannels =3;
    cvCopy(toCanny,merge,NULL);
    cvResetImageROI(merge);

 //c.加标志
 CvFont textfont = cvFont(10.0,1);  
    cvInitFont(&textfont, CV_FONT_HERSHEY_SIMPLEX, 0.5f, 0.5f, 0, 1);  
    cvPutText(merge, "Frame", cvPoint(10,20), &textfont, cvScalar(0,0,255));  
    cvPutText(merge, "Gray", cvPoint(frame->width+10,20), &textfont, cvScalar(255,0,0));  
    cvPutText(merge, "toCanny", cvPoint(frame->width*2+10, 20), &textfont, cvScalar(0,255,0));  

 cvNamedWindow("merge", CV_WINDOW_AUTOSIZE );
    cvShowImage("merge", merge );

 cvWaitKey(0);

 

    

    cvDestroyWindow("merge");
 cvReleaseImage( &merge );
    
    cvReleaseCapture( &capture );
    cvDestroyWindow("imagefromviedo");
 cvReleaseImage( &frame );

 cvDestroyWindow("convert2gray");
 cvReleaseImage( &gray );
    
 
 cvDestroyWindow("Canny");
 cvReleaseImage( &toCanny );
    
 return 0;
 /*//a.1从视频中趣图并显示。
 CvCapture* capture = cvCreateFileCapture("c:\\test.avi");
    IplImage* frame= cvQueryFrame( capture );
    if( !frame ) return -1;
 cvNamedWindow("imagefromviedo", CV_WINDOW_AUTOSIZE );
    cvShowImage("imagefromviedo", frame );

 //a.2将图转换为灰度图
    IplImage* gray=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
 IplImage* image_gray=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);//用于合并显示的灰度图
    cvCvtColor(frame,gray,CV_BGR2GRAY);
 cvCvtColor(gray,image_gray,CV_GRAY2BGR);
 cvNamedWindow("convert2gray", CV_WINDOW_AUTOSIZE );
    cvShowImage("convert2gray", image_gray );

 //a.3对图像做canny边缘检测
    IplImage* toCanny=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,1);
 IplImage* image_toCanny=cvCreateImage(cvGetSize(frame),IPL_DEPTH_8U,3);//用于合并显示的Canny边缘检测图
 
    cvCanny(gray,toCanny,50,150,3);
 cvCvtColor(toCanny,image_toCanny,CV_GRAY2BGR);
 cvNamedWindow("Canny",CV_WINDOW_AUTOSIZE);
    cvShowImage("Canny",image_toCanny);
  
 

 //b.三合一
 IplImage* merge=cvCreateImage(cvSize(3*frame->width,frame->height),IPL_DEPTH_8U,3);
    cvSetZero(merge);

 CvRect rect1=cvRect(0,0,frame->width,frame->height);
    cvSetImageROI(merge,rect1);
    cvCopy(frame,merge,NULL);
    cvResetImageROI(merge);

 CvRect rect2=cvRect(frame->width,0,frame->width,frame->height);
    cvSetImageROI(merge,rect2);
    cvCopy(image_gray,merge,NULL);
    cvResetImageROI(merge);

 CvRect rect3=cvRect(2*frame->width,0,frame->width,frame->height);
    cvSetImageROI(merge,rect3);
    cvCopy(image_toCanny,merge,NULL);
    cvResetImageROI(merge);

 cvNamedWindow("merge");
 cvShowImage("merge",merge);

 cvWaitKey(0);

   
    cvReleaseCapture( &capture );
 cvReleaseImage( &frame );
    cvDestroyWindow("imagefromviedo");

 cvReleaseImage( &gray );
 cvReleaseImage( &image_gray );
    cvDestroyWindow("convert2gray");

 cvReleaseImage( &toCanny );
 cvReleaseImage( &image_toCanny );
    cvDestroyWindow("Canny");

 cvReleaseImage( &merge );
    cvDestroyWindow("merge");
 return 0;*/
}

 




 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值