为了账号安全,请及时绑定邮箱和手机立即绑定

循环遍历像素图像以找到该像素与任意值最接近的值

循环遍历像素图像以找到该像素与任意值最接近的值

宝慕林4294392 2023-08-08 16:09:41
问题陈述:在 yolo 中成功获取对象周围的边界框后,我想将背景与对象本身分开。我的解决方案:我有一个 RGB-D 相机,它返回深度图以及图像(图像提供给 yolo obv),使用深度图,我做了一个简单的函数来获取深度(四舍五入)和多少像素具有相同的值def GetAllDepthsSortedMeters(depth_image_ocv):    _depth = depth_image_ocv[np.isfinite(depth_image_ocv)]    _depth= -np.sort(-depth_image_ocv)[:int(len(_depth)/2)]    _depth=  np.round(_depth,1)    unique, counts = np.unique(_depth, return_counts=True)    return dict(zip(counts, unique))并绘制它们,我注意到有一些主峰,其余的位于它们周围,经过一些过滤后,我每次都能成功获得这些峰。    #get the values of depths and their number of occurences    counts,values = GetKeysAndValues(_depths)    #find the peaks of depths in those values    peaks = find_peaks_cwt(counts, widths=np.ones(counts.shape)*2)-1使用这些峰值,我能够通过检查该值接近哪些峰值来从背景中分割所需的对象,并为每个峰值(及其周围的像素)制作一个掩模。def GetAcceptedMasks(h,w,depth_map,depths_of_accepted_peaks,accepted_masks):    prev=None    prev_index=None    for pos in product(range(h), range(w)):        pixel = depth_map.item(pos)        if ( (prev is not None) and (round(prev,1) == round(pixel,1)) ):                accepted_masks[prev_index][pos[0],pos[1]]= 255        else:            _temp_array    = abs(depths_of_accepted_peaks-pixel)            _min           = np.amin(_temp_array)            _ind           = np.where( _temp_array == _min )[0][0]            accepted_masks[_ind][pos[0],pos[1]]= 255            prev_index = _ind            prev = pixel    return accepted_masks将图像传递给 YOLOv3 并应用过滤和深度分割后,需要 0.8 秒,这远非最佳,这主要是上述函数的结果,任何帮助都会很棒。谢谢
查看完整描述

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 均值聚类。



查看完整回答
反对 回复 2023-08-08
  • 1 回答
  • 0 关注
  • 170 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号