{"id":846,"date":"2017-06-05T21:26:25","date_gmt":"2017-06-05T19:26:25","guid":{"rendered":"http:\/\/tomaszkacmajor.pl\/?p=846"},"modified":"2020-05-17T15:56:46","modified_gmt":"2020-05-17T13:56:46","slug":"hough-lines-transform-explained","status":"publish","type":"post","link":"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/","title":{"rendered":"Hough Lines Transform Explained"},"content":{"rendered":"\n<p>Hough Lines Transform is the key method used in the previous <a href=\"http:\/\/tomaszkacmajor.pl\/index.php\/2017\/05\/17\/lane-lines-detection\/\">project<\/a> where lane lines are detected. It is very helpful in many Computer Vision applications. The original form of Hough Transform aimed to <strong>identify straight lines<\/strong>. And that&#8217;s what I&#8217;m going to explain today. Furthermore, this technique was later generalized to detect also other shapes like circles, ellipses etc. <a href=\"#GHT\">[1]<\/a>.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h3 class=\"wp-block-heading\">The goal of the method, image preparation<\/h3>\n\n\n\n<p>To use Hough Lines Transform, processed image should be binary. But we would like to search for the straight lines on an original, color image. Therefore, probably the most common solution is to firstly grayscale the image and then to detect edges. Such mask of edges can be then fetched to the Hough Lines method which should output a set of straight lines found on an image.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Straight lines representations<\/h3>\n\n\n\n<p class=\"has-text-align-left\">As we learned from quite early school classes, the straight line can be represented by two parameters. The simplest and most widely used pair of parameters is <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-38afd5fa0311a58706b12abbc68c2d94_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#40;&#97;&#44;&#32;&#98;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"37\" style=\"vertical-align: -4px;\"\/> which correspond to slope and intercept. The line is then described as:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><br><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-062de79f2854a9e353329ee63c41bc27_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#123;&#121;&#32;&#61;&#32;&#97;&#32;&#120;&#32;&#43;&#32;&#98;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"17\" width=\"82\" style=\"vertical-align: -4px;\"\/><\/p>\n\n\n\n<p>Let&#8217;s forget for a while about these parameters. We can also unambiguously describe the line using the pair <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-8a79b6613eafdd3e7aeebb223ecd0f38_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#40;&#92;&#114;&#104;&#111;&#44;&#32;&#92;&#116;&#104;&#101;&#116;&#97;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"38\" style=\"vertical-align: -4px;\"\/> in polar system. The first parameter, <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-da039068127cf2ec5fc05123d4d3546f_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#114;&#104;&#111;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: -4px;\"\/>, is the shortest distance from the origin to the line (approaching the line perpendicularly). The second, <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#116;&#104;&#101;&#116;&#97;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: 0px;\"\/>, is the angle between x-axis and the distance line. One of the benefits of such representation is that we can describe vertical lines by <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-da039068127cf2ec5fc05123d4d3546f_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#114;&#104;&#111;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: -4px;\"\/> and <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#116;&#104;&#101;&#116;&#97;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: 0px;\"\/> which is impossible by using only <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-38afd5fa0311a58706b12abbc68c2d94_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#40;&#97;&#44;&#32;&#98;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"37\" style=\"vertical-align: -4px;\"\/> parameters in cartesian system.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/line.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/line.jpg\" alt=\"Line\" class=\"wp-image-886\" width=\"309\" height=\"216\" srcset=\"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/line.jpg 412w, https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/line-300x210.jpg 300w\" sizes=\"auto, (max-width: 309px) 100vw, 309px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>For a given line, we can determine specific <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-da039068127cf2ec5fc05123d4d3546f_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#114;&#104;&#111;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: -4px;\"\/> and <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#116;&#104;&#101;&#116;&#97;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: 0px;\"\/>. Then, the following equation is satisfied for each <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-c8700e0258243116de0d4f288e2e3b44_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#120;&#95;&#105;\" title=\"Rendered by QuickLaTeX.com\" height=\"11\" width=\"15\" style=\"vertical-align: -3px;\"\/>, <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-bb3c186e5c65fcd066bb23dec8f4e48a_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#121;&#95;&#105;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"14\" style=\"vertical-align: -4px;\"\/> point belonging to this line:<\/p>\n\n\n\n<p class=\"has-text-align-center\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-328384aa8ce2bd34cb9f4772d27cc8d1_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#76;&#65;&#82;&#71;&#69;&#123;&#92;&#114;&#104;&#111;&#32;&#61;&#32;&#120;&#32;&#99;&#111;&#115;&#40;&#92;&#116;&#104;&#101;&#116;&#97;&#41;&#32;&#43;&#32;&#121;&#32;&#115;&#105;&#110;&#40;&#92;&#116;&#104;&#101;&#116;&#97;&#41;&#125;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"168\" style=\"vertical-align: -4px;\"\/><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Mapping from Image space to Hough space<\/h3>\n\n\n\n<p>Let&#8217;s draw a line on an image space again. As we already know, it is represented by some <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-da039068127cf2ec5fc05123d4d3546f_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#114;&#104;&#111;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: -4px;\"\/> and <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#116;&#104;&#101;&#116;&#97;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: 0px;\"\/>. So, we can draw such point in <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-0323ae696d38f2b69cb053de4cd8a12b_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#40;&#92;&#114;&#104;&#111;&#44;&#92;&#116;&#104;&#101;&#116;&#97;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"38\" style=\"vertical-align: -4px;\"\/> coordinates which will be later called a Hough space.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/1-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/1-1.jpg\" alt=\"\" class=\"wp-image-903\" width=\"758\" height=\"308\"\/><\/a><\/figure><\/div>\n\n\n\n<p>Now, in the image space, we are drawing other lines which are intersecting at one common point. Let&#8217;s see what points will be produced in Hough space which are corresponding to these lines.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/2-4.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/2-4.jpg\" alt=\"\" class=\"wp-image-904\" width=\"758\" height=\"308\"\/><\/a><\/figure><\/div>\n\n\n\n<p>It turns out that these points in <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-0323ae696d38f2b69cb053de4cd8a12b_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#40;&#92;&#114;&#104;&#111;&#44;&#92;&#116;&#104;&#101;&#116;&#97;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"38\" style=\"vertical-align: -4px;\"\/> space are forming a sinusoid. Drawing infinite number of additional lines intersecting at this one point would result in a continuous sinusoid in Hough space. So, maybe, we can say that a <strong>point<\/strong> in image space results in a <strong>sinusoid<\/strong> in Hough space? Let&#8217;s recall the equation <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-0329e0866bfafd2b65cdaff09891bfe9_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#114;&#104;&#111;&#32;&#61;&#32;&#120;&#32;&#99;&#111;&#115;&#40;&#92;&#116;&#104;&#101;&#116;&#97;&#41;&#32;&#43;&#32;&#121;&#32;&#115;&#105;&#110;&#40;&#92;&#116;&#104;&#101;&#116;&#97;&#41;&#36;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"168\" style=\"vertical-align: -4px;\"\/>. Indeed, for fixed <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-74548b9e540af56dec9e081ef9fe1475_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#40;&#120;&#95;&#48;&#44;&#32;&#121;&#95;&#48;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"54\" style=\"vertical-align: -4px;\"\/> parameters representing point in image space and sliding through all possible values of <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#116;&#104;&#101;&#116;&#97;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: 0px;\"\/> in some range, we obtain <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-da039068127cf2ec5fc05123d4d3546f_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#114;&#104;&#111;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: -4px;\"\/> values which form a sinusoid.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/4-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/4-1.jpg\" alt=\"\" class=\"wp-image-906\" width=\"758\" height=\"308\"\/><\/a><\/figure><\/div>\n\n\n\n<p>To sum up, we observed following relations:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Image space<\/strong><\/td><td><strong>Hough space<\/strong><\/td><\/tr><tr><td>Straight line<\/td><td>Point<\/td><\/tr><tr><td>Point<\/td><td>Sinusoid<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Finding Hough Lines<\/h3>\n\n\n\n<p>Finally, maybe the most interesting effect. If we draw points which form a line in the image space, we will obtain a bunch of sinusoids in the Hough space. But, magically, they are intersecting at exactly one point!<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/2-2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/2-2.jpg\" alt=\"\" class=\"wp-image-872\" width=\"758\" height=\"326\" srcset=\"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/2-2.jpg 1011w, https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/2-2-300x129.jpg 300w, https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/2-2-768x330.jpg 768w\" sizes=\"auto, (max-width: 758px) 100vw, 758px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>It means that, to identify candidates for being a straight line, we should seek for intersections in Hough space. Below, you can see 2 lines and corresponding image in Hough space. Sure enough, there are 2 main intersections highlighted here for convenience. They are representatives (by <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-da039068127cf2ec5fc05123d4d3546f_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#114;&#104;&#111;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: -4px;\"\/> and <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#116;&#104;&#101;&#116;&#97;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: 0px;\"\/> parameters) of the straight lines.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Hough-example-result-en-1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Hough-example-result-en-1.png\" alt=\"\" class=\"wp-image-938\" width=\"662\" height=\"289\" srcset=\"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Hough-example-result-en-1.png 2648w, https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Hough-example-result-en-1-300x131.png 300w, https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Hough-example-result-en-1-768x335.png 768w, https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Hough-example-result-en-1-1024x446.png 1024w\" sizes=\"auto, (max-width: 662px) 100vw, 662px\" \/><\/a><\/figure><\/div>\n\n\n\n<div style=\"font-size: 10px; text-align: right;\">from <a title=\"\" href=\"https:\/\/en.wikipedia.org\/wiki\/Hough_transform\">en.wikipedia.org\/wiki\/Hough_transform<\/a><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Hough Lines parameters<\/h4>\n\n\n\n<p>In real application, for example using OpenCV library, the Hough space of an image is divided into uniform clusters. The grid is defined by parameters which can be called <em>rho resolution<\/em> and <em>theta resolution<\/em>. They often equals 1 pixel and 1 degree respectively. We sweep through all cells in the grid and count how many votes there are for a certain line. If there are more votes than a given threshold, we claim that the straight line has been found and it&#8217;s described by <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-da039068127cf2ec5fc05123d4d3546f_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#114;&#104;&#111;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: -4px;\"\/> and <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-356a08e839ab6974a16448e16e56745d_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#92;&#116;&#104;&#101;&#116;&#97;\" title=\"Rendered by QuickLaTeX.com\" height=\"12\" width=\"9\" style=\"vertical-align: 0px;\"\/> parameters from the considered cluster.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Implementation in Python<\/h3>\n\n\n\n<p>OK, now we are ready to find Hough Lines in real image using OpenCV and Python. After image loading I perform grayscaling, blurring and edge detection. Then, OpenCV <em>cv2.HoughLines<\/em> function is called to obtain set of detected lines. Next, the results are presented. Note that function <em>cv2.HoughLines<\/em> requires 3 parameters which were discussed before. They were chosen experimentally for the given image example. It&#8217;s good to play with these parameters. E.g. if threshold is lower, more lines will be found and vice versa.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport matplotlib.pyplot as plt\nimport matplotlib.image as mpimg\nimport numpy as np\nimport cv2\n\nimage = mpimg.imread(&quot;test_images\/ppnt.jpg&quot;)\ngray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\nblurred_image = cv2.GaussianBlur(gray_image, (9, 9), 0)\nedges_image = cv2.Canny(blurred_image, 50, 120)\n\nrho_resolution = 1\ntheta_resolution = np.pi\/180\nthreshold = 155\n\nhough_lines = cv2.HoughLines(edges_image, rho_resolution , theta_resolution , threshold)\n\nhough_lines_image = np.zeros_like(image)\ndraw_lines(hough_lines_image, hough_lines)\noriginal_image_with_hough_lines = weighted_img(hough_lines_image,image)\n\nplt.figure(figsize = (30,20))\nplt.subplot(131)\nplt.imshow(image)\nplt.subplot(132)\nplt.imshow(edges_image, cmap='gray')\nplt.subplot(133)\nplt.imshow(original_image_with_hough_lines, cmap='gray')\nplt.show()\n<\/pre><\/pre>\n\n\n\n<p>Below, there are some helper functions. I attach them for a better understanding of the entire code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef draw_lines(img, houghLines, color=&#x5B;0, 255, 0], thickness=2):\n    for line in houghLines:\n        for rho,theta in line:\n            a = np.cos(theta)\n            b = np.sin(theta)\n            x0 = a*rho\n            y0 = b*rho\n            x1 = int(x0 + 1000*(-b))\n            y1 = int(y0 + 1000*(a))\n            x2 = int(x0 - 1000*(-b))\n            y2 = int(y0 - 1000*(a))\n\n            cv2.line(img,(x1,y1),(x2,y2),color,thickness)   \n\ndef weighted_img(img, initial_img, \u03b1=0.8, \u03b2=1., \u03bb=0.):\n    return cv2.addWeighted(initial_img, \u03b1, img, \u03b2, \u03bb)\n<\/pre><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Example results<\/h3>\n\n\n\n<p>Results can be seen below. If there are too much lines in our opinion, or too few of them, we should adjust <em>rho resolution<\/em>, <em>theta resolution<\/em> and <em>threshold<\/em> parameters. But remember also about tuning parameters which are used in blurring and edge detection. Whole pipeline should be revised to fit your needs.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_1_original-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_1_original-1.jpg\" alt=\"\" class=\"wp-image-962\" width=\"675\" height=\"488\" srcset=\"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_1_original-1.jpg 900w, https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_1_original-1-300x217.jpg 300w, https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_1_original-1-768x555.jpg 768w\" sizes=\"auto, (max-width: 675px) 100vw, 675px\" \/><\/a><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_5_edgesImage-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_5_edgesImage-1.jpg\" alt=\"\" class=\"wp-image-963\" width=\"675\" height=\"488\" srcset=\"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_5_edgesImage-1.jpg 900w, https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_5_edgesImage-1-300x217.jpg 300w, https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_5_edgesImage-1-768x555.jpg 768w\" sizes=\"auto, (max-width: 675px) 100vw, 675px\" \/><\/a><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_6_origWithHoughLines-2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_6_origWithHoughLines-2.jpg\" alt=\"\" class=\"wp-image-949\" width=\"675\" height=\"488\"\/><\/a><\/figure><\/div>\n\n\n\n<p>It&#8217;s worth noting that in OpenCV there exists another version of the function to find Hough Lines. It&#8217;s named <em>HoughLines<strong>P<\/strong><\/em>. P suffix stands for <em>probabilistic<\/em> here. It has more efficient implementation and the function outputs extremes of detected lines <img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/tomaszkacmajor.pl\/wp-content\/ql-cache\/quicklatex.com-a0c867c7dcdab3892dca7b78fb946cf5_l3.png\" class=\"ql-img-inline-formula quicklatex-auto-format\" alt=\"&#40;&#120;&#95;&#48;&#44;&#32;&#121;&#95;&#48;&#44;&#32;&#120;&#95;&#49;&#44;&#32;&#121;&#95;&#49;&#41;\" title=\"Rendered by QuickLaTeX.com\" height=\"18\" width=\"104\" style=\"vertical-align: -4px;\"\/> which can be very useful. Below, there is an example of the same image with the straight lines found using <em>HoughLinesP<\/em>.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter is-resized\"><a href=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_6_origWithHoughLines2.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_6_origWithHoughLines2.jpg\" alt=\"\" class=\"wp-image-986\" width=\"675\" height=\"488\" srcset=\"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_6_origWithHoughLines2.jpg 900w, https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_6_origWithHoughLines2-300x217.jpg 300w, https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/ppnt_6_origWithHoughLines2-768x555.jpg 768w\" sizes=\"auto, (max-width: 675px) 100vw, 675px\" \/><\/a><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">References<\/h3>\n\n\n\n<p><br id=\"GHT\">1. Generalized Hough Transform on <a href=\"https:\/\/en.wikipedia.org\/wiki\/Generalised_Hough_transform\" target=\"_blank\" rel=\"noopener noreferrer\">Wikipedia<\/a><br>2. Hough Transform on <a href=\"https:\/\/en.wikipedia.org\/wiki\/Hough_transform\" target=\"_blank\" rel=\"noopener noreferrer\">Wikipedia<\/a><br>3. OpenCV 2.4 <a href=\"http:\/\/docs.opencv.org\/2.4\/doc\/tutorials\/imgproc\/imgtrans\/hough_lines\/hough_lines.html\" target=\"_blank\" rel=\"noopener noreferrer\">documentation<\/a> &#8211; Hough Lines Transform<br>4. OpenCV 3.1 <a href=\"http:\/\/docs.opencv.org\/3.1.0\/d6\/d10\/tutorial_py_houghlines.html&quot;\" target=\"_blank\" rel=\"noopener noreferrer\">documentation<\/a> &#8211; Hough Lines Transform<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hough Lines Transform is the key method used in the previous project where lane lines are detected. It is very helpful in many Computer Vision applications. The original form of Hough Transform aimed to identify straight lines. And that&#8217;s what I&#8217;m going to explain today. Furthermore, this technique was later generalized to detect also other [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":850,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[8,22,21],"class_list":["post-846","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-self-driving-cars","tag-image-processing","tag-opencv","tag-python"],"blocksy_meta":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Hough Lines Transform Explained - tomaszkacmajor.pl<\/title>\n<meta name=\"description\" content=\"Explanation of Hough Lines method used for detecting straight lines on an image. Many visualizations along with implementation in Python and real examples.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Hough Lines Transform Explained - tomaszkacmajor.pl\" \/>\n<meta property=\"og:description\" content=\"Explanation of Hough Lines method used for detecting straight lines on an image. Many visualizations along with implementation in Python and real examples.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/\" \/>\n<meta property=\"og:site_name\" content=\"tomaszkacmajor.pl\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ProggBlogg\/\" \/>\n<meta property=\"article:published_time\" content=\"2017-06-05T19:26:25+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-05-17T13:56:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Clipboard01-1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"561\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"tomasz.kacmajor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@tkacmajor\" \/>\n<meta name=\"twitter:site\" content=\"@tkacmajor\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"tomasz.kacmajor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/\",\"url\":\"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/\",\"name\":\"Hough Lines Transform Explained - tomaszkacmajor.pl\",\"isPartOf\":{\"@id\":\"https:\/\/tomaszkacmajor.pl\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Clipboard01-1.jpg\",\"datePublished\":\"2017-06-05T19:26:25+00:00\",\"dateModified\":\"2020-05-17T13:56:46+00:00\",\"author\":{\"@id\":\"https:\/\/tomaszkacmajor.pl\/#\/schema\/person\/5f40890309a32ae4f63fa6a284215b6c\"},\"description\":\"Explanation of Hough Lines method used for detecting straight lines on an image. Many visualizations along with implementation in Python and real examples.\",\"breadcrumb\":{\"@id\":\"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/#primaryimage\",\"url\":\"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Clipboard01-1.jpg\",\"contentUrl\":\"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Clipboard01-1.jpg\",\"width\":900,\"height\":561},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Strona g\u0142\u00f3wna\",\"item\":\"https:\/\/tomaszkacmajor.pl\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Hough Lines Transform Explained\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/tomaszkacmajor.pl\/#website\",\"url\":\"https:\/\/tomaszkacmajor.pl\/\",\"name\":\"tomaszkacmajor.pl\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/tomaszkacmajor.pl\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/tomaszkacmajor.pl\/#\/schema\/person\/5f40890309a32ae4f63fa6a284215b6c\",\"name\":\"tomasz.kacmajor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/tomaszkacmajor.pl\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/a1fe5c8a80549b9a680c7a6f9ea33a94?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/a1fe5c8a80549b9a680c7a6f9ea33a94?s=96&d=mm&r=g\",\"caption\":\"tomasz.kacmajor\"},\"url\":\"https:\/\/tomaszkacmajor.pl\/index.php\/author\/tomasz-kacmajor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Hough Lines Transform Explained - tomaszkacmajor.pl","description":"Explanation of Hough Lines method used for detecting straight lines on an image. Many visualizations along with implementation in Python and real examples.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/","og_locale":"en_US","og_type":"article","og_title":"Hough Lines Transform Explained - tomaszkacmajor.pl","og_description":"Explanation of Hough Lines method used for detecting straight lines on an image. Many visualizations along with implementation in Python and real examples.","og_url":"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/","og_site_name":"tomaszkacmajor.pl","article_publisher":"https:\/\/www.facebook.com\/ProggBlogg\/","article_published_time":"2017-06-05T19:26:25+00:00","article_modified_time":"2020-05-17T13:56:46+00:00","og_image":[{"width":900,"height":561,"url":"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Clipboard01-1.jpg","type":"image\/jpeg"}],"author":"tomasz.kacmajor","twitter_card":"summary_large_image","twitter_creator":"@tkacmajor","twitter_site":"@tkacmajor","twitter_misc":{"Written by":"tomasz.kacmajor","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/","url":"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/","name":"Hough Lines Transform Explained - tomaszkacmajor.pl","isPartOf":{"@id":"https:\/\/tomaszkacmajor.pl\/#website"},"primaryImageOfPage":{"@id":"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/#primaryimage"},"image":{"@id":"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/#primaryimage"},"thumbnailUrl":"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Clipboard01-1.jpg","datePublished":"2017-06-05T19:26:25+00:00","dateModified":"2020-05-17T13:56:46+00:00","author":{"@id":"https:\/\/tomaszkacmajor.pl\/#\/schema\/person\/5f40890309a32ae4f63fa6a284215b6c"},"description":"Explanation of Hough Lines method used for detecting straight lines on an image. Many visualizations along with implementation in Python and real examples.","breadcrumb":{"@id":"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/#primaryimage","url":"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Clipboard01-1.jpg","contentUrl":"https:\/\/tomaszkacmajor.pl\/wp-content\/uploads\/2017\/06\/Clipboard01-1.jpg","width":900,"height":561},{"@type":"BreadcrumbList","@id":"https:\/\/tomaszkacmajor.pl\/index.php\/2017\/06\/05\/hough-lines-transform-explained\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Strona g\u0142\u00f3wna","item":"https:\/\/tomaszkacmajor.pl\/"},{"@type":"ListItem","position":2,"name":"Hough Lines Transform Explained"}]},{"@type":"WebSite","@id":"https:\/\/tomaszkacmajor.pl\/#website","url":"https:\/\/tomaszkacmajor.pl\/","name":"tomaszkacmajor.pl","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/tomaszkacmajor.pl\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/tomaszkacmajor.pl\/#\/schema\/person\/5f40890309a32ae4f63fa6a284215b6c","name":"tomasz.kacmajor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/tomaszkacmajor.pl\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/a1fe5c8a80549b9a680c7a6f9ea33a94?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a1fe5c8a80549b9a680c7a6f9ea33a94?s=96&d=mm&r=g","caption":"tomasz.kacmajor"},"url":"https:\/\/tomaszkacmajor.pl\/index.php\/author\/tomasz-kacmajor\/"}]}},"_links":{"self":[{"href":"https:\/\/tomaszkacmajor.pl\/index.php\/wp-json\/wp\/v2\/posts\/846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tomaszkacmajor.pl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tomaszkacmajor.pl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tomaszkacmajor.pl\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tomaszkacmajor.pl\/index.php\/wp-json\/wp\/v2\/comments?post=846"}],"version-history":[{"count":134,"href":"https:\/\/tomaszkacmajor.pl\/index.php\/wp-json\/wp\/v2\/posts\/846\/revisions"}],"predecessor-version":[{"id":1703,"href":"https:\/\/tomaszkacmajor.pl\/index.php\/wp-json\/wp\/v2\/posts\/846\/revisions\/1703"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tomaszkacmajor.pl\/index.php\/wp-json\/wp\/v2\/media\/850"}],"wp:attachment":[{"href":"https:\/\/tomaszkacmajor.pl\/index.php\/wp-json\/wp\/v2\/media?parent=846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tomaszkacmajor.pl\/index.php\/wp-json\/wp\/v2\/categories?post=846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tomaszkacmajor.pl\/index.php\/wp-json\/wp\/v2\/tags?post=846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}