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

问题陈述:在 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 秒,这远非最佳,这主要是上述函数的结果,任何帮助都会很棒。谢谢


宝慕林4294392
浏览 171回答 1
1回答

陪伴而非守候

这是一种方法。制作一个与图像具有相同高度和宽度的浮点数组,并且最终尺寸等于您要识别的唯一深度的数量在每个像素位置,计算到三个所需深度中每个深度的距离并存储在最终尺寸中用于np.argmin(..., axis=2)选择三个深度中最接近的深度我不是在计算机上进行测试,您的图像不是您的实际图像,而是带有窗口装饰、标题栏和不同值的图片,但如下所示:import cv2# Load the image as greyscale float - so we can store positive and negative distancesim = cv2.imread('depth.png', cv2.IMREAD_GRAYSCALE).astype(np.float)# Make list of the desired depthsdepths = [255, 181, 125]# Make array with distance to each depthd2each = 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 distancesmask = 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 均值聚类。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python