我的代码有问题。我尝试在Visual Studio 2015上使用opencv 3.0执行背景减法。函数main是:
int main()
{
//altrimento creo la finestra
Mat image;
Mat fgMaskMOG2;
Rect mr;
Ptr<BackgroundSubtractor> pKNN;
//imposto la sequenza delle immagini
std::string path = "D:\\ProjectFile\\CoreSaw_DataFusion\\Dataset\\Persona posa oggetto\\gray-%06d.jpg";
VideoCapture sequence(path);
//se non è aperto esco con codice di errore
if (!sequence.isOpened())
{
cerr << "Failed to open Image Sequence!\n" << endl;
return 1;
}
for (;;)
{
//scelgo l'immagine seguente
sequence >> image;
resize(image, image, Size(400,400));
//ridimensiono l'immagine perchè permette una miglior rilevazione
//se vuota finisco la sequenza
if (image.empty())
{
cout << "End of Sequence" << endl;
break;
}
try
{
apply_BackgroundSubstraction(image, fgMaskMOG2);
}
catch (Exception exc)
{
cerr << "Main: " << exc.msg << endl;
}
waitKey(1);
}
return 0;
}
我的功能是:
void apply_BackgroundSubstraction(Mat image, Mat fgMask)
{
vector< vector<Point> > contours;
try
{
namedWindow("Display window");// Create a window for display.
Ptr<BackgroundSubtractor> pKNN = createBackgroundSubtractorKNN(216, 3500.0, true);
pKNN->apply(image, fgMask); //applico l'algoritmo di background substraction
Mat contourImg = fgMask.clone();
findContours(contourImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
vector<vector< Point> >::iterator itc = contours.begin();
while (itc != contours.end()) {
Rect rect = boundingRect(Mat(*itc));
rectangle(image, rect, CV_RGB(255, 0, 0));
itc++;
}
imshow("Display window", fgMask);
imshow("Blob", image);
}
catch (Exception exc)
{
cerr << "apply_BackgroundSubstraction: " << exc.msg << endl;
}
}
当这个功能结束时我有这个错误
DEBUG ASSERTION FAILDED : IS BLOCK_TYPE_VALID(HEADER->BLOCK_USE)
你可以帮帮我吗?答案 0 :(得分:0)
这不是错误的答案,但您应在createBackgroundSubtractorKNN
函数中创建main
并将其传递给apply_BackgroundSubstraction
函数。
我的测试代码是使用VS 2015编译的,并且运行良好。
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include<opencv2/objdetect/objdetect.hpp>
#include "opencv2/video/background_segm.hpp"
#include<iostream>
using namespace cv;
using namespace std;
void apply_BackgroundSubstraction( Ptr<BackgroundSubtractor> pKNN, Mat image, Mat fgMask)
{
vector< vector<Point> > contours;
try
{
namedWindow("Display window");// Create a window for display.
pKNN->apply(image, fgMask); //applico l'algoritmo di background substraction
Mat contourImg = fgMask.clone();
findContours(contourImg, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
vector<vector< Point> >::iterator itc = contours.begin();
while (itc != contours.end()) {
Rect rect = boundingRect(Mat(*itc));
rectangle(image, rect, CV_RGB(255, 0, 0));
itc++;
}
imshow("Display window", fgMask);
imshow("Blob", image);
}
catch (Exception exc)
{
cout << "apply_BackgroundSubstraction: " << exc.msg << endl;
}
}
int main( int argc, char** argv )
{
char* filename = argc >= 2 ? argv[1] : (char*)"768x576.avi";
VideoCapture capture( filename );
Mat frame,output;
Ptr<BackgroundSubtractor> pKNN = createBackgroundSubtractorKNN(216, 3500.0, true);
while(true)
{
capture.read(frame);
if (!frame.data)
return 0;
apply_BackgroundSubstraction( pKNN, frame, output);
if(waitKey(1)==27)
{
break;
}
}
return 0;
}