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:
Contours and mass center: