网上查了下,没能找到关于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;
}
![](https://i-blog.csdnimg.cn/blog_migrate/3bf23fac98bff3e90e544b39ae3117a2.png)