Clear Filters
Clear Filters

An equivalent function to "imregionalmax" with higher speed

17 views (last 30 days)
Hello, I have to find the second biggest peak value of a number of 2D array. One way is to iterate over all of these arrays and perform "imregionalmax" to each one and find the second biggest peak. The problem that I am facing with, is that the number of these 2D arrays are a lot and "imregionalmax" is low-speed. (I need to implement this algorithm thousands of times and therefore, its speed is a critical issue.) So, I'm eager to know if there is any replacement for above method? Thank you
  2 Comments
Image Analyst
Image Analyst on 13 Jan 2024
What is your definition of " second biggest peak value" for a 2-D array? The area of it? The gray level at the very peak?
imregionalmax will only give a single pixel at the brightest point -- it will not give you the entire peak out to where a valley is encountered and it begins to turn upwards again.
Are you sure that doing it slice by slice on the 2-D slices will give you the correct peak as if you considered the 3-D array as a whole?
DGM
DGM on 14 Jan 2024
Edited: DGM on 14 Jan 2024
imregionalmax() will give the entire connected region of maximal value. The only case in which the region is a single pixel is when a single pixel is surrounded by smaller values.
It's hard to suggest an alternative without knowing the requirements, but bear in mind that imregionalmax() does what imregionalmax() needs to do. It's lean and written in MEX already and actually has a method for gpuarrays. You're probably going to have a hard time trying to write anything in assorted m-code that can beat it, unless you actually need something other than what imregionalmax() does.
As to the ambiguity of "second biggest peak value", bear in mind that the regional max with the second-highest value is not necessarily the apex of the region it encloses. The blog post I linked to below shows an example where it's not. I don't know if that matters to you.

Sign in to comment.

Accepted Answer

Matt J
Matt J on 14 Jan 2024
Edited: Matt J on 14 Jan 2024
If you can count on the "peaks" to be a single pixel in size, you can just do,
ispeak=A>=imdilate(A,ones(3)); %A is the image
  2 Comments
Matt J
Matt J on 14 Jan 2024
Edited: Matt J on 14 Jan 2024
Timing test:
A=rand(200,200,500);
tic;
B=false(size(A));
for i=1:size(A,3)
B(:,:,i)=imregionalmax(A(:,:,i));
end
toc
Elapsed time is 0.888028 seconds.
tic;
ispeak=A>=imdilate(A,ones(3)); %A is the image
toc
Elapsed time is 0.333737 seconds.
isequal(B,ispeak)
ans = logical
1

Sign in to comment.

More Answers (1)

Hassaan
Hassaan on 13 Jan 2024
  1. Parallel Processing: If you have multiple cores available, you can use MATLAB's parallel processing tools (parfor loop or batch processing) to process multiple arrays simultaneously. This can significantly speed up the computation.
  2. Optimized Search Algorithm: Instead of using imregionalmax, you can write a custom function that is optimized for your specific data. For example, if your data has certain characteristics (like sparsity or known range of peak values), you can tailor your search algorithm to leverage these.
  3. Downsampling: If the arrays are large, consider downsampling them to reduce the amount of data to process. This is effective if the peaks are still discernible in the lower-resolution data.
  4. Pre-Filtering: Apply a filter to remove noise or smooth the data, which might make the peaks more pronounced and easier to detect. Be cautious with filtering as it can also distort the data.
  5. Algorithm Refinement: Instead of finding all regional maxima, you might directly search for the top two peaks. This can be done by scanning the array and keeping track of the highest and second-highest values found so far.
  6. Utilize Built-in Functions for Peak Finding: MATLAB's findpeaks function can be used directly on 2D data by converting the 2D array to a 1D array (either by rows or columns). This might be faster than imregionalmax in some cases.
  7. Caching Results: If some of the 2D arrays are repeated in the dataset, you can cache the results of the peak-finding process to avoid redundant computations.
  8. Reducing Precision: If you're working with high-precision data types (like double), consider reducing the precision to single. This can decrease memory usage and potentially speed up calculations, but it might affect the accuracy of the results.
---------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  1 Comment
moh mor
moh mor on 13 Jan 2024
in case 6, using "findpeaks" is not acceptable for 2D data because we need to check 8-connected pixels around the pixel that we are going to check if it is peak or not. if I'm wrong, let me know please.
Thank you

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!