#include <iostream>
#include <stdio.h>
#include <stdlib.h>
int thresh = 100;
int max_thresh = 255;
void thresh_callback(int, void* );
int main( int, char** argv )
{
const char* source_window = "Source";
createTrackbar(
" Canny thresh:",
"Source", &thresh, max_thresh, thresh_callback );
thresh_callback( 0, 0 );
return(0);
}
void thresh_callback(int, void* )
{
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
Canny( src_gray, canny_output, thresh, thresh*2, 3 );
vector<Moments> mu(contours.size() );
for( size_t i = 0; i < contours.size(); i++ )
{ mu[
i] =
moments( contours[i],
false ); }
vector<Point2f> mc( contours.size() );
for( size_t i = 0; i < contours.size(); i++ )
{ mc[
i] =
Point2f( static_cast<float>(mu[i].m10/mu[i].m00) , static_cast<float>(mu[i].m01/mu[i].m00) ); }
for( size_t i = 0; i< contours.size(); i++ )
{
Scalar color =
Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
circle( drawing, mc[i], 4, color, -1, 8, 0 );
}
imshow(
"Contours", drawing );
printf("\t Info: Area and Contour Length \n");
for( size_t i = 0; i< contours.size(); i++ )
{
printf(
" * Contour[%d] - Area (M_00) = %.2f - Area OpenCV: %.2f - Length: %.2f \n", (
int)i, mu[i].m00,
contourArea(contours[i]),
arcLength( contours[i],
true ) );
Scalar color =
Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
circle( drawing, mc[i], 4, color, -1, 8, 0 );
}
}