0

The code looks like that:

#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);
} 

It is supposed to cut the region of interest where the contours are detected, leaving the background which i dont need.

The code works on the computer with OpenCV 3.1, it is a little bit modified code from OpenCV documentation.

The error is as follows:

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.

and the debugger points to the line:

        __scrt_debugger_hook_flag = 0;

No idea how to tackle this one, is it because my libraries are incorrectly installed or is it some rookie mistake in the code?

Cheers

  • The code above works as intended in my environment. When you are in debug mode you should use opencv_core249d.lib, "d" means debug. Not sure but that might be the problem. Try, in release mode also. – Ockhius May 29 '16 at 12:54
  • I'm using the release mode and it crashes. Either way I've found a way around it without using the findContour() function, just going through the thresholded image pixel by pixel. – Jeremi Podlasek May 29 '16 at 12:56
  • If you're linking to release libraries (without trailing "d") but you run in debug, you'll have this error. – Miki May 29 '16 at 20:40

0 Answers0