def drawColoredTriangles(img, triangleList, disp):
#sort the triangle list by distance from the top left corner in order to get a gradient effect when drawing triangles
triangleList=sorted(triangleList, cmp=triDistanceSort)
h, w, c = img.shape
#get bounding rectangle points of image
r = (0, 0, w, h)
#iterate through and draw all triangles in the list
for idx, t in enumerate(triangleList):
#grab individual vertex points
pt1 = [t[0], t[1]]
pt2 = [t[2], t[3]]
pt3 = [t[4], t[5]]
#select a position for displaying the enumerated triangle value
pos = (t[2], t[3])
#create the triangle
triangle = np.array([pt1, pt2, pt3], np.int32)
#select a color in HSV!! (manipulate idx for cool color gradients)
color = np.uint8([[[idx, 100, 200]]])
#color = np.uint8([[[0, 0, idx]]])
#convert color to BGR
bgr_color = cv2.cvtColor(color, cv2.COLOR_HSV2BGR)
color = (int(bgr_color[(0, 0, 0)]), int(bgr_color[(0, 0, 1)]), int(bgr_color[(0, 0, 2)]))
#draw the triangle if it is within the image bounds
if rect_contains(r, pt1) and rect_contains(r, pt2) and rect_contains(r, pt3):
cv2.fillPoly(img, [triangle], color)
# if display triangle number was selected, display the number.. this helps with triangle manipulation later
if(disp==1):
cv2.putText(img, str(idx), pos, fontFace=cv2.FONT_HERSHEY_SCRIPT_SIMPLEX, fontScale=0.3, color=(0, 0, 0))
######################################## example script ########################################
faceWarp.py 文件源码
python
阅读 30
收藏 0
点赞 0
评论 0
评论列表
文章目录