Quantcast
Channel: OpenCV Q&A Forum - RSS feed
Viewing all articles
Browse latest Browse all 41027

Why do I get different Length and Area for identical objects

$
0
0
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 #include #include using namespace cv; using namespace std; Mat src; Mat srcGray; RNG rng(12345); int 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> contours; vector hierarchy; findContours(cannyOut, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0)); // Get the moments vector mu(contours.size()); for (int i = 0; i < contours.size(); i++) { mu[i] = moments(contours[i], false); } // Get the mass centers: vector 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](/upfiles/14459416058441928.png) Contours and mass center: ![image description](/upfiles/14459416608556099.png)

Viewing all articles
Browse latest Browse all 41027

Trending Articles