多尺度掩模图像质心问题
mutil-scale mask image mass center problem
现在我想使用 python3 cv2 lib 找到二进制掩码的质心(力矩),正如你在这个 same mask mutil-scale pic 中看到的,蓝色的是原始大小的掩码,紫色和绿色的是缩小的,我猜如果我继续缩小蓝色面具,所有较小版本的面具最终都会收敛到一个中心,所有较小版本面具的质心也会移动,这就是我想要的,所以可以在opencv中实现它python?
我该如何称呼这个中心?
非常感谢!
我认为您可以从 the distance map
的最大区域中获取您的 center
,如下所示:
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()
现在我想使用 python3 cv2 lib 找到二进制掩码的质心(力矩),正如你在这个 same mask mutil-scale pic 中看到的,蓝色的是原始大小的掩码,紫色和绿色的是缩小的,我猜如果我继续缩小蓝色面具,所有较小版本的面具最终都会收敛到一个中心,所有较小版本面具的质心也会移动,这就是我想要的,所以可以在opencv中实现它python? 我该如何称呼这个中心?
非常感谢!
我认为您可以从 the distance map
的最大区域中获取您的 center
,如下所示:
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()