1

I am using OpenCV 3.2 and compiling it with Visual Studio 2017.

I am trying to build a simple motion tracking code by thresholding a camera feed into a binary image, then using the OpenCV's findContours function to detect the object. Unfortunately, I have been running into an error which says that there is an unhandled exception at the function. (error message) I have linked the OpenCV libraries by manually adjusting the project properties as following:

  1. C/C++ - General - Additional Include Directory - C:\opencv320\build\include
  2. Linker - General - Additional Library Directory - C:\opencv320\build\x64\vc14\lib
  3. Linker - Input - Additional Dependencies - opencv_world320d.lib and opencv_world320.lib

I have been testing different codes with the same findContours function, but no matter the code, it seems to give the same error. I think it has to do with the heap, and how memory is allocated and deallocated, but I cannot seem to debug it myself.

Here is an example code that I have been testing:

#include "stdafx.h"
#include <opencv2\highgui.hpp>
#include <opencv2\videoio.hpp>
#include <opencv2\opencv.hpp>
#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;


struct Component
{
    cv::Rect boundingBox;
    double area;
    double circularity;
};

int main()
{
    // Create a small image with a circle in it.
    cv::Mat image(256, 256, CV_8UC3, cv::Scalar(0, 0, 0));
    cv::circle(image, cv::Point(80, 110), 42, cv::Scalar(255, 127, 63), -1);

    // Find canny edges.
    cv::Mat cannyEdges;
    cv::Canny(image, cannyEdges, 80, 60);

    // Show the images.
    cv::imshow("img", image);
    cv::imshow("cannyEdges", cannyEdges);

    // Find the contours in the canny image.
    std::vector<cv::Vec4i> hierarchy;

    // "Each contour is stored as a vector of points."
    typedef std::vector<std::vector<cv::Point> > TContours;
    TContours contours;
    cv::findContours(cannyEdges, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);
    // cannyEdges is destroyed after calling cv::findContours

    // Print number of found contours.
    std::cout << "Found " << contours.size() << " contours." << std::endl;

    // Convert contours to Components.
    typedef std::vector<Component> TComponents;
    TComponents components;
    for (TContours::const_iterator it(contours.begin()); it != contours.end(); ++it)
    {
        Component c;
        c.area = cv::contourArea(*it);
        c.boundingBox = cv::boundingRect(*it);
        c.circularity = 0.0; // Insert whatever you mean by circularity;
        components.push_back(c);
    }

    for (TComponents::const_iterator it(components.begin()); it != components.end(); ++it)
        std::cout << it->area << std::endl; // and whatever you want.

                                            // Wait for user input.
    cv::waitKey();
}
Andrew Chey
  • 21
  • 1
  • 6
  • What do you mean by *Additional Dependencies - "opencv_world320d.lib" and "opencv_world320.lib"*? I hope you specify only one, depending on Debug/Release mode. – rustyx Sep 28 '17 at 07:37
  • Oh, I meant that I tried both, one at a time, just in case it had something to do with it. However, both did not work. – Andrew Chey Sep 28 '17 at 07:45
  • I'm 85% sure that CV_RETR_CCOMP is not supported yet in opencv 3.2 – Kamil Szelag Sep 28 '17 at 08:11
  • tried others such as CV_RETR_LIST, CV_RETR_TREE, CV_RETR_EXTERNAL, but all three did not work. – Andrew Chey Sep 28 '17 at 08:36
  • Image is of type CV_8UC3. However cv::Canny requires a single-channel 8-bit input image. http://docs.opencv.org/3.2.0/dd/d1a/group__imgproc__feature.html#ga04723e007ed888ddf11d9ba04e2232de – Baiz Sep 28 '17 at 11:21
  • @Baiz, I did check the image type, and I am very sure that it is CV_8UC1. I checked it using a code found in this link. https://stackoverflow.com/questions/10167534/how-to-find-out-what-type-of-a-mat-object-is-with-mattype-in-opencv – Andrew Chey Sep 29 '17 at 00:21
  • @AndrewChey But your image is of type CV_8UC3: cv::Mat image(256, 256, CV_8UC3, cv::Scalar(0, 0, 0)); And you're even drawing a circle with a color value: cv::Scalar(255, 127, 63) - so from this code alone the image has to have 3 channels. – Baiz Sep 29 '17 at 11:28

0 Answers0