openCV3书中练习题8-1个人解答
#include<opencv.hpp>
#include<highgui/highgui.hpp>
using namespace cv;
int main()
{
//8-1 1(1)从视频中读取图像帧(2)将结果转化为灰度图像(3)对该图像完成边沿检测
//a.在同一窗口显示单个步骤的结果
namedWindow("dog", CV_WINDOW_AUTOSIZE);
VideoCapture cap;
cap.open("C:/Users/limimi/Desktop/learning/OpenCV/Learning-OpenCV-3_examples-master/dog.mp4");
Mat img, Gra1,Gra, Cay;
if (cap.isOpened())
{
//获取原视频的高度和宽度
int tmpw = (int)cap.get(cv::CAP_PROP_FRAME_WIDTH);//cols
int tmph = (int)cap.get(cv::CAP_PROP_FRAME_HEIGHT);//rows
//创建一个图像,大小是原视频帧的三倍
Mat Three2one(tmph,tmpw*3,CV_8UC3);
while(1)
{
cap.read(img);
cvtColor(img, Gra1,COLOR_RGB2GRAY);
cvtColor(Gra1,Gra,COLOR_GRAY2BGR);//把单通道的灰度图还原为三通道,不这样做在拷贝的时候会遇到问题
Canny(Gra,Cay,40,60);//边缘检测得到的图像也是单通道的
cvtColor(Cay,Cay, COLOR_GRAY2BGR);
resize(Gra,Gra,Size(tmpw,tmph));
img.copyTo(Three2one(Rect(0, 0, tmpw, tmph)));
Gra.copyTo(Three2one(Rect(tmpw, 0, tmpw, tmph)));
Cay.copyTo(Three2one(Rect(tmpw * 2, 0, tmpw, tmph)));
imshow("dog",Three2one);
waitKey(100);
}
}
else
{
std::cout << "打开文件失败" << std::endl;
}
return 0;
}
|