如何使用drawcontours绘制给定坐标的自由线?

时间:2019-05-27 13:19:20

标签: python opencv

在图像上,我正在尝试为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)

我希望图像具有自由的线条!线条重叠而不与图像绑定。

1 个答案:

答案 0 :(得分:1)

您可以使用polylines

结果:
enter image description here

代码:

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()