opencv中的帧差分
我复制并粘贴了“OpenCV 2计算机视觉”中的帧差分代码 应用程序编程手册”。 但我给了一个错误 processor.setFrameProcessor(&segmentor);”“是的 “'VideoProcessor::setFrameProcessor':无法将参数1从 “BGFGSegmentor*”到“void(uu cdecl*)(cv::Mat&,cv::Mat&)” 我怎样才能修好它? 谢谢opencv中的帧差分,opencv,frame,cookbook,Opencv,Frame,Cookbook,我复制并粘贴了“OpenCV 2计算机视觉”中的帧差分代码 应用程序编程手册”。 但我给了一个错误 processor.setFrameProcessor(&segmentor);”“是的 “'VideoProcessor::setFrameProcessor':无法将参数1从 “BGFGSegmentor*”到“void(uu cdecl*)(cv::Mat&,cv::Mat&)” 我怎样才能修好它? 谢谢 #包括 #包括 #包括 #包括 #包括 使用名称空间cv; 使用名称空间std; #包
#包括
#包括
#包括
#包括
#包括
使用名称空间cv;
使用名称空间std;
#包括
#包括
#包括
//////////////////////////////////////////////////////////////////////
类视频处理器{
私人:
//OpenCV视频捕获对象
视频捕获;
//要调用的回调函数
//用于处理每个帧
无效(*过程)(cv::Mat&,cv::Mat&);
//用于确定
//将调用进程回调
布尔卡利特;
//输入显示窗口名称
std::字符串windowNameInput;
//输出显示窗口名称
std::字符串windowNameOutput;
//每帧处理之间的延迟
整数延迟;
//已处理帧数
长编号;
//停在这个帧号
长框架顶部;
//停止处理
布尔停止;
公众:
视频处理器():callIt(真),延迟(0),
fnumber(0),stop(false),frameToStop(-1){
//设置所需的回调函数
//将为每个帧调用
void setFrameProcessor(void(*frameProcessingCallback)
(cv::Mat&,cv::Mat&)
{
process=frameProcessingCallback;
}
//设置视频文件的名称
bool setInput(标准::字符串文件名){
fnumber=0;
//以防资源已被删除
//与VideoCapture实例关联
捕获。释放();
//图像。清除();
//打开视频文件
返回capture.open(文件名);
}
//显示已处理的帧的步骤
void displayInput(std::string wn){
windowNameInput=wn;
cv::namedWindow(windowNameInput);
}
//显示已处理的帧的步骤
void显示输出(std::string wn){
windowNameOutput=wn;
cv::namedWindow(windowNameOutput);
}
//不显示已处理的帧
void dontDisplay(){
cv::destroyWindow(windowNameInput);
cv::destroyWindow(windowNameOutput);
windowNameInput.clear();
windowNameOutput.clear();
}
//获取(并处理)序列的帧
无效运行(){
//当前帧
cv::垫架;
//输出帧
cv::Mat输出;
//如果未设置捕获设备
如果(!isopend())
返回;
停止=错误;
而(!isStopped()){
//如果有,请阅读下一帧
如果(!readNextFrame(frame))
打破
//显示输入框
如果(windowNameInput.length()!=0)
cv::imshow(windowNameInput,frame);
//调用进程函数
if(callIt){
//处理框架
过程(帧、输出);
//递增帧数
fnumber++;
}否则{
输出=帧;
}
//显示输出帧
如果(windowNameOutput.length()!=0)
cv::imshow(windowNameOutput,output);
//耽搁
如果(延迟>=0&&cv::waitKey(延迟)>=0)
停止();
//看看我们是否应该停下来
如果(frameToStop>=0&&
getFrameNumber()==frameToStop)
停止();
}
}
//停止处理
void stopIt(){
停止=真;
}
//进程是否已停止?
布尔{
返回站;
}
//是否打开了捕获设备?
bool isOpened(){
capture.isOpened();
}
//设置每个帧之间的延迟
//0表示在每一帧等待
//否定意味着没有延迟
无效设置延迟(int d){
延迟=d;
}
//要获得下一帧
//可以是:视频文件或照相机
bool readNextFrame(cv::Mat&frame){
返回捕获读取(帧);
}
//要调用的进程回调
void callProcess(){
callIt=真;
}
//不要调用进程回调
void dontCallProcess(){
callIt=假;
}
无效停止帧编号(长帧){
frameToStop=框架;
}
//返回下一帧的帧编号
长getFrameNumber(){
//从捕获设备获取的信息
长fnumber=静态\u转换(
获取(CV_CAP_PROP_POS_FRAMES));
返回fnumber;
}
};
///////////////////////////////////////////////////////////////////////
//帧处理器接口
类帧处理器{
公众:
//处理方法
虚拟无效过程(cv::Mat&输入,cv::Mat&输出)=0;
};
类BGFGSegmentor:公共帧处理器{
cv::Mat gray;//当前灰度图像
cv::Mat background;//累积背景
cv::Mat backImage;//背景图像
cv::Mat前景;//前景图像
//背景积累中的学习率
双学习率;
int threshold;//前景提取的阈值
公众:
BGFGSegmentor():阈值(10),学习率(0.01){
无效设置阈值(浮动t){
阈值=t;
}
//处理方法
无效过程(cv::材料和框架,cv::材料和输出){
//转换为灰度图像
cv::CVT颜色(边框、灰色、cv_bgr2灰色);
//将背景初始化为第一帧
if(background.empty())
灰色。convertTo(背景,CV_32F);
//将背景转换为8U
背景转换(背景图像,CV_8U);
//计算图像和背景之间的差异
cv::absdiff(背景图像、灰色、前景);
//对前景图像应用阈值
cv::阈值(前景、输出、,
阈值,255,cv::阈值(二进制值);
//积累背景
cv::累计加权(灰色、背景、,
学习率、输出);
}
};
int main()
{
//创建视频处理器实例
视频处理器;
//创建背景/前景分段器
bgfg分节器;
切分器
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/background_segm.hpp >
using namespace cv;
using namespace std;
#include <iostream>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/video/tracking.hpp>
//////////////////////////////////////////////////////////////////////
class VideoProcessor {
private:
// the OpenCV video capture object
cv::VideoCapture capture;
// the callback function to be called
// for the processing of each frame
void (*process)(cv::Mat&, cv::Mat&);
// a bool to determine if the
// process callback will be called
bool callIt;
// Input display window name
std::string windowNameInput;
// Output display window name
std::string windowNameOutput;
// delay between each frame processing
int delay;
// number of processed frames
long fnumber;
// stop at this frame number
long frameToStop;
// to stop the processing
bool stop;
public:
VideoProcessor() : callIt(true), delay(0),
fnumber(0), stop(false), frameToStop(-1) {}
// set the callback function that
// will be called for each frame
void setFrameProcessor( void (*frameProcessingCallback)
(cv::Mat&,cv::Mat&))
{
process= frameProcessingCallback;
}
// set the name of the video file
bool setInput(std::string filename) {
fnumber= 0;
// In case a resource was already
// associated with the VideoCapture instance
capture.release();
//images.clear();
// Open the video file
return capture.open(filename);
}
// to display the processed frames
void displayInput(std::string wn) {
windowNameInput= wn;
cv::namedWindow(windowNameInput);
}
// to display the processed frames
void displayOutput(std::string wn) {
windowNameOutput= wn;
cv::namedWindow(windowNameOutput);
}
// do not display the processed frames
void dontDisplay() {
cv::destroyWindow(windowNameInput);
cv::destroyWindow(windowNameOutput);
windowNameInput.clear();
windowNameOutput.clear();
}
// to grab (and process) the frames of the sequence
void run() {
// current frame
cv::Mat frame;
// output frame
cv::Mat output;
// if no capture device has been set
if (!isOpened())
return;
stop= false;
while (!isStopped()) {
// read next frame if any
if (!readNextFrame(frame))
break;
// display input frame
if (windowNameInput.length()!=0)
cv::imshow(windowNameInput,frame);
// calling the process function
if (callIt) {
// process the frame
process(frame, output);
// increment frame number
fnumber++;
} else {
output= frame;
}
// display output frame
if (windowNameOutput.length()!=0)
cv::imshow(windowNameOutput,output);
// introduce a delay
if (delay>=0 && cv::waitKey(delay)>=0)
stopIt();
// check if we should stop
if (frameToStop>=0 &&
getFrameNumber()==frameToStop)
stopIt();
}
}
// Stop the processing
void stopIt() {
stop= true;
}
// Is the process stopped?
bool isStopped() {
return stop;
}
// Is a capture device opened?
bool isOpened() {
capture.isOpened();
}
// set a delay between each frame
// 0 means wait at each frame
// negative means no delay
void setDelay(int d) {
delay= d;
}
// to get the next frame
// could be: video file or camera
bool readNextFrame(cv::Mat& frame) {
return capture.read(frame);
}
// process callback to be called
void callProcess() {
callIt= true;
}
// do not call process callback
void dontCallProcess() {
callIt= false;
}
void stopAtFrameNo(long frame) {
frameToStop= frame;
}
// return the frame number of the next frame
long getFrameNumber() {
// get info of from the capture device
long fnumber= static_cast<long>(
capture.get(CV_CAP_PROP_POS_FRAMES));
return fnumber;
}
};
///////////////////////////////////////////////////////////////////////
// The frame processor interface
class FrameProcessor {
public:
// processing method
virtual void process(cv:: Mat &input, cv:: Mat &output)= 0;
};
class BGFGSegmentor : public FrameProcessor {
cv::Mat gray; // current gray-level image
cv::Mat background; // accumulated background
cv::Mat backImage; // background image
cv::Mat foreground; // foreground image
// learning rate in background accumulation
double learningRate;
int threshold; // threshold for foreground extraction
public:
BGFGSegmentor() : threshold(10), learningRate(0.01) {}
void setThreshold(float t) {
threshold= t;
}
// processing method
void process(cv:: Mat &frame, cv:: Mat &output) {
// convert to gray-level image
cv::cvtColor(frame, gray, CV_BGR2GRAY);
// initialize background to 1st frame
if (background.empty())
gray.convertTo(background, CV_32F);
// convert background to 8U
background.convertTo(backImage,CV_8U);
// compute difference between image and background
cv::absdiff(backImage,gray,foreground);
// apply threshold to foreground image
cv::threshold(foreground,output,
threshold,255,cv::THRESH_BINARY_INV);
// accumulate background
cv::accumulateWeighted(gray, background,
learningRate, output);
}
};
int main()
{
// Create video procesor instance
VideoProcessor processor;
// Create background/foreground segmentor
BGFGSegmentor segmentor;
segmentor.setThreshold(25);
// Open video file
processor.setInput("C:/Users/Pedram91/Downloads/Video/FLIR Systems.flv");
// set frame processor
processor.setFrameProcessor( &segmentor);
// Declare a window to display the video
processor.displayOutput("Extracted Foreground");
// Play the video at the original frame rate
processor.setDelay(1000./33);//processor.setDelay(1000./processor.getFrameRate());//capture.get(CV_CAP_PROP_FPS)
// Start the process
processor.run();
}