0

I am trying to run a code on lane detection using opencv library in python. But I am getting a numpy.ndarray error:

TypeError: 'numpy.ndarray' object is not callable
 File "lane_detect.py", line 10, in <module>
    line(img,(x1,y1),(x2,y2),(0,255,0),2)

Here is the code I am trying to run:

from cv2 import * 
from numpy import *
from matplotlib import pyplot as plt

img = imread("Lane1.png", 0)
edges = Canny(img,100,200)
lines = HoughLinesP(edges,1,pi/180,100,minLineLength=100,maxLineGap=10)
for line in lines:
    x1,y1,x2,y2 = line[0]
    line(img,(x1,y1),(x2,y2),(0,255,0),2)

'''for line in lines:
   for ex in line:
    print ex'''
imwrite('Lane1.png', img)
namedWindow("Image", WINDOW_AUTOSIZE)
namedWindow("Canny", WINDOW_AUTOSIZE)
imshow("Canny", edges)
imshow("Image", img)
waitKey(0)
destroyAllWindows()

There might be other errors in the code.

Please suggest some changes in the code.

4
  • What are you trying to achieve with line(img,(x1,y1),(x2,y2),(0,255,0),2)?
    – languitar
    Commented Feb 23, 2017 at 15:00
  • You can refer to the openCV documentation if you want to understand houghlines. I am trying to construct a houghline. Commented Feb 23, 2017 at 15:28
  • Are you able to recognize the lanes in your image??
    – Jeru Luke
    Commented Feb 23, 2017 at 16:33
  • Yeah. But due to lack of understanding of opencv in python, I coded the same in C++. After that the whole structure of the code has changed... Commented Feb 24, 2017 at 17:02

2 Answers 2

2

I think you are misunderstanding what is happening there. lines is the list of detected Hough Lines. You are not constructing them, but they are detected by the algorithm. I think in the loop you are trying to draw them onto the image, but a name collision happens here. the line variable from the for-loop overrides the cv2.line function which you probably want to use to draw the line. So either select a different variable name, or call cv2.line for drawing.

2
  • I see you have figured out the same solution as well :D
    – Jeru Luke
    Commented Feb 23, 2017 at 15:36
  • Perfect example of shooting oneself in the foot due to lazines (to type cv2.). :D
    – Dan Mašek
    Commented Feb 23, 2017 at 17:54
0

Replace the 10th line in your code to:

cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

You want to draw the lines you have found using HoughLinesP(). Hence cv2.line() does it for you. You might have overlooked it while coding

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.