3 回答

TA贡献1951条经验 获得超3个赞
解
数据文件:paw.txt。源代码:
from scipy import *from operator import itemgetter n = 5 # how many fingers are we looking ford = loadtxt("paw.txt")width, height = d.shape# Create an array where every element is a sum of 2x2 squares.fourSums = d[:-1,:-1] + d[1:,:-1] + d[1:,1:] + d[:-1,1:]# Find positions of the fingers.# Pair each sum with its position number (from 0 to width*height-1),pairs = zip(arange(width*height), fourSums.flatten())# Sort by descending sum value, filter overlapping squaresdef drop_overlapping(pairs): no_overlaps = [] def does_not_overlap(p1, p2): i1, i2 = p1[0], p2[0] r1, col1 = i1 / (width-1), i1 % (width-1) r2, col2 = i2 / (width-1), i2 % (width-1) return (max(abs(r1-r2),abs(col1-col2)) >= 2) for p in pairs: if all(map(lambda prev: does_not_overlap(p,prev), no_overlaps)): no_overlaps.append(p) return no_overlaps pairs2 = drop_overlapping(sorted(pairs, key=itemgetter(1), reverse=True))# Take the first n with the heighest valuespositions = pairs2[:n]# Print resultsprint d, "\n"for i, val in positions: row = i / (width-1) column = i % (width-1) print "sum = %f @ %d,%d (%d)" % (val, row, column, i) print d[row:row+2,column:column+2], "\n"
输出没有重叠方块。似乎在您的示例中选择了相同的区域。
一些评论
棘手的部分是计算所有2x2平方的总和。我假设你需要所有这些,所以可能会有一些重叠。我使用切片从原始2D数组中剪切第一个/最后一个列和行,然后将它们重叠在一起并计算总和。
为了更好地理解它,对3x3阵列进行成像:
>>> a = arange(9).reshape(3,3) ; a array([[0, 1, 2], [3, 4, 5], [6, 7, 8]])
然后你可以拿它的切片:
>>> a[:-1,:-1]array([[0, 1], [3, 4]])>>> a[1:,:-1]array([[3, 4], [6, 7]])>>> a[:-1,1:]array([[1, 2], [4, 5]])>>> a[1:,1:]array([[4, 5], [7, 8]])
现在想象一下,将它们叠加在另一个之上,并将元素加在相同位置。这些总和与2x2正方形的总和相同,左上角位于同一位置:
>>> sums = a[:-1,:-1] + a[1:,:-1] + a[:-1,1:] + a[1:,1:]; sums array([[ 8, 12], [20, 24]])
当你有过的2x2平方的总和,可以使用max
以找到最大,或者sort
,或者sorted
找到峰值。
为了记住峰值的位置,我将每个值(总和)与其在平顶阵列中的序数位置耦合(参见参考资料zip
)。然后我打印结果时再次计算行/列位置。
笔记
我允许2x2方格重叠。编辑版本过滤掉其中一些版本,使得结果中仅出现非重叠的正方形。
选择手指(一个想法)
另一个问题是如何从所有峰中选择可能是手指的东西。我有一个想法可能会或可能不会奏效。我现在没时间实现它,所以只是伪代码。
我注意到如果前手指几乎保持完美的圆形,则后指应位于该圆圈的内侧。而且,前指或多或少地等间隔。我们可能会尝试使用这些启发式属性来检测手指。
伪代码:
select the top N finger candidates (not too many, 10 or 12)consider all possible combinations of 5 out of N (use itertools.combinations)for each combination of 5 fingers: for each finger out of 5: fit the best circle to the remaining 4 => position of the center, radius check if the selected finger is inside of the circle check if the remaining four are evenly spread (for example, consider angles from the center of the circle) assign some cost (penalty) to this selection of 4 peaks + a rear finger (consider, probably weighted: circle fitting error, if the rear finger is inside, variance in the spreading of the front fingers, total intensity of 5 peaks)choose a combination of 4 peaks + a rear peak with the lowest penalty
这是一种蛮力的方法。如果N相对较小,那么我认为它是可行的。对于N = 12,有C_12 ^ 5 = 792种组合,有5种方式可以选择一个后指,所以3960个案例来评估每个爪子。

TA贡献1884条经验 获得超4个赞
这是图像注册问题。总体战略是:
有一个已知的例子,或者某种先前的数据。
使您的数据适合示例,或使示例适合您的数据。
如果您的数据首先大致一致,这会有所帮助。
这是一个粗略而准备好的方法,“可能有用的最蠢的事情”:
从大约您期望的位置开始五个脚趾坐标。
每一个,迭代地爬到山顶。即,给定当前位置,如果其值大于当前像素,则移动到最大相邻像素。当你的脚趾坐标停止移动时停止。
要抵消方向问题,您可以为基本方向(北,东北等)设置8个左右的初始设置。单独运行每一个并丢弃任何结果,其中两个或多个脚趾最终在同一像素。我会更多地考虑这个问题,但是这种事情仍然在图像处理中进行研究 - 没有正确的答案!
稍微复杂的想法:(加权)K均值聚类。没那么糟糕。
从五个脚趾坐标开始,但现在这些是“集群中心”。
然后迭代直到收敛:
将每个像素分配给最近的群集(只为每个群集创建一个列表)。
计算每个群集的质心。对于每个聚类,它是:Sum(坐标*强度值)/ Sum(坐标)
将每个群集移动到新的质心。
这种方法几乎可以肯定地提供更好的结果,并且您可以获得每个群集的质量,这可能有助于识别脚趾。
(同样,您已经预先指定了群集的数量。使用群集,您必须以这样或那样的方式指定密度:在这种情况下选择适合的群集数量,或者选择群集半径并查看结束的群集数量后者的一个例子是平均移位。)
对于缺乏实施细节或其他细节感到抱歉。我会对此进行编码,但我有一个截止日期。如果下周没有其他任何工作让我知道,我会试一试。
添加回答
举报