Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find local maxima in an image in MATLAB?

Tags:

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); 

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

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]; 

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); 

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

like image 302
Nathan Fellman Avatar asked Dec 06 '09 18:12

Nathan Fellman


People also ask

How do you find the maxima in Matlab?

M = max( A ) returns the maximum elements of an array. If A is a vector, then max(A) returns the maximum of A . If A is a matrix, then max(A) is a row vector containing the maximum value of each column of A .

How do you find the local maximum of a function in Matlab?

Description. TF = islocalmax( A ) returns a logical array whose elements are 1 ( true ) when a local maximum is detected in the corresponding element of A . TF = islocalmax( A , dim ) specifies the dimension of A to operate along. For example, islocalmax(A,2) finds local maximum of each row of a matrix A .

What is local maxima in image processing?

In image processing, the usual definition of a local maximum is that a pixel is considered to be a local maximum if and only if it is greater than or equal to all of its immediate neighbors. There is a convenient and efficient way to find all the local maxima pixels by using image dilation.

How do you find the area of an object in an image in Matlab?

total = bwarea( BW ) estimates the area of the objects in binary image BW . total is a scalar whose value corresponds roughly to the total number of on pixels in the image, but might not be exactly the same because different patterns of pixels are weighted differently.


1 Answers

bw = pic > imdilate(pic, [1 1 1; 1 0 1; 1 1 1]); 
like image 185
Steve Eddins Avatar answered Nov 16 '22 00:11

Steve Eddins