Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Fitting lines using cv::goodFeaturesToTrack

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