Zara
Zara

Reputation: 146

how to draw free hand line for given coordinates using drawcontours?

On image, i am trying to draw free hand line for given annotation data in json file. Line should be drawn by taking coordinates as a input and return image with free hand line.

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)

I expect image with free hand line!where line is overlay not binding with image.

Upvotes: 0

Views: 366

Answers (1)

J.D.
J.D.

Reputation: 4561

You can use polylines

Result:
enter image description here

Code:

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

Upvotes: 1

Related Questions