Ask Your Question
0

Problem with contourArea return value

asked 2016-10-01 10:39:02 -0600

esteban.da gravatar image

Hello, I'm trying to use the contourArea fonction in ordre to remove the "too small" shapes but when I'm running the above code I'm obtaining this king of results : "contour 1 color 125 area 1" or more generally "countour n color x area n". So si there someone having an idea about why I obtain the number of the contour instead of the real area delimitate by the contour ?

Thank you all by advance

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

void drawStuff(int, void*);
void showInputWindow();
void showCannyWindow();
void showContourWindow();

int lowThreshold;
int const max_lowThreshold = 100;
const char* window_name = "Edge Map";

int thresh = 40;
int max_thresh = 120;
Mat img_rgb,img_gray,img_bw,canny_output,drawing;

int main(int argc, char *argv[]){
    img_rgb  = imread(argv[1]);
    blur( img_rgb, img_rgb, Size(3,3) );
    namedWindow( window_name, WINDOW_AUTOSIZE );
    cv::createTrackbar( "Min Threshold:", window_name, &lowThreshold, max_lowThreshold, drawStuff );

    drawStuff(0,0);
    cv::waitKey(0);
}

void drawStuff(int, void*){
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    Canny( img_rgb, canny_output, lowThreshold, lowThreshold*2, 3 );
    cv::dilate(canny_output, canny_output, cv::Mat(), cv::Point(-1,-1));
    showCannyWindow();

    findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
    drawing = Mat::zeros( canny_output.size(), CV_8UC3 );

    vector<Point> approxShape;
    vector<double> areas;
    for(size_t i = 0; i < contours.size(); i++){
            approxPolyDP(contours[i], approxShape, arcLength(Mat(contours[i]), true)*0.04, true);
        areas.push_back(contourArea(contours[i],false));
            drawContours(drawing, contours, i, Scalar((i+1)*10%255, (i+1)*10%255, (i+1)*10%255), CV_FILLED);   // fill one gray per contour
        printf("contour %d color %d area%d\n",i,(i+i)*10%255,areas[i]);
    }
    //cv::fastNlMeansDenoising(img_rgb,contours,3,7,21);

    showContourWindow();
}

void showInputWindow(){
    cv::namedWindow("InputImage");
    cv::imshow("InputImage",img_rgb);
}

void showCannyWindow(){
    cv::namedWindow("Canny");
    cv::imshow("Canny",canny_output);
}
void showContourWindow(){
    cv::namedWindow("Fill");
    cv::imshow("Fill",drawing);
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-10-01 10:52:29 -0600

berak gravatar image

updated 2016-10-01 11:05:13 -0600

you're simply not seeing the correct result, because of an error in your printf statement.

contourArea() actually returns a double, not an int. please change your printf, so it uses %f, not %d for area

edit flag offensive delete link more

Comments

can anyone reply how does this contourArea() function works ?is there any formula behind it to get values in double

deesha gravatar imagedeesha ( 2017-07-06 07:34:18 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-10-01 10:39:02 -0600

Seen: 1,381 times

Last updated: Oct 01 '16