opencv提取前景
#include <SDKDDKVer.h>
#include <stdio.h>
#include <tchar.h>
#include<opencv2\highgui.hpp>
#include<opencv2\imgproc.hpp>
#include<iostream>
void canny(cv::Mat& img, cv::Mat& out) {
if (img.channels() == 3)
{
cv::cvtColor(img, out, cv::COLOR_BGR2GRAY);
cv::Canny(out, out, 100, 200);
}
else
{
cv::Canny(img, out, 100, 200);
}
cv::threshold(out, out, 128, 255, cv::THRESH_BINARY_INV);
}
class FrameProcessor {
public:
virtual void process(cv::Mat& input, cv::Mat& output) = 0;
};
class VideoProcessor {
private:
cv::VideoCapture capture;
void(*process)(cv::Mat&, cv::Mat&);
bool callIt;
std::string windowNameInput;
std::string windowNameOutput;
int delay;
long fnumber;
long frameToStop;
bool stop;
FrameProcessor* frameProcessor;
public:
VideoProcessor() : callIt(false), delay(-1), fnumber(0), stop(false), frameToStop(-1), process(NULL), frameProcessor(NULL) {}
bool setInput(std::string filename) {
fnumber = 0;
capture.release();
return capture.open(filename);
}
bool setInput(int device) {
fnumber = 0;
capture.release();
return capture.open(device);
}
void displayInput(std::string wn) {
windowNameInput = wn;
cv::namedWindow(windowNameInput);
}
void displayOutput(std::string wn) {
windowNameOutput = wn;
cv::namedWindow(windowNameOutput);
}
void setDelay(int d) {
delay = d;
}
void callProcess() {
callIt = true;
}
void dontCallProcess() {
callIt = false;
}
void setFrameProcessor(void(*frameProcessingCallback)(cv::Mat&, cv::Mat&)) {
process = frameProcessingCallback;
frameProcessor = NULL;
callProcess();
}
void stopAtFrameNo(long frameNo) {
frameToStop = frameNo;
}
void stopIt() {
stop = true;
}
bool isStopped() {
return stop;
}
bool isOpened() {
return capture.isOpened();
}
long getFrameNumber() {
long fnumber = static_cast<long>(capture.get(CV_CAP_PROP_POS_FRAMES));
return fnumber;
}
double getFrameRate() {
if (isOpened())
{
double rate = capture.get(CV_CAP_PROP_FPS);
if (!rate)
{
std::cout << "视频源已打开,但帧率无法获取" << std::endl;
}
return rate;
}
else
{
return 0;
}
}
bool readNextFrame(cv::Mat& frame) {
return capture.read(frame);
}
void run() {
cv::Mat frame;
cv::Mat output;
if (!isOpened())
return;
stop = false;
while (!isStopped()) {
if (!readNextFrame(frame))
break;
if (windowNameInput.length() != 0)
cv::imshow(windowNameInput, frame);
if (callIt) {
if (process)
process(frame, output);
else if (frameProcessor)
frameProcessor->process(frame, output);
else
output = frame;
}
else {
output = frame;
}
fnumber++;
if (windowNameOutput.length() != 0)
cv::imshow(windowNameOutput, output);
if (delay == 0 && 27 == cv::waitKey(0))
stopIt();
else if (delay > 0 && cv::waitKey(delay) >= 0)
stopIt();
if (frameToStop >= 0 && fnumber == frameToStop)
stopIt();
}
}
void setFrameProcessor(FrameProcessor* frameProcessorPtr) {
process = NULL;
frameProcessor = frameProcessorPtr;
callProcess();
}
};
class BGFGSegmentor : public FrameProcessor {
private:
cv::Mat gray;
cv::Mat background;
cv::Mat backImage;
cv::Mat foreground;
double learningRate;
int threshold;
public:
BGFGSegmentor() : threshold(10), learningRate(0.01) {}
void process(cv::Mat& frame, cv::Mat& output) {
cv::cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
if (background.empty())
gray.convertTo(background, CV_32F);
background.convertTo(backImage, CV_8U);
cv::absdiff(backImage, gray, foreground);
cv::threshold(foreground, output, threshold, 255,
cv::THRESH_BINARY_INV);
cv::accumulateWeighted(gray, background,
learningRate,
output);
}
void setThreshold(int t) {
threshold = t;
}
};
int main()
{
VideoProcessor processor;
BGFGSegmentor segmentor;
segmentor.setThreshold(25
);
processor.setInput("bike.avi"
);
processor.setFrameProcessor(
&segmentor);
processor.displayInput("原始视频");
processor.displayOutput("提取的前景\n"
);
processor.setDelay(1000. / processor.getFrameRate());
processor.run();
return 0;
}