Ask Your Question
0

Fitting lines using cv::goodFeaturesToTrack

asked 2017-10-17 04:56:01 -0600

Ahmed gravatar image

I have got the corner points and I'm trying to fit the lines using cv::fitline but I get lines that are from the origin 0,0 that are shown in the picture.

I also have the projection matrix and the view matrix and the camera intersincs parameters if that would help

I'm trying to compute the volume of the box that is in the figure

enter code here

int main( int argc, char** argv )
{


  Mat src, src_copy, edges, dst;
  src = imread( "freezeFrame__1508152029892.png", 0 );

  src_copy = src.clone();

  GaussianBlur( src, edges, Size( 5, 5 ), 1.5, 1.5 );

  erode( edges, edges, Mat() );// these lines may need to be optimized 
  dilate( edges, edges, Mat() );
  dilate( edges, edges, Mat() );
  erode( edges, edges, Mat() );

  Canny( edges, dst, 1, 10, 3 ); // canny parameters may need to be optimized 
  imshow( "canny", dst );

  std::vector< cv::Point2f > corners;

  // maxCorners – The maximum number of corners to return. If there are more corners
  // than that will be found, the strongest of them will be returned
  int maxCorners = 10;

  // qualityLevel – Characterizes the minimal accepted quality of image corners;
  // the value of the parameter is multiplied by the by the best corner quality
  // measure (which is the min eigenvalue, see cornerMinEigenVal() ,
  // or the Harris function response, see cornerHarris() ).
  // The corners, which quality measure is less than the product, will be rejected.
  // For example, if the best corner has the quality measure = 1500,
  // and the qualityLevel=0.01 , then all the corners which quality measure is
  // less than 15 will be rejected.
  double qualityLevel = 0.01;

  // minDistance – The minimum possible Euclidean distance between the returned corners
  double minDistance = 20.;

  // mask – The optional region of interest. If the image is not empty (then it
  // needs to have the type CV_8UC1 and the same size as image ), it will specify
  // the region in which the corners are detected
  cv::Mat mask;

  // blockSize – Size of the averaging block for computing derivative covariation
  // matrix over each pixel neighborhood, see cornerEigenValsAndVecs()
  int blockSize = 3;

  // useHarrisDetector – Indicates, whether to use operator or cornerMinEigenVal()
  bool useHarrisDetector = false;

  // k – Free parameter of Harris detector
  double k = 0.04;

  cv::goodFeaturesToTrack( src, corners, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k );



  std::vector<Vec4f> lines;
  for ( int i = 0; i < corners.size(); i++ )
  {

    cv::Point2f pt = corners[i];
    for ( int j = i + 1; j < corners.size(); j++ )
    {

      cv::Point2f endpt = corners[j];

      std::vector<cv::Point2f> points;
      points.push_back( pt );
      points.push_back( endpt );


      Vec4f line;
      cv::fitLine( points, line, CV_DIST_L2, 0, 0.01, 0.01 );
      lines.push_back( line );
    }
  }

  for ( size_t i = 0; i < lines.size(); i++ )
  {

    cv::Vec4i v = lines[i];
    line( src, Point( v[0], v[1] ), Point( v[2], v[3] ), Scalar( 0, 0, 255 ), 3, 4 );
  }


  for ( size_t i = 0; i < corners.size(); i++ )
  {
    cv::circle( src, corners[i], 10, cv::Scalar( 255. ), -1 );
  }

  imshow( "line src", src );
  imshow("line dest", edges );
  cv::waitKey( 0 );


  return 0;
}

image description

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
1

answered 2017-10-17 07:29:55 -0600

VxW gravatar image

the drawing of line is not correct. Please read the docu

Result of line is: "Output line parameters. In case of 2D fitting, it should be a vector of 4 elements (like Vec4f) - (vx, vy, x0, y0), where (vx, vy) is a normalized vector collinear to the line and (x0, y0) is a point on the line"

therefore, draw the line using:

int length = 10;
line( src, Point(v[2],v[3]), Point(v[2]+v[0]*length,v[3]+v[1]*length, Scalar( 0, 0, 255 ), 3, 4 );
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-10-17 04:56:01 -0600

Seen: 834 times

Last updated: Oct 17 '17