/**

* @file main-opencv.cpp

* @date July 2014

* @brief An exemplative main file for the use of ViBe and OpenCV*/

//#include

#include "vibe-background-sequential.h"

using namespacecv;using namespacestd;const int minArea = 2; //舍去面积极小的方框

const double maxAreaRatio = 0.1; //舍去面积极大的方框

const double boxRatio = 0.1; //舍去长宽比严重失衡的方框

/**

* Displays instructions on how to use this program.*/

voidhelp()

{

cout<< "--------------------------------------------------------------------------" <" <

}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 <

cerr <

return EXIT_FAILURE;

}

/* Create GUI windows.*/

//namedWindow("Frame");//namedWindow("Segmentation by ViBe");

processVideo("framelink_yd.avi"); //读取 framelink_yd.avi 视频进行处理

/*Destroy GUI windows.*/destroyAllWindows();returnEXIT_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;doubletotal;

start=clock();/*Variables.*/

static int frameNumber = 1; /*The current frame number*/

int lastCount = 0;int probFactor = gradient_Factor; /*概率因子,用梯度因子初始化概率因子*/Mat frame,frame1;/*Current frame.*/Mat segmentationMap;/*Will contain the segmentation map. This is the binary output map.*/

int keyboard = 0; /*Input from keyboard. Used to stop the program. Enter 'q' to quit.*/

char fileName[200] = { 0};int sampleCounts[10] = {0};int speSamples[10] = {0};

vibeModel_Sequential_t*model = NULL; /*Model used by ViBe.*/

int heig = 512;int widt = 1340;

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'), 70, sz, true);while ((char)keyboard != 'q' && (char)keyboard != 27 ) { /*Read input data. ESC or 'q' for quitting.*/

if (!capture.read(frame1)) { /*Read the current frame.*/cerr<< "Unable to read next frame." <

cerr<< "Exiting..." <

}/*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==1) {

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,3); /*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(0, 0, 640, 512)));

frame1.copyTo(res(Rect(700, 0, 640, 512)));

imshow("res", res); //显示合并后的图像/*将 res 写入打开的视频文件中*/writer<

keyboard= waitKey(1); /*Gets the input from the keyboard.*/}

finish=clock();

total=(double)(finish-start);

cout<

cout<

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 == 255);//Each detected contour is stored as a vector of points

vector >contours;

vector hierarchy; //containing information about the image topology

findContours(tmp, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);for (size_t k = 0; 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 < 20)

rect.width= 20;/*if(rect.width < 6)

rect.width = 2*rect.width;

else

rect.width = rect.width + rect.width/2;*/

if(rect.height < 20)

rect.height= 20;/*if(rect.height < 6)

rect.height = 2*rect.height;

else

rect.height = rect.height + rect.height/2;*/

int w_add = (rect.width - w)/2;int h_add = (rect.height - h)/2;

rect.x= rect.x -h_add;

rect.y= rect.y -w_add;

rectangle(img, rect, Scalar(0,0,255));

}

}

}

opencv方框内图像保存_opencv::将两幅图像合并后,在同一个窗口显示;并将合并的图像流保存成视频文件...相关推荐

  1. matlab求两个图像的误差,求两幅图像的均方误差

    测量电源电动势和内阻图像误差 从坐标图上可以清楚的看到:由于电流表和电压表连接的位置不同,测试的误差也不同,当把电压表接在电流表的外侧时,在电路接通状态下,实际把电流表的内阻产生的压降也算进去了,电压 ...

  2. python——生成带logo的二维码图片并且保存、控制打印机打印图片二维码、整合打印(获取输入框的值)、打包成exe文件

    1.生成带logo的二维码图片并且保存 前提条件:在D盘里有logo.png的图片,生成的二维码图片在D盘里的111.png import qrcode from PIL import Image# ...

  3. opencv方框内图像保存_opencv 图像滤波(均值,方框,高斯,中值)

    为什么要使用滤波 消除图像中的噪声成分叫作图像的平滑化或滤波操作.信号或图像的能量大部分集中在幅度谱的低频和中频段是很常见的,而在较高频段,感兴趣的信息经常被噪声淹没.因此一个能降低高频成分幅度的滤波 ...

  4. opencv方框内图像保存_opencv利用矩形框选中某一区域并保存为新图片

    本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下 一.基本原理 Mat img= imread(image): Rect rect(50,20, 200, 50); ...

  5. 利用Python+opencv进行视频文件的读取和保存,打开笔记本摄像头拍照保存、图像在窗口显示等操作

    版权声明:本文为博主原创文章,转载请附源链接 一.视频文件的读取和保存 Opencv中视频的读入是用VideoCapture函数,保存用的是VideoWriter函数.这两个函数支持的视频格式因电脑系 ...

  6. matlab合并fig图像,matlab怎么把两个fig叠加

    1. 怎样用matlab把两个figure中的图叠加 figure(1); x=-4:0.5:4; y=x [X,Y]=meshgrid(x,y); Z=X.^2+Y.^2; subplot(211) ...

  7. ffmpeg和opencv 播放视频文件和显示器

    ffmpeg它是基于最新版本,在官网下载http://ffmpeg.zeranoe.com/builds/.编译时VS2010配置相关头文件及库的路径就可以.opencv的搭建參考上一个博客. 首先简 ...

  8. ffmpeg和opencv 播放视频文件并显示

    ffmpeg是基于最新版本,在官网下载http://ffmpeg.zeranoe.com/builds/.编译时VS2010配置相关头文件及库的路径即可.opencv的搭建参考上一个博客. 首先简单介 ...

  9. hutool 读取扩展名文件_Python OpenCV视觉智能感知第一讲——读取摄像头或视频文件并播放显示...

    Python OpenCV视觉智能感知 第一讲--读取摄像头或视频文件并播放显示 本部分内容将深入.全面.详细地介绍如何使用Anaconda Python和OpenCV读取摄像头或视频文件,并进行播放 ...

最新文章

  1. 3、vue-router之什么是动态路由
  2. 控制div的大小自适应_可以漂移的电动轮椅,采用“自适应重心控制系统”,根本不怕翻车...
  3. C++字符串完全指引之二 —— 字符串封装类
  4. 4dda在linux中的意思,Evvail | MaxQuant-蛋白质组DDA数据分析金标准 | Omics - Hunter
  5. bzoj 1010: [HNOI2008]玩具装箱toy 2011-12-27
  6. STM32 电机教程 32 - 基于ST X-CUBE-SPN7 无刷无感电机库的电机驱动实现
  7. 插入排序法(思路及代码实现)
  8. html怎么用ui打开,HTML5教程 如何使用原生UI
  9. clickhouse 分片
  10. 最爱的城市(dfs)
  11. Tutorials 使用窗口功能分析信息
  12. Hr人力资源管理系统怎样给企业创造价值
  13. python官网下载pip_python怎么下载pip
  14. 《王道计算机网络》学习笔记总目录+思维导图
  15. MCS-51单片机寻址方式详解
  16. 基于数组判断字符串是否是回文
  17. 《假如爱有天意》月光如春风拂面,你如种子深埋我心
  18. Echarts 用图形纹理来填充颜色(color - pattern)
  19. 红帽子服务器虚拟化技术只要是,红帽子企业版.Linux.5
  20. 三菱FX2N系列PLC如何进行远程上下载和调试

热门文章

  1. C#中IEnumerableT.Select()、SelectMany()的简单使用
  2. java js websocket_js+java websocket记录
  3. 字符串转内存c语言,【一起学C】C语言面试题必考:字符串操作函数,内存操作函数实现...
  4. python函数进阶小结_python之函数进阶
  5. JAVA实现inotify一样的功能_WPF实现INotifyPropertyChanged
  6. 谷歌浏览器无网络连接 打不开网页解决办法
  7. 优酷视频如何意见反馈?优酷视频怎么意见反馈
  8. 如何打造高大上的微信朋友圈 打造微信高逼格朋友圈教程
  9. 禁止IE页面自动跳转到EDGE浏览器的方法教程
  10. 如何推送和播放RTMP H265流 (RTMP HEVC)