title: Fine Tuning Hough Line function parameters OpenCV
body: <p>I've been trying to get 4 lines around the square so that I can obtain the vertices of the square. I'm going with this approach rather than finding corners directly using Harris or contours method due to accuracy. Using houghlines in built function in opencv I'm unable to get full length lines to get intersection points and I'm also getting too many irrelevant lines. I'd like to know if the parameters can be fine tuned to obtain my requirements? If yes how do I go about it? My question is exactly the same as this one <a href="https://stackoverflow.com/questions/25303396/choosing-lines-from-hough-lines">here.</a> However I'm not getting those lines itself even after changing those parameters. I've attached the original image along with the code and output:</p>
  
  <p>Original Image:</p>
  
  <p><a href="https://i.stack.imgur.com/3MYBn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3MYBn.png" alt="Original Image"></a></p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;Windows.h&gt;
  #include "opencv2\highgui.hpp"
  #include "opencv2\imgproc.hpp"
  #include "opencv2/imgcodecs/imgcodecs.hpp"
  #include "opencv2/videoio/videoio.hpp"
  
  using namespace cv;
  using namespace std;
  
  int main(int argc, const char** argv)
  {
  
      Mat image,src;
      image = imread("c:/pics/output2_1.bmp");
      src = image.clone();
      cvtColor(image, image, CV_BGR2GRAY);
      threshold(image, image, 0, 255, CV_THRESH_OTSU + CV_THRESH_BINARY_INV);
  
      namedWindow("thresh", WINDOW_NORMAL);
      resizeWindow("thresh", 600, 400);
  
      imshow("thresh", image);
  
      cv::Mat edges;
  
      cv::Canny(image, edges, 0, 255);
  
      vector&lt;Vec2f&gt; lines;
      HoughLines(edges, lines, 1, CV_PI / 180, 100, 0, 0);
      for (size_t i = 0; i &lt; lines.size(); i++)
      {
          float rho = lines[i][0], theta = lines[i][1];
          Point pt1, pt2;
          double a = cos(theta), b = sin(theta);
          double x0 = a*rho, y0 = b*rho;
          pt1.x = cvRound(x0 + 1000 * (-b));
          pt1.y = cvRound(y0 + 1000 * (a));
          pt2.x = cvRound(x0 - 1000 * (-b));
          pt2.y = cvRound(y0 - 1000 * (a));
          line(src, pt1, pt2, Scalar(0, 0, 255), 3, CV_AA);
      }
  
  namedWindow("Edges Structure", WINDOW_NORMAL);
  resizeWindow("Edges Structure", 600, 400);
  
  imshow("Edges Structure", src);
  waitKey(0);
  
  return(0);
  }
  </code></pre>
  
  <p>Output Image:</p>
  
  <p><a href="https://i.stack.imgur.com/CltGI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/CltGI.png" alt="HoughLinesOutput"></a></p>
  
  <p>Update: There is a frame on this image, so I was able to reduce the irrelevant lines in the border of the image by removing that frame, however I'm still not getting complete lines covering the square.</p>