【问题标题】:How to get the middle point of contour heapmap(s)如何获取轮廓堆图的中点
【发布时间】:2021-12-02 13:30:54
【问题描述】:

我想使用 GradCAM 激活来推断图像上的对象位置,如果只有一个轮廓热图,这不是问题,因为我可以简单地使用 argmax 来获得它。但我希望能够获取多个热图,希望通过多个热图我们可以更准确地指出位置。
这是一个例子

import matplotlib.pyplot as plt
activation_map = [[0.0724, 0.0615, 0.0607, 0.0710, 0.0000, 0.0000, 0.0154],
        [0.1111, 0.0835, 0.0923, 0.0409, 0.0000, 0.0000, 0.0000],
        [0.0986, 0.0860, 0.1138, 0.0706, 0.0144, 0.0000, 0.0000],
        [0.1134, 0.1109, 0.2244, 0.3414, 0.2652, 0.2708, 0.1664],
        [0.1165, 0.1620, 0.5605, 0.7064, 0.4593, 0.6628, 0.6103],
        [0.0852, 0.2324, 1.0000, 0.8605, 0.5095, 0.8457, 0.8332],
        [0.0349, 0.2422, 0.9287, 0.5717, 0.2054, 0.4749, 0.6983]]
plt.imshow(activation_map)

在我将此激活图重新缩放到适当缩放后,它看起来像这样

import cv2
activation_map_resized = cv2.resize(np.array(activation_map), (64, 64))
plt.imshow(activation_map_resized)

我希望能够得到 (22, 50) 和 (55, 50) 附近的点。

我可以使用什么方法有效地找到这两个轮廓热图的中心?我可以想象使用渐变或一些聚类,但我不确定它是否是最有效的方法。

【问题讨论】:

    标签: opencv machine-learning deep-learning computer-vision heatmap


    【解决方案1】:

    您正在寻找的是所谓的峰值检测,在您的情况下,您必须首先对数据进行阈值处理,因为梯度中有一些您想忽略的“低”峰值

    import numpy as np
    from scipy.ndimage.filters import maximum_filter
    from scipy.ndimage.morphology import generate_binary_structure, binary_erosion
    import matplotlib.pyplot as pp
    
    def detect_peaks(image):
        neighborhood = generate_binary_structure(2,2)
        local_max = maximum_filter(image, footprint=neighborhood)==image
        background = (image==0)
        eroded_background = binary_erosion(background, structure=neighborhood, border_value=1)
        detected_peaks = local_max ^ eroded_background
        return detected_peaks
    
    
    activation_map = np.array([[0.0724, 0.0615, 0.0607, 0.0710, 0.0000, 0.0000, 0.0154],
                               [0.1111, 0.0835, 0.0923, 0.0409, 0.0000, 0.0000, 0.0000],
                               [0.0986, 0.0860, 0.1138, 0.0706, 0.0144, 0.0000, 0.0000],
                               [0.1134, 0.1109, 0.2244, 0.3414, 0.2652, 0.2708, 0.1664],
                               [0.1165, 0.1620, 0.5605, 0.7064, 0.4593, 0.6628, 0.6103],
                               [0.0852, 0.2324, 1.0000, 0.8605, 0.5095, 0.8457, 0.8332],
                               [0.0349, 0.2422, 0.9287, 0.5717, 0.2054, 0.4749, 0.6983]])
    
    #Thresholding - remove this line to expose "low" peaks
    activation_map[activation_map<0.3] = 0
    detected_peaks = detect_peaks(activation_map)
    pp.subplot(4,2,1)
    pp.imshow(activation_map)
    pp.subplot(4,2,2)
    pp.imshow(detected_peaks)
    pp.show()
    

    有关峰值检测的参考/更详细信息: Peak detection in a 2D array

    【讨论】:

    • 谢谢,我试过了,效果很好,会进一步研究。还要感谢提供额外的资源!
    猜你喜欢
    • 2019-03-27
    • 1970-01-01
    • 2016-06-25
    • 1970-01-01
    • 1970-01-01
    • 2011-08-18
    • 2017-03-11
    • 1970-01-01
    • 2021-02-17
    相关资源
    最近更新 更多