1 回答
TA贡献1757条经验 获得超8个赞
这是一种方法。
制作一个与图像具有相同高度和宽度的浮点数组,并且最终尺寸等于您要识别的唯一深度的数量
在每个像素位置,计算到三个所需深度中每个深度的距离并存储在最终尺寸中
用于
np.argmin(..., axis=2)选择三个深度中最接近的深度
我不是在计算机上进行测试,您的图像不是您的实际图像,而是带有窗口装饰、标题栏和不同值的图片,但如下所示:
import cv2
# Load the image as greyscale float - so we can store positive and negative distances
im = cv2.imread('depth.png', cv2.IMREAD_GRAYSCALE).astype(np.float)
# Make list of the desired depths
depths = [255, 181, 125]
# Make array with distance to each depth
d2each = np.zeros(((im.shape[0],im.shape[1],len(depths)), dtype=np.float)
for i in range(len(depths)):
d2each[...,i] = np.abs(im - depths[i])
# Now let Numpy choose nearest of three distances
mask = np.argmin(d2each, axis=2)
另一种方法是范围测试距离。加载图像如上:
# Make mask of pixels matching first distance
d0 = np.logical_and(im>100, im<150)
# Make mask of pixels matching second distance
d1 = np.logical_and(im>180, im<210)
# Make mask of pixels matching third distance
d2 = im >= 210
这些蒙版将是合乎逻辑的(即 True/False),但如果您想让它们变成黑白,只需将它们乘以 255 并使用mask0 = d0.astype(np.uint8)
另一种方法可能是使用K 均值聚类。
添加回答
举报
