坐标旋转计算
主要是在做数据增强时,需要进行轮廓旋转操作,但对于多轮廓的旋转只能通过自定义函数进行计算,这里主要是平移坐标原点至中心,旋转,再将原点平移至左上角,具体代码如下:
def rotateContours(image, degree, Contours):
center = int(image.shape[0]/2), int(image.shape[1]/2)
hc, wc = center*2
R = cv2.getRotationMatrix2D(center, degree, 1.0)
image = cv2.warpAffine(image, R, (wc, hc))
Contours = Contours.reshape(-1, 2)
Contours = Contours*[1, -1] + [-center[1], center[0]]
rotate_Contours = np.dot(Contours, np.array([R[0,0], R[0,1], -R[0,1], R[0,0]]).reshape(2,2))
rotated_Contours = rotate_Contours*[1, -1] + [center[1], center[0]]
return image, np.array(rotated_Contours, dtype=int32)
def drawContours(image, Contours):
# the Contours contain multi contour
cnts = list()
for contour in Contours:
contour=np.array(contour).reshape(-1, 1, 2)
cnts.append(contour)
cv2.drawContours(image, cnts, -1, (0, 255, 0), 2)
return image