OpenCV 2.4.9函数findContours()导致程序崩溃

时间:2016-05-29 10:08:20

标签: c++ opencv roi

代码如下:

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <stdio.h>
#include<time.h>
using namespace cv;
using namespace std;
int main()
{
    Mat image, edges, src_gray, output;

    int largest_area = 0;
    int largest_contour_index = 0;
    Rect bounding_rect;
    string Destination = "F:\\vision_systems\\NoveltyDetectionData\\Set3\\TRAINING_DATA\\m_DSC_0030_m.jpg";
    image = imread(Destination, CV_LOAD_IMAGE_COLOR);
    Mat dst(image.rows, image.cols, CV_8UC1, Scalar::all(0));
    cvtColor(image, src_gray, CV_BGR2GRAY);
    blur(src_gray, src_gray, Size(3, 3));

    Canny(src_gray, edges, 80, 200, 3);
    threshold(edges, output, 100, 255, THRESH_BINARY);
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    vector<Rect> boundRect(contours.size());
    findContours(output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
    Mat drawing = Mat::zeros(edges.size(), CV_8UC3);
    for (int i = 0; i< contours.size(); i++) 
    {
        double a = contourArea(contours[i], false);  
        if (a>largest_area) {
            largest_area = a;
            largest_contour_index = i;                
            bounding_rect = boundingRect(contours[i]); 
        }

    }

    Scalar color(255, 255, 255);
    drawContours(dst, contours, largest_contour_index, color, CV_FILLED, 8, hierarchy); 
    rectangle(image, bounding_rect, Scalar(0, 255, 0), 1, 8, 0);
    imshow("src", image);
    imshow("largest Contour", output);
    Mat roiImg;
    roiImg = image(bounding_rect);
    imshow("roiIMG", roiImg);
    waitKey(0);
} 

应该切割检测到轮廓的感兴趣区域,留下我不需要的背景。

代码在OpenCV 3.1的计算机上运行,​​它是OpenCV文档中的一点修改代码。

错误如下:

Microsoft Visual Studio C Runtime Library has detected a fatal error in ConsoleApplication2.exe.

Press Break to debug the program or Continue to terminate the program.

并且调试器指向该行:

        __scrt_debugger_hook_flag = 0;

不知道如何解决这个问题,是因为我的库安装不正确还是代码中出现了一些菜鸟错误?

干杯

0 个答案:

没有答案