It is really well documented how to draw a polygon using opencv:
import numpy as np
import cv2 as cv
img = np.zeros((125, 240, 3), dtype = "uint8")
poly = np.array([[[20,15],[210,30],[220,100],[20,100]]], np.int32)
cv.polylines(img, [poly], True, (0,0,255), 1)
cv.imshow('Shapes', img)
cv.waitKey(0)
cv.destroyAllWindows()
My question is, how do I get the data of the polygon. I need a (n, 2) or (1, n, 2) sized array with all the pixels the red line "follows".
The best sollution I've got so far is filling the poly and getting the contour from that:
mask = np.zeros((125, 240), dtype = "uint8")
cv.fillPoly(mask, poly, 255)
contours, _ = cv.findContours(mask, cv.RETR_TREE, cv.CHAIN_APPROX_NONE)
contour = contours[0][:, 0, :]
# ->
# array([[20, 15],
# [20, 16],
# [20, 17],
# ...,
# [23, 15],
# [22, 15],
# [21, 15]], dtype=int32)
I wonder if I can't get this data from cv.polylines if OpenCV is already able to display the polygon?