本人自己做的:
#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;*/
}
《学习opencv》第四章课后习题1
最新推荐文章于 2022-06-22 21:51:40 发布