c++ - Opencv 2.4.12_2 calcOpticalFlowPyrLK() 错误

标签 c++ opencv video-tracking

我正在尝试使用 OpenCV 的 calcOpticalFlowPyrLK() 函数,但我似乎无法克服这个错误:

OpenCV Error: Assertion failed (mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0)) in create, file /tmp/opencv20160107-21708-lubvml/opencv-2.4.12/modules/core/src/matrix.cpp, line 1486 libc++abi.dylib: terminating with uncaught exception of type cv::Exception: /tmp/opencv20160107-21708-lubvml/opencv-2.4.12/modules/core/src/matrix.cpp:1486: error: (-215) mtype == type0 || (CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 << type0) & fixedDepthMask) != 0) in function create

我检查了输入参数,两张图片都是: 尺寸:[1280 x 720] 深度:(CV_8U)

代码如下:

Mat frame;
Mat back;
Mat fore;
Mat temp;
Mat prevImage;
Mat currImage;
// for floodfill
Point seed = Point(0,0);
VideoCapture cap("./Sequence2/Sequence_03_%03d.jpg");
// Background subtraction
BackgroundSubtractorMOG2 bg;
bg.set("nmixtures",5);
bg.set("detectShadows", true);
bg.set("fTau", 0.5);
//
bool foundpoints = false;
vector<uchar> status, err;
// Blob Detector
SimpleBlobDetector::Params params;
//params.filterByColor = true;
//params.blobColor = 255; // use if bitwise not statement used
// // Filter by Area.
params.filterByArea = true; //size 
params.minArea = 25;
params.maxArea = 300;
params.filterByCircularity = true; // circle or not
params.minCircularity = 0.15;
params.filterByConvexity = true; // closed or not
params.minConvexity = 0.92;
params.filterByInertia = true; // elongated or not
params.minInertiaRatio = 0.40;
SimpleBlobDetector detector(params);
//
std::vector<std::vector<Point> > contours;
std::vector<KeyPoint> keypoints;
std::vector<Point2f> pKeypoints, prevKeypoints;
namedWindow("Video");
//namedWindow("Background");

for (int i = 0; i < 623; i++) {
    file << "./Output3/image" << i << ".jpg";
    cap >> frame;
    // CONVERT TO GRAY
    cvtColor(frame, temp, CV_RGB2GRAY);
    //adaptiveThreshold(frame,temp,1,ADAPTIVE_THRESH_MEAN_C,THRESH_BINARY,3,1);
    bg.operator ()(temp,fore);
    //bg.getBackgroundImage(back);

    // THREHOLD THE IMAGE
    threshold(fore,fore,80,150,THRESH_TOZERO);
    erode(fore,fore,cv::Mat());
    dilate(fore,fore,cv::Mat());
    findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
    // REMOVE SOME NOISE
    cv::floodFill(fore, seed,(255));
    erode(fore,fore,cv::Mat());
    dilate(fore,fore,cv::Mat());
    //bitwise_not(fore,fore);
    erode(fore,fore,cv::Mat());
    dilate(fore,fore,cv::Mat());
    detector.detect(fore, keypoints);
    //std::cout << keypoints.size() << "\n";
    if (keypoints.size() > 0){
        if (foundpoints == false)
            foundpoints = true;
        else{
            cv::Size winsize = fore.size();
            KeyPoint::convert(keypoints,pKeypoints);
           // std::cout << pKeypoints.size() << " , " << prevKeypoints.size() << " , " << prevImage.depth() << " , "<< fore.depth();
            calcOpticalFlowPyrLK(prevImage,fore,prevKeypoints,pKeypoints,status,err,winsize);
            prevImage.pop_back();
        }
        prevImage.push_back(fore);
        KeyPoint::convert(keypoints,prevKeypoints);
    }
    imshow("Frame",fore); // frame
    // imshow("Background",back);
    if(cv::waitKey(30) >= 0) break;
    imwrite(file.str(), fore);
    file.str("");
}

最佳答案

更改 vector<uchar> errMat err .

关于c++ - Opencv 2.4.12_2 calcOpticalFlowPyrLK() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36955367/

相关文章:

c++ - 如何不运行vcvars32.bat直接运行cl.exe?

c++ - Opencv 人脸变形

c++ - vector 索引变量声明(size_t 或 std::vector<DATATYPE>::size_type)

opencv - 从opencv imencode删除std::vector <unsigned char>时发生异常

python - 我如何一次获得许多帧?

python - 在 Python 中为 OpenCV BoundryBox 设置自定义 RoI

python - 如何找到运动物体轨迹中的冗余路径(子路径)?

matlab - 比较两个边界框 Matlab

c++ - cpp文件中的全局变量

c++ - 如何抛出超出范围的 C++ 异常