如何在MATLAB中找到图像中的局部最大值? [英] How can I find local maxima in an image in MATLAB?

查看:2513
本文介绍了如何在MATLAB中找到图像中的局部最大值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MATLAB中有一个图像:

I have an image in MATLAB:

y = rgb2gray(imread('some_image_file.jpg'));

我想对其进行一些处理:

and I want to do some processing on it:

pic = some_processing(y);

并找到输出的局部最大值。也就是说, y 中的所有点都大于他们所有的邻居。

and find the local maxima of the output. That is, all the points in y that are greater than all of their neighbors.

我似乎无法忍受找到一个MATLAB函数来做得很好。我能想到的最好的是:

I can't seem to find a MATLAB function to do that nicely. The best I can come up with is:

[dim_y,dim_x]=size(pic);
enlarged_pic=[zeros(1,dim_x+2);
              zeros(dim_y,1),pic,zeros(dim_y,1);
              zeros(1,dim_x+2)];

% now build a 3D array
% each plane will be the enlarged picture
% moved up,down,left or right,
% to all the diagonals, or not at all

[en_dim_y,en_dim_x]=size(enlarged_pic);

three_d(:,:,1)=enlarged_pic;
three_d(:,:,2)=[enlarged_pic(2:end,:);zeros(1,en_dim_x)];
three_d(:,:,3)=[zeros(1,en_dim_x);enlarged_pic(1:end-1,:)];
three_d(:,:,4)=[zeros(en_dim_y,1),enlarged_pic(:,1:end-1)];
three_d(:,:,5)=[enlarged_pic(:,2:end),zeros(en_dim_y,1)];
three_d(:,:,6)=[pic,zeros(dim_y,2);zeros(2,en_dim_x)];
three_d(:,:,7)=[zeros(2,en_dim_x);pic,zeros(dim_y,2)];
three_d(:,:,8)=[zeros(dim_y,2),pic;zeros(2,en_dim_x)];
three_d(:,:,9)=[zeros(2,en_dim_x);zeros(dim_y,2),pic];

然后查看第3层的最大值是否出现在第1层(即: three_d(:,:,1)):

And then see if the maximum along the 3rd dimension appears in the 1st layer (that is: three_d(:,:,1)):

(max_val, max_i) = max(three_d, 3);
result = find(max_i == 1);

还有更优雅的方法吗?这看起来有点像kludge。

Is there any more elegant way to do this? This seems like a bit of a kludge.

推荐答案

bw = pic > imdilate(pic, [1 1 1; 1 0 1; 1 1 1]);

这篇关于如何在MATLAB中找到图像中的局部最大值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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