eccentricity ( elongation ) of contours

asked 2014-10-14 06:42:43 -0500

fedor gravatar image

updated 2014-10-14 07:12:47 -0500

Hi ! I'm trying to calculate eccentricity of finded conturs. I's works with circles and elips ( not ideally ), but gives extremele low raiting to lines with angle like this '/' ( I draw some images in paint :) ) And for line with angle like this '\' it's gives extremele hige rate.

http://www.cs.cf.ac.uk/Dave/Vision_lecture/node36.html

lowRate.jpg highRate.jpg

double getEccentricity(Moments &mu){
    double bigSqrt = sqrt( ( mu.m20 - mu.m02 ) *  ( mu.m20 - mu.m02 )  + 4 * mu.m11 * mu.m11  );
    return (double) ( mu.m20 + mu.m02 + bigSqrt ) / ( mu.m20 + mu.m02 - bigSqrt );
}
void tryElongation(){
    Mat img;
    img = imread("5.jpg");
    cvtColor(img, img, CV_RGB2GRAY);
    close( &img );
    imshow( "img", img );
    RNG rng(12345);
    Mat drawing = Mat::zeros( img.size(), CV_8UC3 );
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;
    findContours( img, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_TC89_L1, Point(0, 0) );
    for( int i = 0; i< contours.size(); i++ ){
        //show conturs
        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() );
        Moments mu = moments( contours[i] , 1 ); // bool 1 ???   
        double eccentricity = getEccentricity( mu ); 
        cout << eccentricity << " " ;
    }
    namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
    imshow( "Contours", drawing );
    cv::waitKey(2000);  
}
edit retag flag offensive close merge delete

Comments

work for me :

double eccentricity2( vector<Point> &contour ){ RotatedRect ellipse = fitEllipse( contour ); return ellipse.size.height / ellipse.size.width; }

( it's not normalize + maybe width/heigth )

fedor gravatar imagefedor ( 2014-10-14 09:22:18 -0500 )edit