主要也就是用了个一阶中心矩和二阶中心矩计算了一下质心和方向,主要用在印章上面,算是一个思路吧,毕竟还是需要调参的,鲁棒性比较一般。
用这种方式可以来稍微矫正一下图像,方便后期的处理。
主要代码如下:
#img为二值图像
M = cv2.moments(img)
cx = int(M['m10']/M['m00'])
cy = int(M['m01']/M['m00'])
cv2.circle(img, (cx, cy), 10, (0, 0, 255), -1)
cv2.imshow('th',img)
cv2.waitKey()
a = int(M['mu20']/M['m00'])
b = int(M['mu11']/M['m00'])
c = int(M['mu02']/M['m00'])
square = math.sqrt(4 * b * b + (a - c) * (a - c))
theta = math.atan2(2 * b, a - c + square)*180/math.pi
cv2.line(img,(cx, cy),(cx+int(30*math.cos(theta)), cy+int(30*math.sin(theta))),(255, 0, 0),4)
cv2.imshow('th',img)
cv2.waitKey()