findContours 返回断言失败



似乎findContours函数在Visual Studios C++2012中返回了一些断言失败。 我已经确保我所有的包含目录都很好。

void track (Mat input_video, Mat &output_video)
    {
    Mat temp;
    input_video.copyTo(temp);
    vector<vector<cv::Point> > contours;
    vector<Vec4i> hierarchy;
    findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    //Only search through the most external layer.
    for (int i=0; i>=0; i=hierarchy[i][0])
    {
        Moments moment = moments((Mat) contours[i]);
        double area= moment.m00;
        double xmc= moment.m10;
        double ymc= moment.m01;
        double x= xmc/area;
        double y= ymc/area;
        circle(output_video, Point(x,y), 3, Scalar(0,255,255));
    }
}

编辑:

我的其余代码(如果您需要):

#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
//This function threshold the HSV image and create a binary image
Mat GetThresholdedImage(Mat imgHSV, int huemin, int satmin, int valmin, int huemax, int satmax, int valmax)
{
    //Size s= imgHSV->size();
    Mat imgThresh(imgHSV.size().height, imgHSV.size().width, CV_8U);
    //returns matrix that contains all values within the given HSV range.
    inRange(imgHSV, Scalar(huemin,satmin,valmin), Scalar(huemax,satmax,valmax), imgThresh); 
       return imgThresh;
}
Mat morph(Mat imgThresh)
{
    Mat erode_element= getStructuringElement(MORPH_RECT, Size(3,3));
    Mat dilate_element= getStructuringElement(MORPH_RECT, Size(8,8));
    erode(imgThresh, imgThresh, erode_element);
    erode(imgThresh, imgThresh, erode_element);
    dilate(imgThresh, imgThresh, dilate_element);
    return imgThresh;
}
void track (Mat input_video, Mat &output_video)
{
    Mat temp;
    input_video.copyTo(temp);
    vector< vector<Point> > contours;
    cerr<< contours.size();
    vector<Vec4i> hierarchy;
    findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
    cerr<<contours.size();
    //Only search through the most external layer.
    for (int i=0; i>=0; i=hierarchy[i][0])
    {
        Moments moment = moments((Mat) contours[i]);
        double area= moment.m00;
        double xmc= moment.m10;
        double ymc= moment.m01;
        double x= xmc/area;
        double y= ymc/area;
        circle(output_video, Point(x,y), 3, Scalar(0,255,255));
    }
}

int main()
{
    VideoCapture capture(0);
    if(!capture.isOpened()){
        cerr<< "Capture failed";
        return -1;
      }
    Mat frame;
    namedWindow("Original");      
    namedWindow("Binary");
    //Create slider for adjustments...
    namedWindow("Adjustments");
    int huemin= 0, satmin= 0, valmin= 0;
    int huemax= 256, satmax= 256, valmax= 256;
    createTrackbar("Min Hue", "Adjustments", &huemin, 256);
    createTrackbar("Max Hue", "Adjustments", &huemax, 256);
    createTrackbar("Min Saturation", "Adjustments", &satmin, 256);
    createTrackbar("Max Saturation", "Adjustments", &satmax, 256);
    createTrackbar("Min Value", "Adjustments", &valmin, 256);
    createTrackbar("Max Value", "Adjustments", &valmax, 256);
      //iterate through each frames of the video      
    while(true)
    {
        //Grabs each frame from video to be processed.
        capture>> frame;
        //Error checks and breaks if the grab failed.
        if(!frame.data)
            {
                cerr<< "Failed to grab framen";
                break;
            }
        //Apply a Gaussian Blur kernel.
        //GaussianBlur(frame, frame, Size(3,3), 3, 3, 4);
        Mat imgHSV(frame.size(), CV_8UC3);
        cvtColor(frame, imgHSV, CV_BGR2HSV, 0); //Change the color format from BGR to HSV
        Mat imgThresh = GetThresholdedImage(imgHSV, huemin, satmin, valmin, huemax, satmax, valmax);
        //GaussianBlur(imgThresh, imgThresh, Size(5,5), 3, 3, 4); //smooth the binary image using Gaussian kernel
        imgThresh= morph(imgThresh);
        track(imgThresh, frame);
        imshow("Binary", imgThresh);  
        imshow("Original", frame);
        int key = waitKey(30);
        //If 'ESC' is pressed, break the loop
        if(key==27 ) break;      
      }
    destroyAllWindows();
      //cvReleaseCapture(&capture);     
      return 0;
}

金,我遇到了同样的问题。 尝试添加 "cv::imshow("im", output_video);"如中所述 - OpenCV 查找轮廓导致调试断言返回失败

相关内容

  • 没有找到相关文章

最新更新