opencv矩計算圖形主方向

主要也就是用了個一階中心矩和二階中心矩計算了一下質心和方向,主要用在印章上面,算是一個思路吧,畢竟還是需要調參的,魯棒性比較一般。

用這種方式可以來稍微矯正一下圖像,方便後期的處理。

主要代碼如下:

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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章