【opencv课后练习】-- 第二章节

本文通过几个实践案例展示了如何利用OpenCV进行视频处理,包括视频捕捉、存储、图像缩放及动态显示等操作,并介绍了如何使用滚动条实现动态缩放比例调整。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

      网上查了下,没能找到关于learning opencv的课后习题讲解,现张贴出自我学习过程的code,与广大Coder共同探讨学习,多多指出小弟的错误,谢谢。

题目:使用例2-10中的视频捕捉和存储方法,结合例2-5中的doPyrDown()创建一个程序,使其从摄像机读入视频数据并将缩放变换后的彩色图像存入磁盘。

/*
	使用例2-10中的视频捕捉和存储方法,结合例2-5中的doPyrDown()创建一个程序,
	使其从摄像机读入视频数据并将缩放变换后的彩色图像存入磁盘
	chapter2-3
*/
#include <opencv2/core/core.hpp>                  
#include <opencv2/highgui/highgui.hpp>                  
#include <opencv2/imgproc/imgproc.hpp>   
#include <cv.h>  
#include <iostream>                
using namespace std;  
using namespace cv;
IplImage *doPyrDown(IplImage * in)
{
	IplImage* out = cvCreateImage(
		cvSize(in->width/2,in->height/2),
		in->depth,
		in->nChannels
		);
	//借用下opencv1 中的cvPyrDown函数修改frame尺寸
	cvPyrDown(in,out,CV_GAUSSIAN_5x5);
	return out;
}

int main (int argc,char* argv[])
{
	
	CvCapture* capture = cvCreateFileCapture("D:\\123.avi");
	if(!capture)
	{
		cout<<"Fail to open!"<<endl;
		return -1;
	}
	IplImage* bgr_frame = cvQueryFrame(capture);
	double fps = cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
	CvSize size = cvSize(
		(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH)/2,
		(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT)/2
		);
	CvVideoWriter *writer = cvCreateVideoWriter(
		"2.avi",
		CV_FOURCC('M','J','P','G'),
		fps,
		size
		);
	IplImage * logpolar_frame = cvCreateImage(
		size,
		IPL_DEPTH_8U,
		3
		);
	while( (bgr_frame = cvQueryFrame(capture)) != NULL )
	{
		IplImage* out =  doPyrDown(bgr_frame);
		cvWriteFrame(writer,out);
	}
	cvReleaseVideoWriter(&writer);
	cvReleaseImage(&logpolar_frame);
	cvReleaseCapture(&capture);
	return 0;
	
}

题目4:修改练习3的代码,结合例2-1,将变换结果显示在窗口中。

/*
	修改练习3的代码,结合例2-1,将变换结果显示在窗口中。
	chapter2-4
*/
#include <opencv2/core/core.hpp>                  
#include <opencv2/highgui/highgui.hpp>                  
#include <opencv2/imgproc/imgproc.hpp>   
#include <cv.h>  
#include <iostream>                
using namespace std;  
using namespace cv;
IplImage *doPyrDown(IplImage * in,int filter = IPL_GAUSSIAN_5x5)
{
	IplImage* out = cvCreateImage(
		cvSize((int)(in->width/2),(int)(in->height/2)),
		in->depth,
		in->nChannels
		);
	//借用下opencv1 中的cvPyrDown函数修改frame尺寸
	cvPyrDown(in,out,CV_GAUSSIAN_5x5);
	return out;
}

int main (int argc,char* argv[])
{
	
	CvCapture* capture = cvCreateFileCapture("D:\\123.avi");
	if(!capture)
	{
		cout<<"Fail to open!"<<endl;
		return -1;
	}
	IplImage* bgr_frame = cvQueryFrame(capture);
	double fps = cvGetCaptureProperty(capture,CV_CAP_PROP_FPS);
	CvSize size = cvSize(
		(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH)/2,
		(int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT)/2
		);
	CvVideoWriter *writer = cvCreateVideoWriter(
		"3.avi",
		CV_FOURCC('M','J','P','G'),
		fps,
		size
		);
	IplImage * logpolar_frame = cvCreateImage(
		size,
		IPL_DEPTH_8U,
		3
		);

	while( (bgr_frame = cvQueryFrame(capture)) != NULL )
	{
		IplImage* out =  doPyrDown(bgr_frame);
		cvWriteFrame(writer,out);
	}
	cvReleaseVideoWriter(&writer);
	cvReleaseImage(&logpolar_frame);
	cvReleaseCapture(&capture);
	cvNamedWindow("src",CV_WINDOW_AUTOSIZE);
	cvNamedWindow("dis");
	CvCapture* capture_dis = cvCreateFileCapture("2.avi");
	CvCapture* capture_src = cvCreateFileCapture("D:\\123.avi");
	while(true)
	{
		IplImage* src = cvQueryFrame(capture_src);
		IplImage* dis = cvQueryFrame(capture_dis);
		if(src!=NULL && dis != NULL)
		{
			cvShowImage("src",src);
			cvShowImage("dis",dis);
		}
		else 
		{
			break;
		}

	}
	cvWaitKey(0);

	return 0;
	
}


题目五:对于练习4种的代码进行修改,参考2-3,给程序添加滚动条,可以动态缩放比例。采用摄像头动态显示

/*
	对于练习4种的代码进行修改,参考2-3,给程序添加滚动条,可以动态缩放比例
	chapter2-5
*/
#include <opencv2/core/core.hpp>                  
#include <opencv2/highgui/highgui.hpp>                  
#include <opencv2/imgproc/imgproc.hpp>   
#include <cv.h>  
#include <iostream>                
using namespace std;  
using namespace cv;
int g_slider_position = 0;
////缩放比例
int g_scaling = 2;    
CvCapture* g_capture = NULL;
IplImage *doPryDown(IplImage * in,int filter = IPL_GAUSSIAN_5x5)
{
	IplImage* out = cvCreateImage(
		cvSize((int)(in->width/2),(int)(in->height/2)),
		in->depth,
		in->nChannels
		);
	//借用下opencv1 中的cvPyrDown函数修改frame尺寸
	cvPyrDown(in,out,CV_GAUSSIAN_5x5);
	return out;
}
//视频播放控制 
void onTrackbarSlide(int pos)
{
    // 改变缩放比例
     g_scaling = pos + 2;
}
int main()
{
	VideoCapture cap(0);
	Mat img;
	IplImage* dis;
	cvNamedWindow("src");
	//创建滚动条
	cvCreateTrackbar("scaling","src", &g_slider_position, 6, onTrackbarSlide);
	while(cap.read(img))
	{
		if(!img.empty())
		{
			IplImage frame = IplImage(img);
			dis = doPryDown(&frame);
			for(int i = 2; i < g_scaling; i++)
			{
				dis = doPryDown( dis);
			}
			cvShowImage("src", dis);
			char c = cvWaitKey(50);
			if (c == 27)
			{
				break;
			}
		}

	}
	cvReleaseImage( &dis);
	cvDestroyWindow("src");
	return 0;
}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值