二维阵列中的峰值检测 [英] Peak detection in a 2D array

查看:34
本文介绍了二维阵列中的峰值检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在帮助一家兽医诊所测量狗爪下的压力.我使用 Python 进行数据分析,现在我一直在尝试将爪子划分为(解剖学)子区域.

我制作了每个爪子的 2D 阵列,其中包含爪子随时间加载的每个传感器的最大值.这是一个爪子的示例,我使用 Excel 绘制了我想要检测"的区域.这些是传感器周围 2 x 2 的具有局部最大值的框,它们一起具有最大的总和.

所以我尝试了一些实验并决定简单地寻找每一列和每一行的最大值(由于爪子的形状,不能朝一个方向看).这似乎可以很好地检测"单独脚趾的位置,但它也标记了相邻的传感器.

那么告诉 Python 哪些最大值是我想要的最好方法是什么?

注意:2x2 方块不能重叠,因为它们必须是分开的脚趾!

此外,我采用 2x2 为方便起见,欢迎任何更高级的解决方案,但我只是一名人体运动科学家,所以我既不是真正的程序员也不是数学家,所以请保持简单".

>

这是一个 和 我得出了一个惊人的结论:几乎所有我的小型犬都没有找到第五个脚趾,而且在超过 50% 的大型犬的影响中,它会找到更多!

很明显我需要改变它.我自己的猜测是将 neighborhood 的大小更改为小型犬的较小和大型犬的较大.但是 generate_binary_structure 不允许我改变数组的大小.

因此,我希望其他人对定位脚趾有更好的建议,也许脚趾面积与爪子大小成比例?

解决方案

我使用局部最大值过滤器检测到峰值.这是您的第一个 4 个爪子数据集的结果:

我还在 9 个爪子的第二个数据集上运行了它,它也运行良好.

这是你的做法:

将 numpy 导入为 np从 scipy.ndimage.filters 导入maximum_filter从 scipy.ndimage.morphology 导入 generate_binary_structure, binary_erosion将 matplotlib.pyplot 导入为 pp#出于某种原因,我不得不重塑.Numpy 忽略了形状标题.paws_data = np.loadtxt("paws.txt").reshape(4,11,14)#获取图像列表paws = [p.squeeze() for p in np.vsplit(paws_data,4)]定义检测峰值(图像):"""拍摄图像并使用局部最大值滤波器检测峰值.返回峰值的布尔掩码(即 1 当像素的值为邻域最大值,否则为 0)"""# 定义一个 8 连通的邻域邻域 = generate_binary_structure(2,2)#应用局部最大过滤器;最大值的所有像素#在他们的邻居中设置为1local_max = maximum_filter(image,footprint=neighborhood)==image#local_max 是一个包含我们所在峰值的掩码#寻找,也是背景.#为了隔离峰值,我们必须从蒙版中去除背景.#我们创建背景的蒙版背景 = (图像==0)#有点技术性:我们必须侵蚀背景才能#成功地从local_max中减去它,否则会出现一行#沿着背景边框出现(局部最大值过滤器的产物)侵蚀背景=二元侵蚀(背景,结构=邻域,边界值=1)#我们得到最终的掩码,只包含峰值,#通过从local_max掩码中去除背景(异或操作)detection_peaks = local_max ^ eroded_background返回detected_peaks#应用检测和绘图结果对于我,爪子枚举(爪子):检测峰值 = 检测峰值(爪子)pp.subplot(4,2,(2*i+1))pp.imshow(爪子)pp.subplot(4,2,(2*i+2))pp.imshow(detected_peaks)pp.show()

之后您需要做的就是在掩码上使用 scipy.ndimage.measurements.label 标记所有不同的对象.然后你就可以单独和他们一起玩了.

注意该方法效果很好,因为背景没有噪音.如果是这样,您会在背景中检测到一堆其他不需要的峰值.另一个重要因素是社区的规模.如果峰大小发生变化,您将需要对其进行调整(应保持大致成比例).

I'm helping a veterinary clinic measuring pressure under a dogs paw. I use Python for my data analysis and now I'm stuck trying to divide the paws into (anatomical) subregions.

I made a 2D array of each paw, that consists of the maximal values for each sensor that has been loaded by the paw over time. Here's an example of one paw, where I used Excel to draw the areas I want to 'detect'. These are 2 by 2 boxes around the sensor with local maxima's, that together have the largest sum.

So I tried some experimenting and decide to simply look for the maximums of each column and row (can't look in one direction due to the shape of the paw). This seems to 'detect' the location of the separate toes fairly well, but it also marks neighboring sensors.

So what would be the best way to tell Python which of these maximums are the ones I want?

Note: The 2x2 squares can't overlap, since they have to be separate toes!

Also I took 2x2 as a convenience, any more advanced solution is welcome, but I'm simply a human movement scientist, so I'm neither a real programmer or a mathematician, so please keep it 'simple'.

Here's a version that can be loaded with np.loadtxt


Results

So I tried @jextee's solution (see the results below). As you can see, it works very on the front paws, but it works less well for the hind legs.

More specifically, it can't recognize the small peak that's the fourth toe. This is obviously inherent to the fact that the loop looks top down towards the lowest value, without taking into account where this is.

Would anyone know how to tweak @jextee's algorithm, so that it might be able to find the 4th toe too?

Since I haven't processed any other trials yet, I can't supply any other samples. But the data I gave before were the averages of each paw. This file is an array with the maximal data of 9 paws in the order they made contact with the plate.

This image shows how they were spatially spread out over the plate.

Update:

I have set up a blog for anyone interested and I have setup a OneDrive with all the raw measurements. So to anyone requesting more data: more power to you!


New update:

So after the help I got with my questions regarding paw detection and paw sorting, I was finally able to check the toe detection for every paw! Turns out, it doesn't work so well in anything but paws sized like the one in my own example. Off course in hindsight, it's my own fault for choosing the 2x2 so arbitrarily.

Here's a nice example of where it goes wrong: a nail is being recognized as a toe and the 'heel' is so wide, it gets recognized twice!

The paw is too large, so taking a 2x2 size with no overlap, causes some toes to be detected twice. The other way around, in small dogs it often fails to find a 5th toe, which I suspect is being caused by the 2x2 area being too large.

After trying the current solution on all my measurements I came to the staggering conclusion that for nearly all my small dogs it didn't find a 5th toe and that in over 50% of the impacts for the large dogs it would find more!

So clearly I need to change it. My own guess was changing the size of the neighborhood to something smaller for small dogs and larger for large dogs. But generate_binary_structure wouldn't let me change the size of the array.

Therefore, I'm hoping that anyone else has a better suggestion for locating the toes, perhaps having the toe area scale with the paw size?

解决方案

I detected the peaks using a local maximum filter. Here is the result on your first dataset of 4 paws:

I also ran it on the second dataset of 9 paws and it worked as well.

Here is how you do it:

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

#for some reason I had to reshape. Numpy ignored the shape header.
paws_data = np.loadtxt("paws.txt").reshape(4,11,14)

#getting a list of images
paws = [p.squeeze() for p in np.vsplit(paws_data,4)]


def detect_peaks(image):
    """
    Takes an image and detect the peaks usingthe local maximum filter.
    Returns a boolean mask of the peaks (i.e. 1 when
    the pixel's value is the neighborhood maximum, 0 otherwise)
    """

    # define an 8-connected neighborhood
    neighborhood = generate_binary_structure(2,2)

    #apply the local maximum filter; all pixel of maximal value 
    #in their neighborhood are set to 1
    local_max = maximum_filter(image, footprint=neighborhood)==image
    #local_max is a mask that contains the peaks we are 
    #looking for, but also the background.
    #In order to isolate the peaks we must remove the background from the mask.

    #we create the mask of the background
    background = (image==0)

    #a little technicality: we must erode the background in order to 
    #successfully subtract it form local_max, otherwise a line will 
    #appear along the background border (artifact of the local maximum filter)
    eroded_background = binary_erosion(background, structure=neighborhood, border_value=1)

    #we obtain the final mask, containing only peaks, 
    #by removing the background from the local_max mask (xor operation)
    detected_peaks = local_max ^ eroded_background

    return detected_peaks


#applying the detection and plotting results
for i, paw in enumerate(paws):
    detected_peaks = detect_peaks(paw)
    pp.subplot(4,2,(2*i+1))
    pp.imshow(paw)
    pp.subplot(4,2,(2*i+2) )
    pp.imshow(detected_peaks)

pp.show()

All you need to do after is use scipy.ndimage.measurements.label on the mask to label all distinct objects. Then you'll be able to play with them individually.

Note that the method works well because the background is not noisy. If it were, you would detect a bunch of other unwanted peaks in the background. Another important factor is the size of the neighborhood. You will need to adjust it if the peak size changes (the should remain roughly proportional).

这篇关于二维阵列中的峰值检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆