四边形顶点排序算法
将四边形顶点按照一定的算法进行排序
顺时针顺序进行排序
预先得到四个点的坐标或者没有也行,通过opencv画出矩形框并使用opencv自带的算法进行多边形取点,取完点之后 findCounters所得到的坐标点是逆时针排序的,所以如果使用顺时针进行排序则需要进一步对点进行处理
label=[x1,y1,x2,y2,x3,y3,x4,y4]
points = [label[0],label[1],label[2],label[3],label[4],label[5],label[6],label[7]]
points = list(map(int,points))
max_x=max(int(label[0]),int(label[0]),int(label[4]),int(label[6]))+100
max_y=max(int(label[1]),int(label[3]),int(label[5]),int(label[7]))+100
mask=np.zeros((max_y,max_x,3)).astype(np.uint8)
points_1=list(map(int,[label[0],label[1],label[2],label[3],label[6],label[7],label[4],label[5]]))
points_2=list(map(int,[label[0],label[n1],label[4],label[5],label[2],label[3],label[6],label[7]]))
points_3=list(map(int,[label[0],label[1],label[4],label[5],label[6],label[7],label[2],label[3]]))
points_4=list(map(int,[label[0],label[1],label[6],label[7],label[2],label[3],label[4],label[5]]))
points_5=list(map(int,[label[0],label[1],label[6],label[7],label[n4],label[5],label[2],label[3]]))
points = np.int32(points).reshape(4, 2)
points_1 = np.int32(points_1).reshape(4, 2)
points_2 = np.int32(points_2).reshape(4, 2)
points_3 = np.int32(points_3).reshape(4, 2)
points_4 = np.int32(points_4).reshape(4, 2)
points_5 = np.int32(points_5).reshape(4, 2)
cv2.fillPoly(mask, [points], (255,255,255))
cv2.fillPoly(mask, [points_1], (255, 255, 255))
cv2.fillPoly(mask, [points_2], (255,255,255))
cv2.fillPoly(mask, [points_3], (255,255,255))
cv2.fillPoly(mask, [points_4], (255,255,255))
cv2.fillPoly(mask, [points_5], (255,255,255))
mask=cv2.cvtColor(mask,cv2.COLOR_RGB2GRAY)
contours, hierarchy = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = cv2.approxPolyDP(contours[0], 5, True)
contours=sort_contours(contours)
print(contours)