多尺度掩模图像质心问题

2024-06-25 07:23:01 发布

您现在位置:Python中文网/ 问答频道 /正文

现在我想用python3cv2lib来找到一个二元掩模的质心(矩),正如你在这个same mask mutil-scale pic中看到的,蓝色的是原始尺寸的掩模,紫色和绿色的是缩小的掩模,我猜如果我继续缩小蓝色掩模,所有较小的掩模最终都会收敛到一个中心,所有较小版本的掩码的质心也会移动,这就是我想要的,所以有可能在opencv python中实现它吗? 我怎么称呼这个中心

非常感谢


Tags: 版本尺寸mask中心蓝色samescale绿色
1条回答
网友
1楼 · 发布于 2024-06-25 07:23:01

我想你可以从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 

enter image description here

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

相关问题 更多 >