我从“OpenCV 2计算机视觉”中复制并粘贴了帧差分代码 应用程序编程手册“。 但我在
上发了错误processor.setFrameProcessor(& segmentor);“它是 “'VideoProcessor :: setFrameProcessor':无法转换参数1 'BGFGSegmentor *'到'void(__ cdecl *)(cv :: Mat&,cv :: Mat&)'
我该如何解决? 感谢。
#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();
}