#include <iostream>
int thresh = 100;
int max_thresh = 255;
void thresh_callback(int, void* );
int main( int argc, char** argv )
{
CommandLineParser parser( argc, argv,
"{@input | ../data/stuff.jpg | input image}" );
{
cout << "Could not open or find the image!\n" << endl;
cout << "usage: " << argv[0] << " <Input image>" << endl;
exit(0);
}
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;
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 );
}
}