opencv::将两幅图像合并后,在同一个窗口显示;并将合并的图像流保存成视频文件
/**
* @file main-opencv.cpp
* @date July 2014
* @brief An exemplative main file for the use of ViBe and OpenCV
*/
//#include <opencv2\core\core.hpp>
#include "vibe-background-sequential.h" using namespace cv;
using namespace std; const int minArea = ; // 舍去面积极小的方框
const double maxAreaRatio = 0.1; // 舍去面积极大的方框
const double boxRatio = 0.1; // 舍去长宽比严重失衡的方框 /**
* Displays instructions on how to use this program.
*/
void help()
{
cout
<< "--------------------------------------------------------------------------" << endl
<< "This program shows how to use ViBe with OpenCV " << endl
<< "Usage:" << endl
<< "./main-opencv <video filename>" << endl
<< "for example: ./main-opencv video.avi" << endl
<< "--------------------------------------------------------------------------" << endl
<< endl;
} void processVideo(char* videoFilename); void contour(const Mat &mor, Mat &img); /**
* Main program. It shows how to use the grayscale version (C1R) and the RGB version (C3R).
*/
int main(int argc, char* argv[])
{
/* Print help information. */
help(); /* Check for the input parameter correctness. */
/* if (argc != 2) {
cerr <<"Incorrect input" << endl;
cerr <<"exiting..." << endl;
return EXIT_FAILURE;
} /* Create GUI windows. */
//namedWindow("Frame");
//namedWindow("Segmentation by ViBe"); processVideo("framelink_yd.avi"); //读取 framelink_yd.avi 视频进行处理
/* Destroy GUI windows. */
destroyAllWindows();
return EXIT_SUCCESS;
} /**
* Processes the video. The code of ViBe is included here.
*
* @param videoFilename The name of the input video file.
*/
void processVideo(char* videoFilename)
{
VideoCapture capture(videoFilename); /* Create the capture object. */
if (!capture.isOpened()) {
cerr << "Unable to open video file: " << videoFilename<< endl; /* Error in opening the video input. */
exit(EXIT_FAILURE);
} clock_t start, finish;
double total;
start=clock(); /* Variables. */
static int frameNumber = ; /* The current frame number */
int lastCount = ;
int probFactor = gradient_Factor; /*概率因子,用梯度因子初始化概率因子*/
Mat frame,frame1; /* Current frame. */
Mat segmentationMap; /* Will contain the segmentation map. This is the binary output map. */
int keyboard = ; /* Input from keyboard. Used to stop the program. Enter 'q' to quit. */
char fileName[] = { };
int sampleCounts[] = {};
int speSamples[] = {};
vibeModel_Sequential_t *model = NULL; /* Model used by ViBe. */ int heig = ;
int widt = ;
Mat res = Mat::zeros(heig, widt, CV_8UC3); //res为三通道像素帧,用来保存最后合并的图像 /* 创建保存视频的文件名并打开 */
const string Name = "res.avi";
VideoWriter writer;
Size sz(widt, heig);
writer.open(Name, CV_FOURCC('M', 'J', 'P', 'G'), , sz, true);
while ((char)keyboard != 'q' && (char)keyboard != ) { /* Read input data. ESC or 'q' for quitting. */
if (!capture.read(frame1)) { /* Read the current frame. */
cerr << "Unable to read next frame." << endl;
cerr << "Exiting..." << endl;
break;// exit(EXIT_FAILURE);
} /* Applying ViBe.
* If you want to use the grayscale version of ViBe (which is much faster!):
* (1) remplace C3R by C1R in this file.
* (2) uncomment the next line (cvtColor).
*/
cvtColor(frame1, frame, CV_BGR2GRAY); //将三通道图像帧转换为单通道
if ( frameNumber== ) {
segmentationMap = Mat(frame.rows, frame.cols, CV_8UC1);
model = (vibeModel_Sequential_t*)libvibeModel_Sequential_New();
libvibeModel_Sequential_AllocInit_8u_C1R(model, frame.data, frame.cols, frame.rows);
class_samples(model,frame.data, sampleCounts,speSamples,frame.cols, frame.rows);
} /* ViBe: Segmentation and updating. */
libvibeModel_Sequential_Segmentation_8u_C1R(model, frame.data, segmentationMap.data);
libvibeModel_Sequential_Update_8u_C1R(model, frame.data, segmentationMap.data, &probFactor,frameNumber); medianBlur(segmentationMap, segmentationMap, ); /* 3x3 median filtering */
contour(segmentationMap, frame1); //在原图上框出动目标,至此图像处理完毕 //sprintf(fileName, "results55_2/%06d.jpg",frameNumber);
//imwrite(fileName, frame1); Mat segmentationMap1; //新定义一个帧变量,不能 cvtColor(segmentationMap, segmentationMap, CV_GRAY2BGR),因为 segmentationMap 为单通道帧;
cvtColor(segmentationMap, segmentationMap1, CV_GRAY2BGR); //将单通道帧图像 segmentationMap 转化为三通道segmentationMap1,因为 res 帧为三通道帧图像
// segmentationMap1要合并在 res 中;
/* Shows the current frame and the segmentation map. */
//imshow("Frame", frame1);
//imshow("Segmentation by ViBe", segmentationMap); /* 将 segmentationMap1 和 frame1 合并成一帧存放在 res 中*/
segmentationMap1.copyTo(res(Rect(, , , )));
frame1.copyTo(res(Rect(, , , )));
imshow("res", res); //显示合并后的图像 /* 将 res 写入打开的视频文件中 */
writer << res; //将合并图像写入,连续的图像帧保存为视频文件;
++frameNumber;
keyboard = waitKey(); /* Gets the input from the keyboard. */
} finish=clock();
total=(double)(finish-start);
cout<<total<<endl;
cout<<frameNumber<<endl;
capture.release(); /* Delete capture object. */
libvibeModel_Sequential_Free(model); /* Frees the model. */
} /* 框出运动目标 */
void contour(const Mat &mor, Mat &img)
{
int img_size = img.cols * img.rows;
Mat tmp = (mor == );
// Each detected contour is stored as a vector of points
vector<vector<Point> > contours;
vector<Vec4i> hierarchy; // containing information about the image topology
findContours(tmp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);
for (size_t k = ; k < contours.size(); k++) {
Rect rect = boundingRect(contours[k]);
double whratio = double(rect.width) / double(rect.height);
double hwratio = double(rect.height) / double(rect.width);
double ratio = min(hwratio, whratio);
double area = rect.area();
//框出符合条件的轮廓,舍去: 面积很小的, 面积很大的, 长宽比严重失调的
if (area > minArea && area < img_size*maxAreaRatio && ratio > boxRatio) {
int w = rect.width;
int h = rect.height; if(rect.width < )
rect.width = ;
/*
if(rect.width < 6)
rect.width = 2*rect.width;
else
rect.width = rect.width + rect.width/2;
*/
if(rect.height < )
rect.height = ;
/*
if(rect.height < 6)
rect.height = 2*rect.height;
else
rect.height = rect.height + rect.height/2;
*/
int w_add = (rect.width - w)/;
int h_add = (rect.height - h)/;
rect.x = rect.x - h_add;
rect.y = rect.y - w_add; rectangle(img, rect, Scalar(,,));
} }
}
opencv::将两幅图像合并后,在同一个窗口显示;并将合并的图像流保存成视频文件的更多相关文章
- [转]MFC子线程更改图像数据后更新主窗口图像显示方法
程序思路是由外部的输入输出控制卡发出采集图像信号,之后相机采集图像得到图像数据指针,接收图像数据指针创建成图像最后显示到MFC对话框应用程序的Picture Control控件上,同时,为了标定相机位 ...
- Opencv实现两幅图像融合
实现两幅图像线性(不同系数下)的融合涉及到Opencv中两个关键的方法,addWeighted()和createTrackbar() addWeighted方法: 函数原型: void addWeig ...
- OpenCv实现两幅图像的拼接
直接贴上源码 来源:http://www.myexception.cn/image/1498389.html 实验效果 Left.jpg right.jpg ImageMatch.jpg #inclu ...
- OpenCV --- 实现两幅图像并排合并(ROI)
Mat img1 = imread("1.png"); Mat img2 = imread("2.png"); int height = img1.rows; ...
- OpenCV 对两幅图像求和(求混合(blending))
#include <cv.h> #include <highgui.h> #include <iostream> using namespace cv; int m ...
- Opencv——将摄像头拍摄写成视频文件
这里主要利用了Opencv打开摄像头的代码,以及写入视频的函数,只是这里要注意的是摄像头好像没有帧率,在cvCreateVideoWriter,时要自己设置 #include"cv.h&qu ...
- maltab-图像拼接(左右两幅图)
图像拼接 参考自 https://blog.csdn.net/m0_37565736/article/details/79865990 并修改了其中错误的地方,添加自己的讲解或者看法. 我要拼接的是一 ...
- element-ui 使用span-method表格合并后hover样式的处理
在使用element表格合并后,发现鼠标只有移入第一个合并行时,合并的部分会高亮,移入其他行,不会高亮,这样效果看起来不是很好.查看了文档也没有直接的解决方法,就通过现有的方法处理了一下,解决了hov ...
- ffmpeg和opencv 播放视频文件和显示器
ffmpeg它是基于最新版本,在官网下载http://ffmpeg.zeranoe.com/builds/.编译时VS2010配置相关头文件及库的路径就可以.opencv的搭建參考上一个博客. 首先简 ...
随机推荐
- 【转】 VGA时序及其原理
显示器扫描方式分为逐行扫描和隔行扫描:逐行扫描是扫描从屏幕左上角一点开始,从左向右逐点扫描,每扫描完一行,电子束回到屏幕的左边下一行的起始位置,在这期间,CRT对电子束进行消隐,每行结束时,用行同步信 ...
- 机器学习 之梯度提升树GBDT
目录 1.基本知识点简介 2.梯度提升树GBDT算法 2.1 思路和原理 2.2 梯度代替残差建立CART回归树 1.基本知识点简介 在集成学习的Boosting提升算法中,有两大家族:第一是AdaB ...
- Windows Server 2012 R2域控制器部署
1. 概述 该文档描述了在Windows 2012R2 系统上搭建域控的方式. 2. 具体步骤 2.1 首先我们先配置好IP地址.计算名(默认的计算机名比较长,后期其它计算机加入域控的时候需要输入比较 ...
- 7——ThinkPhp中的响应和重定向:
public function index3(){ //响应数据: $data=['title'=>"标题部分","content"=>" ...
- 如何把已有SQLSERVER数据库更名而且附加到数据库中?
如何把已有SQLSERVER数据库更名而且附加到数据库中? 例如:已有数据库:zrmaa,希望更名为jjsh 特别提醒:数据库名中不能加入下划线,因为数据库日志文件有下划线. 把数据库文件mdf和数据 ...
- spring @bean 的理解
1.spring @bean 注解只能注解到方法上 2. 该方法必须返回一个实例对象 3.该过程相当于,通过一个方法去构造一个实例对象 ,然后交给spring管理 4.使用场景 如需要构造出一个特 ...
- ajax请求本地文件
这是一个小随笔,真的很简短! 主要入坑点有两个 一.Chrome浏览器默认不支持ajax读取本地文件 解决:1.关闭所有Chrome网页 2.右击Chrome浏览器,打开“属性” 3.弹出属性 ...
- Django的下载与项目的创建
一.Django的下载安装 Django官网下载页面 二.DOS窗口下的django项目从创建和启动 1. DOS窗口下载Django pip3 install django==1.11.9 2.DO ...
- java变量的作用域和基本数据类型转换
1.变量的作用域 赋值运算符 变量名 = 表达式 列: a = (b+3)+(b-1) 表达式就是符号(如:加号,减号)与操作数(如:b,3)的组合 自动类型转换(隐式类型转换):从小类型到大类型可以 ...
- Forth相关IO操作
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...