How to draw lines between points in OpenCV?

14,590

Solution 1

Using draw contours, you can draw the shape all at once.

img = np.zeros([512, 512, 3],np.uint8)
a = np.array([(375, 193), (364, 113), (277, 20), (271, 16), (52, 106), (133, 266), (289, 296), (372, 282)])
cv2.drawContours(img, [a], 0, (255,255,255), 2)

If you don't want the image closed and want to continue how you started:

image = np.zeros([512, 512, 3],np.uint8)
pointsInside = [(375, 193), (364, 113), (277, 20), (271, 16), (52, 106), (133, 266), (289, 296), (372, 282)]

for index, item in enumerate(pointsInside): 
    if index == len(pointsInside) -1:
        break
    cv2.line(image, item, pointsInside[index + 1], [0, 255, 0], 2) 

Regarding your current code, it looks like you are trying to access the next point by indexing the current point. You need to check for the next point in the original array.

A more Pythonic way of doing the second version would be:

for point1, point2 in zip(a, a[1:]): 
    cv2.line(image, point1, point2, [0, 255, 0], 2) 

Solution 2

If you just want to draw lines, how about cv2.polylines? cv2.drawContours would be preferred when you already have a contours object.

cv2.polylines(image, 
              a, 
              isClosed = False,
              color = (0,255,0),
              thickness = 3, 
              linetype = cv2.LINE_AA)
Share:
14,590

Related videos on Youtube

OPV
Author by

OPV

Updated on June 04, 2022

Comments

  • OPV
    OPV about 2 years

    I have an array of tuples:

    a = [(375, 193)
    (364, 113)
    (277, 20)
    (271, 16)
    (52, 106)
    (133, 266)
    (289, 296)
    (372, 282)]
    

    How to draw lines between points in OpenCV?

    Here is my code that isn't working:

    for index, item in enumerate(a): 
        print (item[index]) 
        #cv2.line(image, item[index], item[index + 1], [0, 255, 0], 2) 
    
    • w-m
      w-m about 6 years
      There's a tutorial in the official documentation for drawing. docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.h‌​tml - what have you tried so far?
    • OPV
      OPV about 6 years
      I know, but tutorial tells to use two points: cv2.line(img,(0,0),(511,511),(255,0,0),5), but I have some points
    • alkasm
      alkasm about 6 years
      You can also draw polygons or contours using your point list. What exactly do you want to achieve?
    • OPV
      OPV about 6 years
      I can use polygon, but I dont need to close polygon
    • Zev
      Zev about 6 years
      Please show some of your code. If you have nothing coded yet, it is hard to meet Stack Overflow's requirements that the question be specific.
    • OPV
      OPV about 6 years
      pointsInside =[] for index, item in enumerate(pointsInside): print (item[index]) #cv2.line(image, item[index], item[index + 1], [0, 255, 0], 2)
  • Zev
    Zev about 6 years
    cv2.drawContours(img, [a], 0, (255,255,255), 2) which I posted is one line as well but I do think your solution is a good fit.
  • bfris
    bfris about 6 years
    @Zev: my bad. I didn't read your code that carefully. cv2.drawContours is probably just as good, especially if you already have a contours object to work with.
  • Martin
    Martin about 5 years
    Great. Doesnt close lines and its loopless
  • spaceman
    spaceman about 4 years
    This worked for me cv2.polylines(image, [np.array(a)], isClosed = False, color = (0,255,0), thickness = 3, linetype = cv2.LINE_AA) a = [(375, 193) (364, 113) (277, 20) (271, 16) (52, 106) (133, 266) (289, 296) (372, 282)]
  • mattsmith5
    mattsmith5 about 3 years
    after you draw the contours, how do you get the all the contours and store them, of what you just drew, while ignoring everything else in the image?
  • mattsmith5
    mattsmith5 about 3 years
    hi, I left a question here, stackoverflow.com/questions/66923488/…
  • bfris
    bfris about 3 years
    @mattsmith5, you can make a blank white image with ` image = 255*np.ones((512,512)) ` for a 512x512 pixel image. Then draw on that.