boolean fitLine ( int contourIndex0 , int contourIndex1 , LineGeneral2D_F64 line ) { int numPixels = CircularIndex . distanceP ( contourIndex0 , contourIndex1 , contour . size ( ) ) ; // if its too small if ( numPixels < minimumLineLength ) return false ; Point2D_I32 c0 = contour . get ( contourIndex0 ) ; Point2D_I32 c1 = contour . get ( contourIndex1 ) ; double scale = c0 . distance ( c1 ) ; double centerX = ( c1 . x + c0 . x ) / 2.0 ; double centerY = ( c1 . y + c0 . y ) / 2.0 ; int numSamples = Math . min ( maxSamples , numPixels ) ; pointsFit . reset ( ) ; for ( int i = 0 ; i < numSamples ; i ++ ) { int index = i * ( numPixels - 1 ) / ( numSamples - 1 ) ; Point2D_I32 c = contour . get ( CircularIndex . addOffset ( contourIndex0 , index , contour . size ( ) ) ) ; Point2D_F64 p = pointsFit . grow ( ) ; p . x = ( c . x - centerX ) / scale ; p . y = ( c . y - centerY ) / scale ; } if ( null == FitLine_F64 . polar ( pointsFit . toList ( ) , linePolar ) ) { return false ; } UtilLine2D_F64 . convert ( linePolar , line ) ; // go from local coordinates into global line . C = scale * line . C - centerX * line . A - centerY * line . B ; return true ; }
Given a sequence of points on the contour find the best fit line .