Python Opencv: Draw Lines Between Points And Find Full Contours
I have a set of three points, in a triangle. eg: [[390 37], [371 179], [555 179]] After drawing the lines, How to draw lines between points in OpenCV? How do I find the full Conto
Solution 1:
I believe your main issue is that you can only find contours on a binary image (not color). Your points also need to be in a Numpy array (as x,y pairs -- note the comma). So here is how I did it in Python/OpenCV.
import cv2
import numpy as np
# create black background image
img = np.zeros((500, 1000), dtype=np.uint8)
# define triangle points
points = np.array( [[390,37], [371,179], [555,179]], dtype=np.int32 )
# draw triangle polygon on copy of input
triangle = img.copy()
cv2.polylines(triangle, [points], True, 255, 1)
# find the external contours
cntrs = cv2.findContours(triangle, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cntrs = cntrs[0] iflen(cntrs) == 2else cntrs[1]
# get the single contour of the triangle
cntr = cntrs[0]
# draw filled contour on copy of input
triangle_filled = img.copy()
cv2.drawContours(triangle_filled, [cntr], 0, 255, -1)
# save results
cv2.imwrite('triangle.jpg', triangle)
cv2.imwrite('triangle_filled.jpg', triangle_filled)
cv2.imshow('triangle', triangle)
cv2.imshow('triangle_filled', triangle_filled)
cv2.waitKey()
Triangle Polygon Image:
Triangle Filled Contour Image:
Post a Comment for "Python Opencv: Draw Lines Between Points And Find Full Contours"