Opencv python HoughLinesP strange results -


i'm trying same result got in this tutorial houghlinesp filter. took same images , same threshold values :

import cv2 line import line import numpy np  img = cv2.imread('building.jpg',1) cannied = cv2.canny(img, 50, 200, 3) lines = cv2.houghlinesp(cannied, 1, np.pi / 180, 80, 30, 10)   leftx, boty, rightx, topy in lines[0]:     line = line((leftx, boty), (rightx,topy))     line.draw(img, (255, 255, 0), 2)  cv2.imwrite('lines.png',img) cv2.imwrite('canniedhouse.png',cannied) cv2.waitkey(0) cv2.destroyallwindows() 

line class custom class not interesting calculates stuff , can draw line. , these 2 images : enter image description here enter image description here

so can see 1 litle line in midle of image.

not sure what's going wrong. did miss thing?

thanks.

note: since linked tutorial opencv 2.4.x, assumed wrote code opencv 2.4.11. turns out, you're using opencv 3.x. keep in mind there subtle changes in api between 2.x , 3.x.


you call houghlinesp incorrectly.

according documentation, signature of python function is:

cv2.houghlinesp(image, rho, theta, threshold[, lines[, minlinelength[, maxlinegap]]]) → lines 

if label parameters in call, following:

lines = cv2.houghlinesp(cannied, rho=1, theta=np.pi / 180     , threshold=80, lines=30, minlinelength=10) 

however, c++ code correctly ported python be

lines = cv2.houghlinesp(cannied, rho=1, theta=np.pi / 180     , threshold=80, minlinelength=30, maxlinegap=10) 

result


similar situation canny

cv2.canny(image, threshold1, threshold2[, edges[, aperturesize[, l2gradient]]]) → edges 

again, let's label parameters:

cannied = cv2.canny(img, threshold1=50, threshold2=200, edges=3) 

but should be:

cannied = cv2.canny(img, threshold1=50, threshold2=200, aperturesize=3) 

however makes no difference in output, since default value aperturesize 3.


finally, identified vasanth , namatoj, there difference in format of output generated cv2.houghlinesp:

  • in 2.4 looks [[[x1, y1, x2, y2], [...], ..., [...]]]
  • in 3.x looks [[[x1, y1, x2, y2]], [[...]], ..., [[...]]]

i added short get_lines function transform lines consistent layout ([[x1, y1, x2, y2], [...], ..., [...]]) in both versions.


full script works in both opencv versions:

import cv2 import numpy np   def get_lines(lines_in):     if cv2.__version__ < '3.0':         return lines_in[0]     return [l[0] l in lines]   img = cv2.imread('building.jpg') img_gray = gray = cv2.cvtcolor(img, cv2.color_bgr2gray)  cannied = cv2.canny(img_gray, threshold1=50, threshold2=200, aperturesize=3) lines = cv2.houghlinesp(cannied, rho=1, theta=np.pi / 180, threshold=80, minlinelength=30, maxlinegap=10)  line in get_lines(lines):     leftx, boty, rightx, topy = line     cv2.line(img, (leftx, boty), (rightx,topy), (255, 255, 0), 2)  cv2.imwrite('lines.png',img) cv2.imwrite('canniedhouse.png',cannied) cv2.waitkey(0) cv2.destroyallwindows() 

Comments