为什么我会通过findContours接收断点?

时间:2014-04-13 13:04:44

标签: c++ opencv

我一直在尝试设置一个程序,该程序使用HSV检测特定颜色,并在所述颜色的最大区域周围放置一个矩形。当我到达向量和“findContours”函数时,我遇到了问题,该函数使用“xxx.exe触发了断点”的错误来停止程序。

仅当我插入行

时才会发生这种情况
findContours(dilatedBlue,contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

我是OpenCV和C ++的新手,但我希望有人能提供帮助。我正在使用Windows 7 x64,Visual Studio 2012和OpenCV 2.4.8。

我的完整代码如下:

#include "stdafx.h"
#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;


int _tmain(int argc, _TCHAR* argv[])
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened())  // check if we succeeded
    return -1;

// Declare Local Variables
int Quit = 0;
Mat imageDiff;
Mat HSVMatrix[3];
Mat erodedBlue;
Mat dilatedBlue;


//Start of search/shoot routine
while( Quit != 'q' )    // For as long as User wishes to continue...
{

    Mat image;
    cap >> image; // get a new frame from camera
    imshow("Original", image);
    //GaussianBlur(image, image, Size(3,3), 1.5, 1.5);            // Setting up a Gaussian blur using 5x5 grid
    cvtColor(image, imageDiff, CV_BGR2HSV);
    split(imageDiff,HSVMatrix);

    //Setting up saturation values for colour blue
    threshold(HSVMatrix[0],HSVMatrix[0],130,255,THRESH_TOZERO_INV);    // High value of colour - Dynamic changing of variable to select mode 130
    threshold(HSVMatrix[0],HSVMatrix[0],75,255,THRESH_BINARY);        // Low  value of colour 75
    threshold(HSVMatrix[1],HSVMatrix[1],255,255,THRESH_TOZERO_INV);    // High saturation value - Modify for varying levels of light
    threshold(HSVMatrix[1],HSVMatrix[1],100,255,THRESH_BINARY);        // Low saturation value

    HSVMatrix[2]=HSVMatrix[1]&HSVMatrix[0];
    imshow("HSVblue", HSVMatrix[2]);            // Displays binarised image
    erode(HSVMatrix[2], HSVMatrix[2], Mat(), Point(-1, -1), 2, 1, 1);
    erode(HSVMatrix[2], erodedBlue, Mat(), Point(-1, -1), 2, 1, 1);
    imshow("Eroded-Blue", erodedBlue);
    dilate(erodedBlue, dilatedBlue, Mat(), Point(-1, -1), 2, 1, 1);
    imshow("Dilated-Blue", dilatedBlue);

    vector<vector<Point> > contours;        // Temporary variables used to hold contour information
    vector<Point> approx;
    vector<Vec4i> hierarchy;
    vector<Rect> boundRect(contours.size());
    findContours(dilatedBlue,contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

    for (int i = 0; i < contours.size(); i++)
        {
            approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true) * 0.02, true);               // Calculation of contours and their approximate size and sides
        }

        for (int i = 0; i < contours.size(); i++)
        {
            if ((approx.size() == 4) && (fabs(contourArea(Mat(approx))) > 100))                                     // The properties that have to be fulfilled in order for the object to be recognized by the computer
            {
                Rect outline;                                                                                 // If the properties have been fulfilled and the object has been recognized, display an outline around the object
                outline = boundingRect(approx);
                rectangle(image,Rect(outline),Scalar(0, 255, 255),5,8,0);                                       // The properties of the outline: the colour[bgr] and thickness
            }
        }
        imshow("Target", image);
    }

    Quit = cvWaitKey(1);                   // The program waits for the key "q" to be pressed to exit
}
cap.release();
return 0;

}

0 个答案:

没有答案