如何在图像中找到局部最大值 [英] how to find local maxima in image
问题描述
问题在于特征检测的概念. 找到图像的角点后卡住了,我想知道如何在计算的角点内找到特征点.
The question is about feature detection concept. I'm stuck after I finding the corner of image and I want to know how to finding the feature point within the computed corners.
假设我有这样的灰度图像
Suppose I have grayscale image that have data like this
A = [ 1 1 1 1 1 1 1 1;
1 3 3 3 1 1 4 1;
1 3 5 3 1 4 4 4;
1 3 3 3 1 4 4 4;
1 1 1 1 1 4 6 4;
1 1 1 1 1 4 4 4]
如果我使用
B = imregionalmax(A);
结果将是这样
B = [ 0 0 0 0 0 0 0 0;
0 1 1 1 0 0 1 0;
0 1 1 1 0 1 1 1;
0 1 1 1 0 1 1 1;
0 0 0 0 0 1 1 1;
0 0 0 0 0 1 1 1]
问题是如何在最大局部区域内拾取最高峰(在示例中,我是如何从3中选择5并从4中选择6)?
The question is how do I pick the highest peak inside max local region (in sample how did I chose 5 from 3 and 6 from 4)?
我的想法是使用B来检测每个区域并再次使用imregionalmax()
,但是我不擅长编码,因此我需要一些建议或其他想法.
My idea was using B to detect each region and use imregionalmax()
again but I'm not good at coding and I need some advice or other ideas.
推荐答案
还有其他两种简单的方法可以实现2D峰查找器: imdilate
.
There are a couple of other easy ways to implement a 2D peak finder: ordfilt2
or imdilate
.
最直接的方法是使用ordfilt2
,它对本地邻域中的值进行排序并选择第n个值. ( MathWorks示例演示了如何实现最大过滤器).您还可以使用ordfilt2
来实现3x3峰值查找器,方法是:(1)使用不包括中心像素的3x3域,(2)选择最大的(第8个)值,然后( 3)与中心值比较:
The most direct method is to use ordfilt2
, which sorts values in local neighborhoods and picks the n-th value. (The MathWorks example demonstrates how to implemented a max filter.) You can also implement a 3x3 peak finder with ordfilt2
by, (1) using a 3x3 domain that does not include the center pixel, (2) selecting the largest (8th) value and (3) comparing to the center value:
>> mask = ones(3); mask(5) = 0 % 3x3 max
mask =
1 1 1
1 0 1
1 1 1
此掩码中考虑了8个值,因此第8个值是最大值.过滤器输出:
There are 8 values considered in this mask, so the 8-th value is the max. The filter output:
>> B = ordfilt2(A,8,mask)
B =
3 3 3 3 3 4 4 4
3 5 5 5 4 4 4 4
3 5 3 5 4 4 4 4
3 5 5 5 4 6 6 6
3 3 3 3 4 6 4 6
1 1 1 1 4 6 6 6
技巧是将其与每个邻域的中心值A
进行比较:
The trick is compare this to A
, the center value of each neighborhood:
>> peaks = A > B
peaks =
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0
imdilate
图像膨胀通常在二进制图像上完成,但是灰度图像膨胀只是一个最大滤镜(请参见). ordfilt2
使用的相同技巧也适用于此:定义不包含中心邻域像素的邻域,应用滤镜并将其与未滤镜的图像进行比较:
imdilate
Image dilation is usually done on binary images, but grayscale image dilation is simply a max filter (see Definitions section of imdilate
docs). The same trick used with ordfilt2
applies here: define a neighborhood that does not include the center neighborhood pixel, apply the filter and compare to the unfiltered image:
B = imdilate(A, mask);
peaks = A > B;
注意:这些方法只能找到一个像素峰值.如果任何一个邻居的值相同,那么它就不会是峰值.
NOTE: These methods only find a single pixel peak. If any neighbors have the same value, it will not be a peak.
这篇关于如何在图像中找到局部最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!