File Exchange

## Fast 2D peak finder

version 1.12 (4.14 KB) by

Find local maxima \ peak positions in noisy 2D arrays

4.76471
22 Ratings

Updated

A simple and fast 2D peak finder. The aim was to be faster than more sophisticated techniques yet good enough to find peaks in noisy data. The code analyzes noisy 2D images and find peaks using robust local maxima finder (1 pixel resolution) or by weighted centroids (sub-pixel resolution). The code is designed to be as fast as possible, so I kept it pretty basic. It best works when using uint16 \ uint8 images, and assumes that peaks are relatively sparse.

The code requires Matlab's Image Processing Toolbox, and can be used inside parfor for faster processing times.

Steven

Dennis Dunn

Bridget Tannian

### Bridget Tannian (view profile)

Thank you so much Natan, i got my answer,
What i mean by peak area is that if the data is flat at the peak (i mean for example when you have a peak surface instaed of just a peak point ),

Natan

### Natan (view profile)

Hi Mehrann, I dont understand what you mean by "peak area" vs "peak point". For the purpose of this code, a peak is defined by a point spread function (or PSF, see here https://en.wikipedia.org/wiki/Point_spread_function ) + some noise. The filter to use should be as close as possible to that theoretical PSF in order to optimize the peak detection. The reason is that the filter is then used to "blur" the data (using conv2). The output is are "blurred" peaks that have a unique local maximum value in each of them, and then the rest of the code is used to find that local maximum pixel in each peak. If that doesn't answer your question maybe send me an example so I'll be able to better understand what you ask.

Very nice job Natan, thank you
I just have a question? The 'filt' input parameter is a filter (for example a gaussian filter) which you have to define somewhere else and then use it in your code, right? And what it does is first implementing the filter and then find the peaks, right?
Because as i understood, your code does not work if there is peak area (instead of peak point) and you do not use filt, however if you define a simple filt then you will get at least one pixel per each peak area, am i correct?

Wim

### Wim (view profile)

excellent work, cleanly written and therefore easily adaptable to special needs

Natan

### Natan (view profile)

Hi Wim and thanks for the feedback! I'll fix the first point in the next update for sure. However, having a "flat" peak means for me that the either detector was saturated
( for example,the camera was over-exposed) , or that it is not really a peak but some distribution, because by definition a peak has a well defined maxima, or that the filter was not set properly. The latter point related to the 2d-convolution with some filter (the default is a 7x7 Gaussian, with sigma=1). if the data is still flat at the peak maybe a different filter matrix should be used. Please send me an example and we can discuss this further (my email is in the code header somewhere).

Wim

### Wim (view profile)

Nice piece of work, Natan!
However, I think there are 2 problems with your code:
1. if one inputs a floating point matrix with peaks < 1, the data gets scaled. You should scale the threshold then too
2. it cannot handle flat peaks: peaks that have more than 1 pixel of the same value in x or y direction will not be recognised

SL

### SL (view profile)

I am testing your code now with gray background but I get strange results. Discussion here http://stackoverflow.com/q/40119075/54964

SL

### SL (view profile)

This is an excellent piece of work! I think the sensitivity and specificity of the method should be included for each method. I get quite low sensitivities with the basic `function(I)`, analysing energy maps with rapid peaks. It would be great to extend it with statistical power!

Jeroen

### Jeroen (view profile)

This is a correction of my former wrong rate.

John Little

### John Little (view profile)

Excellent work, it would be nice to also have GPU support.

Jeroen

Natan

### Natan (view profile)

Hi Faruk, looking into your image1.tif file I see the issue of not recognizing the fainter peaks. The main reason for that, as I suspected, is that a flat threshold is not suitable for this image. In order to make it work you need to apply a local threshold, or do something that is called "common mode correction" of your image. Some cameras produce imaging data that should not directly be used in analysis and need corrections. Most popular corrections are: dark rate (pedestal) subtraction, bad pixel masking, common mode correction, gain correction, etc. Without going into too many details (you can search more info about the above), you can do the following "computational" trick, taking the local mean of the image you want to analyze. Denoting your image d:

ws=100; % window size
mean_d=imfilter(d,fspecial('average',ws),'replicate'); % this a local mean operation
dd=d-mean_d;

Then apply the code "as usual" on dd, and set a flat threshold that will capture all the peaks.

Faruk Moonschi

### Faruk Moonschi (view profile)

Thanks Natan! I am sending you an image.

Natan

### Natan (view profile)

Hi Faruk, if I had to guess, your image may need a different type of threshold as it may be a non uniform background, another reason might be related to the PSF size of your peaks vs the filtering used, but without looking at your image it's hard for me to help you, so can you please send me an example of an image that does that and we can discuss this further? (my email is commented in the code)

Faruk Moonschi

### Faruk Moonschi (view profile)

Great Job!

I have a issue though. Some peaks look like are not being selected. If I lower down the threshold value, it gets lots more spots, zooming in indicates those are not peaks.

I was wondering if you have any suggestion to overcome this issue. Thanks.

Moti Fridman

### Moti Fridman (view profile)

Works well in different data sets and can overcome noisy data.

Gael Leveque

Natan

### Natan (view profile)

Hi Gael, I'd like to help you, however, without the images that you're talking about and the exact way you used the code, I can't see how I can do that. Please email me (my email in commented in the code itself) with more info and I'll try to help you solve the problem if I can.

Gael Leveque

### Gael Leveque (view profile)

Great job!
I have some issues for a few pictures though. When the noisy peak I want to detect is too sharp, if I use the first mode too many peaks are found (I can't increase too much the matrix filter value or the real peak is flattened) and if I use the second mode, the function returns roughly the middle of the picture...
Any idea of how I could get trough this ?

Natan

### Natan (view profile)

Bruno, you can edit the code to include the cluster size threshold very easily. Just write: bw = bwareaopen(logical(d), thresC);
after line 116. where tresC is the cluster threshold you want. Then edit the line that reads "[x y]=find(..." to [x y]=find(bw(edg:sd(1)...

### Brunno Machado de Campos (view profile)

Great job! Very useful code!
If you includes a cluster size threshold to your code, it would be more than great!!! Anyway is great!

Yuanwei

Natan

### Natan (view profile)

The default filter matrix is a 7x7 Gaussian, and the smoothing is done via 2D convolution of the image with that filter. If you don't understand why this smooths the data you can refer to this simple demo (http://blogs.mathworks.com/videos/2012/04/17/using-convolution-to-smooth-data-with-a-moving-average-in-matlab/). The size of the filter was selected for typical PSF's, and what it does is to take the weighted average for each pixel with it's 7x7 pixels surrounding it (so it's a moving average), where the weights are normally distributed (hence a Gaussian). If you have more questions email me.

Jochen

### Jochen (view profile)

Thanks a lot for that brilliant function!
But I don't understand how the filter matrix is supposed to look like to smooth the image. Can you give me hint?
Thanks!

Andrey

Natan

### Natan (view profile)

@Dan, I've added a sub-pixel resolution centroid feature to the code...

Dan Hickstein

### Dan Hickstein (view profile)

This is great! Makes imregionalmax seem really slow and unreliable. Would be nice to have a subpixel centroid as well.

Roman Voronov

Sujay

### Sujay (view profile)

Thanks. it works well.

Natan

### Natan (view profile)

@Sujay, as the code documentation say in line 8: "The 2D data raw image - assumes a Double\Single-precision ... or unit16 array. Please note that the code casts the raw image to uint16, if your image is 8-bit depth, I recommend to change all the places that uint16 is used to uint8 for faster run times." So, either use FastPeakFind(uint16(image),...) or change all uint16 to uint8 in the code. In future updates I'll include this feature automatically.

Sujay

### Sujay (view profile)

I have a 512x512 .tif image that I can read in matlab as, A=imread(''). I am trying to use this program as,
FastPeakFind(A). It is showing the following error:
??? Error using ==> times
Integers can only be combined with integers of the same
class, or scalar doubles.

Error in ==> FastPeakFind at 81
d=d.*uint16(d>threshold);

What is wrong?

Slawomir

### Slawomir (view profile)

Great algorithm and pretty fast. Thanks !!!!

Natan

### Natan (view profile)

@Carl Witthoft, Thanks for the comments, I've updated the information regarding the image processing toolbox requirement the scaling in case of a 0:1 range of image values. I've also already tested some more compact local maxima conditions, such as:
if all ( reshape( d(x(j),y(j)) >= d(x(j)-1:x(j)+1,y(j)-1:y(j)+1),9,1))

if d(x(j),y(j)) == max(max(d((x(j)-1):(x(j)+1),(y(j)-1):(y(j)+1))))

if d(x(j),y(j)) == max( reshape( d(x(j),y(j)) >= d(x(j)-1:x(j)+1,y(j)-1:y(j)+1),9,1))

All these were a factor of 2-3 slower than the simple 8-fold if condition. It takes Matlab more time to do the max or reshape functions than to go through the simple conditions...

Additionally, Matlab's image processing toolbox has the function imregionalmax, but I've found it is 6-7 times slower than this code.

Carl Witthoft

### Carl Witthoft (view profile)

First commment lost.. try again. 1) Please specify that ImageProcessingToolbox is required. 2) For speed reasons, it may help to replace the 8-fold logical test with something like "d(j,j) > max(max(d(j-1:j+1,j-1:j+1))"

Carl Witthoft

### Carl Witthoft (view profile)

One more thought: to allow for input files with float ranges (e.g. 0:1.0), replace the uint16 cast with "d = uint16( d.*2^16./(max(max(d))" or similar scaling function.

Song