现在我想使用python3 cv2 lib查找二进制掩码的质量中心(矩),如您在此same mask mutil-scale pic中所见,蓝色是原始大小的掩码,紫色和绿色是那些是缩小的,我想如果我继续缩小蓝色蒙版,所有较小版本的蒙版最终都会收敛到一个中心,所有较小版本的蒙版的质心也将偏移,要,那么可以在opencv python中实现它吗? 以及我如何称呼这个中心?
非常感谢!
答案 0 :(得分:0)
我认为您可以从center
的最大区域中获取the distance map
,如下所示:
left: your origin image
mid: distmap, blur for the border, purple for the center, green for moments center
right: display in color
# 2018.09.23 12:00 (CST)
import numpy as np
import cv2
img = cv2.imread("masks.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
th, threshed = cv2.threshold(gray, 100, 255, cv2.THRESH_OTSU|cv2.THRESH_BINARY_INV)
dist = cv2.distanceTransform(threshed, cv2.DIST_C, 3, None, cv2.CV_32F)
## Notice, I just find the first max of the max-regions in the distmap
xid = dist.argmax()
nh, nw = img.shape[:2]
cx = xid%nw
cy = xid//nw
print((cx, cy))
cv2.circle(img, (cx,cy), 5, purple, -1, cv2.LINE_AA)
cv2.imshow("img", img)
cv2.waitKey()
cv2.destroyAllWindows()