Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV: findContours function error

I am using 2.4.3 version of opencv, and trying to use "findContours" function after canny edge detection like this:

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

cv::vector < Component > components;
cv::vector < cv::Vec4i > hierarchy;
cv::findContours ( cannyEdges, components, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_NONE);

Then it throws an error for line line "cv::findContours" like this:

OpenCV Error: Assertion failed (mtype == type0 || ( CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1((type0) & fixedDepthMask) != 0 )) in unknown function, file ...\opencv\modeuls\core\src\matrix.cpp, line 1421

How can I solve this?

like image 619
E_learner Avatar asked Nov 30 '12 14:11

E_learner


People also ask

What is cv2 findContours?

Output: We see that there are three essential arguments in cv2. findContours() function. First one is source image, second is contour retrieval mode, third is contour approximation method and it outputs the image, contours, and hierarchy. 'contours' is a Python list of all the contours in the image.

How to detect contour in OpenCV?

Use the findContours() function to detect the contours in the image. Draw Contours on the Original RGB Image.

What are contours in image processing?

Contours in image processing. Contours are designed using edges. They are edges with an identity, geometrical parameters and are continuous. They are useful for shape analysis and object recognition.


1 Answers

cv::findcontours returns every contour as a vector of points (see http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#findcontours).

You have to convert these vectors to your data structure (Component) by yourself like in this minimal example I created:

#include <opencv2/opencv.hpp>
#include <iostream>
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.
    cv::vector<cv::Vec4i> hierarchy;

    // "Each contour is stored as a vector of points."
    // http://docs.opencv.org/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#findcontours
    typedef cv::vector<cv::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 cv::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();
}
like image 185
Tobias Hermann Avatar answered Sep 29 '22 05:09

Tobias Hermann