如果您有权访问图像处理工具箱 https://www.mathworks.com/help/images/,您可以利用它包含的许多过滤和形态操作。这是解决问题的一种方法,使用函数imfilter https://www.mathworks.com/help/images/ref/imfilter.html, imclose https://www.mathworks.com/help/images/ref/imclose.html, and imregionalmax https://www.mathworks.com/help/images/ref/imregionalmax.html:
% Load and plot the image data:
imageData = imread('lattice_pic.jpg'); % Load the lattice image
subplot(221);
imshow(imageData);
title('Original image');
% Gaussian-filter the image:
gaussFilter = fspecial('gaussian', [31 31], 9); % Create the filter
filteredData = imfilter(imageData, gaussFilter);
subplot(222);
imshow(filteredData);
title('Gaussian-filtered image');
% Perform a morphological close operation:
closeElement = strel('disk', 31); % Create a disk-shaped structuring element
closedData = imclose(filteredData, closeElement);
subplot(223);
imshow(closedData);
title('Closed image');
% Find the regions where local maxima occur:
maxImage = imregionalmax(closedData);
maxImage = imdilate(maxImage, strel('disk', 5)); % Dilate the points to see
% them better on the plot
subplot(224);
imshow(maxImage);
title('Maxima locations');
这是上面代码创建的图像:
为了让事情看起来不错,我只是不断尝试高斯滤波器参数的几种不同组合(使用创建fspecial https://www.mathworks.com/help/images/ref/fspecial.html)和结构元素(使用创建strel https://www.mathworks.com/help/images/ref/strel-class.html)。然而,一点点的尝试和错误却得到了非常好的结果。
NOTE:返回的图像来自imregionalmax https://www.mathworks.com/help/images/ref/imregionalmax.html并不总是将单个像素设置为 1(以指示最大值)。输出图像通常包含像素簇,因为输入图像中的相邻像素可以具有相等的值,因此都被计为最大值。在上面的代码中,我还用以下内容扩展了这些点imdilate https://www.mathworks.com/help/images/ref/imdilate.html只是为了让它们在图像中更容易看到,这使得以最大值为中心的像素簇更大。如果要将像素簇减少为单个像素,则应该删除膨胀步骤并以其他方式修改图像(向结果添加噪声或对其进行过滤,然后找到新的最大值等)。