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.
please cite As:
Natan (2021). Fast 2D peak finder (https://www.mathworks.com/matlabcentral/fileexchange/37388-fast-2d-peak-finder), MATLAB Central File Exchange. Retrieved May 26, 2021.
How the code works: In theory, each peak is a smooth point spread function (SPF), like a Gaussian of some size, etc. In reality, there is always noise, such as "salt and pepper" noise, which typically has a 1-pixel variation. Because the peak's PSF is assumed to be larger than 1 pixel, the "true" local maximum of that PSF can be obtained if we can get rid of these single-pixel noise variations. There comes medfilt2, which is a 2D median filter that gets rid of "salt and pepper" noise. Next we "smooth" the image using conv2, so that with high probability there will be only one pixel in each peak that will correspond to the "true" PSF local maximum. The weighted centroid approach uses the same image processing, with the difference that it just calculated the weighted centroid of each connected object that was obtained following the image processing. While this gives sub-pixel resolution, it can overlook peaks that are very close to each other, and runs slightly slower. Read more about how to treat these cases in the relevant code comments.
- d - The 2D data raw image - assumes a Double\Single-precision floating-point, uint8 or unit16 array. Please note that the code casts the raw image to uint16 if needed. If the image dynamic range is between 0 and 1, I multiplied to fit uint16. This might not be optimal for generic use, so modify according to your needs.
- thres - A number between 0 and max(raw_image(:)) to remove background
- filt - A filter matrix used to smooth the image. The filter size should correspond the characteristic size of the peaks
- edg - A number>1 for skipping the first few and the last few 'edge' pixels
- res - A handle that switches between two peak finding methods: 1 - the local maxima method (default). 2 - the weighted centroid sub-pixel resolution method. Note that the latter method takes ~20% more time on average.
- fid - In case the user would like to save the peak positions to a file, the code assumes a "fid = fopen([filename], 'w+');" line in the script that uses this function.
- cent - a 1xN vector of coordinates of peaks (x1,y1,x2,y2,...
- [cent cm] - in addition to cent, cm is a binary matrix of size(d) with 1's for peak positions. (not supported in the weighted centroid sub-pixel resolution method)
imagesc(image); hold on
A. Natan (2023). Fast 2D peak finder (https://github.com/adinatan/fastpeakfind/releases/tag/126.96.36.199), GitHub. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!
See release notes for this release on GitHub: https://github.com/adinatan/fastpeakfind/releases/tag/188.8.131.52
Added sub-pixel resolution using weighted centroids as a feature. Improved documentation.
the case binary matrix output when the image is all zeros returned an error (bug now fixed). Many thanks to Roman Voronov for spotting this.
Added automatic uint8 support for improved performance. Added an output option of a binary matrix of peak positions besides the regular coordinate vector.
corrected typos from previous release that prevented the function from running for no arguments (demo mode)
Improved performance of saving to file and additional small improvements.
Added scaling correction for the case pixel values are all between 0 and 1. Improved file documentation.
minor editing, added functionality - when no input is used, the function generates random peaks data and plot a figure.
Code runs faster by casting to appropriate numeric classes for medfilt2 and conv2
bug fixed, code now handles images of arbitrary size.