Ask Your Question

Problem with contourArea return value

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

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


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

    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);
            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]);


void showInputWindow(){

void showCannyWindow(){
void showContourWindow(){
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

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

berak gravatar image

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

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


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 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


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

Seen: 621 times

Last updated: Oct 01 '16