Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Why do I get different Length and Area for identical objects

Hello,

I would like to compare two objects' area and length. I just simply find the contours and use arcLength() and contourArea() functions but the results are way too different.

Why would that be?

Also, mass center of the upper one seems a little off from the center. What would the reason be?

I see contours are well-drawn, and the shapes are rectangle-ish. Therefore, I don't see what could the reason be.

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

using namespace cv;
using namespace std;

Mat src; Mat srcGray;

int main()
{
    // Load source image and convert it to gray
    src = imread("source.png", 1);

    // Convert image to gray and blur it
    cvtColor(src, srcGray, CV_BGR2GRAY);
    blur(srcGray, srcGray, Size(3, 3));

    Mat cannyOut;
    Canny(srcGray, cannyOut, 187, 187, 3, 1);

    // Find contours
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    findContours(cannyOut, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    for (int i = 0; i < contours.size(); i++)
    {
        cout << "Contour: " << i << " Area: " << contourArea(contours[i]) << " Length: " << arcLength(contours[i], true) << "\n";
    }

    waitKey(0);
    return(0);
}

Output:

Contour: 0 Area: 39951.5 Length: 837.124
Contour: 1 Area: 16 Length: 1645.37

Source:

image description

Contours and mass center: image description

Why do I get different Length and Area for identical objects

Hello,

I would like to compare two objects' area and length. I just simply find the contours and use arcLength() and contourArea() functions but the results are way too different.

Why would that be?

Also, mass center of the upper one seems a little off from the center. What would the reason be?

I see contours are well-drawn, and the shapes are rectangle-ish. Therefore, I don't see what the reason could the reason be.

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

using namespace cv;
using namespace std;

Mat src; Mat srcGray;

int main()
{
    // Load source image and convert it to gray
    src = imread("source.png", 1);

    // Convert image to gray and blur it
    cvtColor(src, srcGray, CV_BGR2GRAY);
    blur(srcGray, srcGray, Size(3, 3));

    Mat cannyOut;
    Canny(srcGray, cannyOut, 187, 187, 3, 1);

    // Find contours
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    findContours(cannyOut, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    for (int i = 0; i < contours.size(); i++)
    {
        cout << "Contour: " << i << " Area: " << contourArea(contours[i]) << " Length: " << arcLength(contours[i], true) << "\n";
    }

    waitKey(0);
    return(0);
}

Output:

Contour: 0 Area: 39951.5 Length: 837.124
Contour: 1 Area: 16 Length: 1645.37

Source:

image description

Contours and mass center: image description

Why do I get different Length and Area for identical objects

Hello,

I would like to compare two objects' area and length. I just simply find the contours and use arcLength() and contourArea() functions but the results are way too different.

Why would that be?

Also, mass center of the upper one seems a little off from the center. What would the reason be?

I see contours are well-drawn, and the shapes are rectangle-ish. Therefore, I don't see what the reason could be.

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

using namespace cv;
using namespace std;

Mat src; Mat srcGray;
RNG rng(12345);

int main()
main(int argc, char** argv)
{
    // Load source image and convert it to gray
    src = imread("source.png", 1);

    // Convert image to gray and blur it
    cvtColor(src, srcGray, CV_BGR2GRAY);
    blur(srcGray, srcGray, Size(3, 3));

    Mat cannyOut;
    Canny(srcGray, cannyOut, 187, 187, 3, 1);

    // Find contours
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

    findContours(cannyOut, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

    // Get the moments
    vector<Moments> mu(contours.size());
    for (int i = 0; i < contours.size(); i++)
    {
        mu[i] = moments(contours[i], false);
    }

    //  Get the mass centers:
    vector<Point2f> mc(contours.size());
    for (int i = 0; i < contours.size(); i++)
    {
        mc[i] = Point2f(mu[i].m10 / mu[i].m00, mu[i].m01 / mu[i].m00);
    }

    // Draw contours
    Mat drawing = Mat::zeros(cannyOut.size(), CV_8UC3);
    for (int i = 0; i< contours.size(); i++)
    {
        Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
        drawContours(drawing, contours, i, color, 2, 8, hierarchy, 0, Point());
        circle(drawing, mc[i], 4, color, -1, 8, 0);
    }

    for (int i = 0; i < contours.size(); i++)
    {
        cout << "Contour: " << i << " Area: " << contourArea(contours[i]) << " Length: " << arcLength(contours[i], true) << "\n";
    }

    imshow("Contours", drawing);

    waitKey(0);
    return(0);
}

Output:

Contour: 0 Area: 39951.5 Length: 837.124
Contour: 1 Area: 16 Length: 1645.37

Source:

image description

Contours and mass center: image description