如何检测图像中的光点?

How to detect light spots in image?

为了检测某个地理区域的 inselbergs,我下载了该区域的地形图像(地势和斜度)。斜率图像似乎最适合这项任务。

应用高斯模糊(或使用 ImageMagick 的更快的普通模糊)后,图像似乎已准备好进行自动检测。

现在我想知道 best/fastest 检测黑色背景上这些白色污渍的方法。我的第一个想法是使用一个简单的函数(没有外部库),它的工作原理类似于绘图程序的 "bucket paint" 函数,计算比预定义阈值亮的对象的面积。问题是:由于图像非常大,5400x3600 像素,通常的递归函数可能会导致堆栈溢出,特别是在像下面这样的大山脉上。

那么,有什么建议吗?我认为理想的语言可能是 C++(也许 JavaScript)。我不习惯Python。也许使用像 OpenCV 这样的库会更容易,但也许问题太微不足道,无法请求外部库(包括隐含的学习曲线)。


TIFF图像来自here(我可以将它们转换成其他格式进行处理)。示例图像位于 17S42 四面体("Declividade" 选项),靠近坐标 18°S 41°W。在中间的图像(上图)中,每个 "white ball" 都是一个 inselberg。精度将取决于所选择的灰度阈值。

一种方法是使用 ImageMagick 连接组件并在适当的阈值后从每个白色区域提取质心。

这是 ImageMagick 6 中的命令行。但如果需要,您可以使用 C+ API。

毛边图像:

convert blur.png -auto-level -threshold 65% -type bilevel \
-define connected-components:verbose=true \
-define connected-components:mean-color=true \
-connected-components 4 binary.png |\
grep "gray(255)" | awk '{print }'


质心(像素坐标):

915.9,367.4
873.4,76.8
936.4,269.5
664.8,212.0
598.2,554.3
954.0,59.3
732.6,446.0
353.5,696.4
69.7,848.0
750.4,357.6
537.5,609.9
832.0,230.9
517.9,539.1


阈值图像:

简而言之,使用您的方法您将得到一个非常差的尺寸估计值。模糊区域的边缘,无论您选择什么阈值,都不对应于您要测量的 inselbergs 的边缘。 相反,我建议您遵循以下食谱。为此,我在 Python 中使用 DIPlib(免责声明:我是作者)。 Python 绑定是 C++ 库上的薄层,将下面的 Python 代码转换为 C++ 相当简单(我更容易在 Python 中交互式开发它)。使用 pip install diplib.

安装

我从您提供的 link 下载了原始高度数据(而不是倾斜度)。 DIPlib 可以直接读取浮点值的 TIFF 文件,因此不需要任何特殊的转换。我裁剪了一个类似于 OP 用于此演示的区域,但没有理由不将该方法应用于整个图块。

import diplib as dip

height = dip.ImageRead('17S42_ZN.tif')
height.SetPixelSize(0.000278, 'rad')  # not really radian, but we don't have degrees
height = height[3049:3684, 2895:3513];

代码还根据 TIFF 文件中的数据设置像素大小(使用弧度单位,因为 DIPlib 不计算度数)。

接下来,我应用特定直径(25 像素)的顶帽滤镜。这将隔离所有直径为 25 像素或更小的峰。根据您认为 inselberg 的最大宽度调整此大小。

local_height = dip.Tophat(height, 25)

实际上,结果是局部高度,即高于某个基线的高度,由过滤器的大小决定。

接下来,我应用滞后阈值(双阈值)。这会产生一个二进制图像,在基线以上 100 米处设置阈值,其中地形在基线以上 200 米以上。也就是说,我决定在基线以上至少 200 米处设置一个岛山,但在 100 米处将每个岛山切断。在这个高度,我们将测量尺寸(面积)。同样,根据需要调整阈值。

inselbergs = dip.HysteresisThreshold(local_height, 100, 200)

现在剩下的就是测量我们找到的区域:

labels = dip.Label(inselbergs)
result = dip.MeasurementTool.Measure(labels, features=['Size', 'Center'])
print(result)

这输出:

   |       Size |                  Center | 
-- | ---------- | ----------------------- | 
   |            |       dim0 |       dim1 | 
   |     (rad²) |      (rad) |      (rad) | 
-- | ---------- | ---------- | ---------- | 
 1 |  1.863e-05 |     0.1514 |    0.01798 | 
 2 |  4.220e-05 |     0.1376 |    0.02080 | 
 3 |  6.214e-05 |    0.09849 |    0.04429 | 
 4 |  6.492e-06 |     0.1282 |    0.04710 | 
 5 |  3.022e-05 |     0.1354 |    0.04925 | 
 6 |  4.274e-05 |     0.1510 |    0.05420 | 
 7 |  2.218e-05 |     0.1228 |    0.05802 | 
 8 |  1.932e-05 |     0.1420 |    0.05689 | 
 9 |  7.690e-05 |     0.1493 |    0.06960 | 
10 |  3.285e-05 |     0.1120 |    0.07089 | 
11 |  5.248e-05 |     0.1389 |    0.07851 | 
12 |  4.637e-05 |     0.1096 |    0.09016 | 
13 |  3.787e-05 |    0.07146 |     0.1012 | 
14 |  2.133e-05 |    0.09046 |    0.09908 | 
15 |  3.895e-05 |    0.08553 |     0.1064 | 
16 |  3.308e-05 |    0.09972 |     0.1143 | 
17 |  3.277e-05 |    0.05312 |     0.1174 | 
18 |  2.581e-05 |    0.07298 |     0.1167 | 
19 |  1.955e-05 |    0.04038 |     0.1304 | 
20 |  4.846e-05 |    0.03657 |     0.1448 | 

(请记住,它说 'rad' 的地方实际上是度数。)平方度数的面积有点奇怪,但您可以将其转换为平方米,因为您知道地球上的位置。实际上,在计算之前将像素大小转换为米可能更容易。

此处为 'Center' 给出的值是相对于左上角像素的,如果我们一开始没有裁剪图块,我们可以添加图块的坐标(可以从TIFF文件中对应的标签获取:(-42.0, -17.0).


在 C++ 中,代码应如下所示:

#include <diplib/simple_file_io.h>
#include <diplib/morphology.h>
#include <diplib/segmentation.h>
#include <diplib/regions.h>
#include <diplib/measurement.h>

//...

dip::Image height = dip::ImageRead("17S42_ZN.tif");
height.SetPixelSize(0.000278 * dip::Units::Radian());
height = height.At(dip::Range(3049, 3684), dip::Range(2895, 3513));

dip::Image local_height = dip::Tophat(height, 25);

dip::Image inselbergs = dip::HysteresisThreshold(local_height, 100, 200);

dip::Image labels = dip::Label(inselbergs);
dip::MeasurementTool measurementTool;
dip::Measurement result = measurementTool.Measure(labels, {}, {"Size", "Center"});
std::cout << result;