【问题标题】:Getting the coordinates for the "hottest areas" on a numpy heatmap在 numpy 热图中获取“最热区域”的坐标
【发布时间】:2018-03-26 17:56:30
【问题描述】:

我有一个形状为 (600,400) 的热图 numpy 数组。热图表示检测概率。就我而言,图像中人脸检测的概率。我的目标是获取此热图并获取最高概率出现的坐标(X 和 Y)。

我已经解决了单面的情况。代码如下:

face_location = np.unravel_index(heatmap.argmax(), heatmap.shape)
    print("Face location: " + str(face_location))

但在某些情况下有多个面孔。我不知道如何调整算法以返回多个“最热区域”。问题是任何一个热点区域都会被逐渐不那么热点的区域包围。所以很有可能在最热的区域之后,接下来的前10名都在初始点的旁边。

如何调整算法以查找多个热点区域?可以假设它们不会并排在一起。

heatmap = [[  2.00299415e-04   2.03753079e-04   8.17560707e-04 ...,   2.23556344e-04
         1.98958180e-04   9.92935777e-01]
      [  2.00642273e-04   2.04473894e-04   8.19963054e-04 ...,   2.24148811e-04
         1.99438742e-04   9.92921114e-01]
      [  2.01056406e-04   2.05344462e-04   8.22864589e-04 ...,   2.24864416e-04
         2.00019145e-04   9.92903233e-01]
      ..., 
      [  7.28193991e-05  -2.73474743e-05   2.95096161e-05 ...,   5.96550672e-05
         1.98282614e-05   9.99637246e-01]
      [  7.34055429e-05  -2.72389279e-05   3.02382941e-05 ...,   5.98490733e-05
         2.04356711e-05   9.99619305e-01]
      [  7.37556256e-05  -2.71740992e-05   3.06735128e-05 ...,   5.99649393e-05
         2.07984649e-05   9.99608397e-01]]

【问题讨论】:

标签: image numpy image-processing computer-vision conv-neural-network


【解决方案1】:

或许可以考虑使用mask array 来定义热点区域的阈值概率?

In [29]: threshold_probability = 0.8

In [30]: prng = np.random.RandomState(42)

In [31]: heatmap = prng.rand(600, 400)

In [32]: heatmap
Out[32]: 
array([[ 0.37454012,  0.95071431,  0.73199394, ...,  0.42899403,
         0.75087107,  0.75454287],
       [ 0.10312387,  0.90255291,  0.50525237, ...,  0.56513318,
         0.69665082,  0.92249938],
       [ 0.70723863,  0.15253904,  0.57628836, ...,  0.96887786,
         0.74965183,  0.13008624],
       ..., 
       [ 0.77669933,  0.98757844,  0.72686576, ...,  0.149866  ,
         0.6685433 ,  0.90248875],
       [ 0.116007  ,  0.96352904,  0.33109138, ...,  0.85776718,
         0.88838363,  0.00901272],
       [ 0.30810176,  0.43190563,  0.60935151, ...,  0.07498895,
         0.60716006,  0.31712892]])

In [33]: hottest_areas = np.ma.MaskedArray(heatmap, heatmap < threshold_probability)

In [34]: X, Y = hottest_areas.nonzero()

In [35]: X
Out[35]: array([  0,   0,   0, ..., 599, 599, 599])

In [36]: Y
Out[36]: array([  1,   7,  11, ..., 376, 388, 394])

结果是一个元组,其中包含定义掩码的布尔条件为 False 的值的 x 和 y 坐标(即面部概率高于阈值的区域)。

【讨论】:

    【解决方案2】:

    如果您想采用像 davidrpugh 提议的阈值,我可以提出不同的方法。 无需查找非零元素,只需查找二进制图像的连接组件即可。

    import numpy as np
    from scipy.ndimage.measurements import label
    from skimage.measure import regionprops
    
    heatmap = np.random.rand(100, 25)
    
    thresh = 0.9
    bw = np.array(heatmap)
    bw[bw < thresh] = 0
    
    img_cc, nb_cc = label(bw)
    cc = regionprops(img_cc)
    
    face_location = np.array([c.centroid for c in cc])
    
    import matplotlib.pyplot as plt
    
    plt.figure()
    plt.imshow(heatmap)
    plt.plot(face_location[:, 1], face_location[:, 0], 'r*')
    
    plt.figure()
    plt.imshow(img_cc)
    plt.plot(face_location[:, 1], face_location[:, 0], 'r*')
    
    plt.show()
    

    面部位置在这里由连接组件的中心定义,但您可以寻找图像中每个区域的最大值。

    【讨论】:

      猜你喜欢
      • 2018-06-03
      • 1970-01-01
      • 1970-01-01
      • 2013-08-29
      • 2010-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-18
      相关资源
      最近更新 更多