0

Hi I am trying to run a simple OpenCV program to detect contours in a picture. however, findcontours keeps throwing exceptions.

here is my code:

#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <opencv2\opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;

/** @function main */
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"


#include <math.h>
#include <string.h>
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, const char * argv[]) {

    IplImage* img = cvLoadImage("C:\\Users\\310217955\\Documents\\Visual Studio 2010\\Projects\\aviTest\\Debug\\Pictures\\Capture.jpg");
 cv::Mat image = cv::cvarrToMat(img);
    if (!image.data) {
        std::cout << "Image file not found\n";
        return 1;
    }

    //Prepare the image for findContours
    cv::cvtColor(image, image, CV_BGR2GRAY);
    cv::threshold(image, image, 128, 255, CV_THRESH_BINARY);

    //Find the contours. Use the contourOutput Mat so the original image doesn't get overwritten
    //std::vector<std::vector<cv::Point> > contours;
 vector<cv::Mat> contours;
    cv::Mat contourOutput = image.clone();
    cv::findContours( contourOutput, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE );

    //Draw the contours
    cv::Mat contourImage(image.size(), CV_8UC3, cv::Scalar(0,0,0));
    cv::Scalar colors[3];
    colors[0] = cv::Scalar(255, 0, 0);
    colors[1] = cv::Scalar(0, 255, 0);
    colors[2] = cv::Scalar(0, 0, 255);
    for (size_t idx = 0; idx < contours.size(); idx++) {
        cv::drawContours(contourImage, contours, idx, colors[idx % 3]);
    }

    cv::imshow("Input Image", image);
    cvMoveWindow("Input Image", 0, 0);
    cv::imshow("Contours", contourImage);
    cvMoveWindow("Contours", 200, 0);
    cv::waitKey(0);
 system("PAUSE");
    return 0;
}

here is an image of the exception:

Exception thrown

how can I get around this? is there a dll file im missing or some settings i need to resolve?

I did the following changes to the code

vector<Vec4i> hierarchy;
findContours( contourOutput, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);

but now I get the following exception:

enter image description here

Adhil
  • 1,678
  • 3
  • 20
  • 31
  • Please do not use depreciated c API's! Please refer the sample code samples in the c++ directory and revert back! – Balaji R Nov 21 '17 at 07:00
  • I do not understand, my current opencv version is 2.4.9 – Adhil Nov 21 '17 at 07:07
  • Please refer this tutorial for further quries https://docs.opencv.org/2.4/doc/tutorials/imgproc/shapedescriptors/find_contours/find_contours.html#find-contours – Balaji R Nov 21 '17 at 07:13
  • how did you get past "error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "StdAfx.h"' to your source? " – Adhil Nov 21 '17 at 07:55
  • Why include same headers multiple times? Why multiple identical `using namespace`? (In fact, [why any](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)?) | Is your copy of OpenCV built with/for MSVC++ 10.0 (i.e. MSVS 2010)? Are you linking with a library built for the "mode" you're in (Debug/Release)? – Dan Mašek Nov 21 '17 at 12:24

1 Answers1

1

Well your code works perfectly fine.I only commented out the header files stdafx.h and nonfree.hpp in my project file . This is the output i gotenter image description here

Make sure you providing the correct path to the input img or paste your input image in your visual studio solution directory and just load the image directly.

The problem may be with one of the modules. I would suggest reinstalling opencv(try downloading the latest version) and try again. Make sure you are adding xyzd.lib files like opencv_highgui330d.lib opencv_imgproc330d.lib in linker>>Input>> additional dependencies. the version might differ for you. Hope this helps you!

  • I previously used opencv 2.4.9, now I downloaded and installed opencv 2.4.10. but findcontours still has issues. I am using older versions because my Development tool is Visual studio 2010 – Adhil Nov 21 '17 at 08:36
  • I dont think the version of opencv has anything to do with the visual studio version. In case it does you might want to consider upgrading your visual studio along with opencv. Staying upgraded will always help you in your future projects. – Abhilesh Borode Nov 21 '17 at 09:43