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)
Related videos on Youtube
![OPV](https://i.stack.imgur.com/GjzJH.png?s=256&g=1)
Author by
OPV
Updated on June 04, 2022Comments
-
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 about 6 yearsThere's a tutorial in the official documentation for drawing. docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html - what have you tried so far?
-
OPV about 6 yearsI 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 about 6 yearsYou can also draw polygons or contours using your point list. What exactly do you want to achieve?
-
OPV about 6 yearsI can use polygon, but I dont need to close polygon
-
Zev about 6 yearsPlease 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 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 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 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 about 5 yearsGreat. Doesnt close lines and its loopless
-
spaceman about 4 yearsThis 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 about 3 yearsafter 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 about 3 yearshi, I left a question here, stackoverflow.com/questions/66923488/…
-
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.