在图像上,我正在尝试为json文件中的给定注释数据绘制自由线。线条应以坐标为输入并用自由线画出图像。
import numpy as np
im = np.zeros([800, 1216],np.uint8)
points = [
[405.49313, 141.8587],[444.5172, 135.35468],
[444.5172, 135.35468],[ 509.44876, 128.50587],
[509.44876, 128.50587],[ 541.78784, 127.92772],
[541.78784, 127.92772],[ 561.9265, 129.86807],
[561.9265, 129.86807],[ 580.7606, 130.36917],
[580.7606, 130.36917],[ 605.0242, 127.64725],
[605.0242, 127.64725],[ 623.9638, 125.8712],
[623.9638, 125.8712],[ 638.4798, 122.56262],
[638.4798, 122.56262],[ 652.50616, 119.102554],
[652.50616, 119.102554],[ 666.3739, 116.87822],
[666.3739, 116.87822],[ 679.47644, 116.87822],
[679.47644, 116.87822],[ 695.8207, 116.87822],
[695.8207, 116.87822],[ 703.9951, 116.25368],
[703.9951, 116.25368],[ 713.08514, 114.554535],
[713.08514, 114.554535],[ 719.17285, 112.84549],
[719.17285, 112.84549],[ 724.0949, 110.882904],
[724.0949, 110.882904],[ 729.0887, 109.88368],
[729.0887, 109.88368],[ 736.0799, 105.88681]
]
cv2.drawContours(im, [points], 3, (0,255,0), 3)
cv2.imshow("",im)
cv2.waitKey(0)
我希望图像具有自由线条!其中线条重叠而不与图像绑定(bind)。
最佳答案
您可以使用polylines
结果:
码:
import cv2
import numpy as np
# create the background, with 3 color channels
im = np.zeros([800, 1216, 3],np.uint8)
# create a numpy array with coordinates, dtype= np.uint32
points = np.array([
[405.49313, 141.8587],[444.5172, 135.35468],
[444.5172, 135.35468],[ 509.44876, 128.50587],
[509.44876, 128.50587],[ 541.78784, 127.92772],
[541.78784, 127.92772],[ 561.9265, 129.86807],
[561.9265, 129.86807],[ 580.7606, 130.36917],
[580.7606, 130.36917],[ 605.0242, 127.64725],
[605.0242, 127.64725],[ 623.9638, 125.8712],
[623.9638, 125.8712],[ 638.4798, 122.56262],
[638.4798, 122.56262],[ 652.50616, 119.102554],
[652.50616, 119.102554],[ 666.3739, 116.87822],
[666.3739, 116.87822],[ 679.47644, 116.87822],
[679.47644, 116.87822],[ 695.8207, 116.87822],
[695.8207, 116.87822],[ 703.9951, 116.25368],
[703.9951, 116.25368],[ 713.08514, 114.554535],
[713.08514, 114.554535],[ 719.17285, 112.84549],
[719.17285, 112.84549],[ 724.0949, 110.882904],
[724.0949, 110.882904],[ 729.0887, 109.88368],
[729.0887, 109.88368],[ 736.0799, 105.88681]
], np.int32)
# reshape array
points = points.reshape((-1,1,2))
# draw a line between coordinates
im = cv2.polylines(im,[points],True,(0,0,255))
# show image
cv2.imshow("",im)
cv2.waitKey(0)
cv2.destroyAllWindows()
关于python - 如何使用drawcontours绘制给定坐标的自由线?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56326908/