azerty
azerty

Reputation: 777

Opencv Mat: find all peaks and valleys

I was wondering what is the best way to find all the peaks and valleys in a 2D grey level Mat ?

I found only opencv functions which find local extremum in a Mat but not all the peaks and valleys.

So I try to find out my own algorithm, but I hope there is a way (already existing or improving this one) to make it faster

My idea would be :

In order to find all the peaks:

Then redo the same thing for valley starting from 0 up to 255.

So, what do you think about it ?

I guess there is a faster way to do it ?

Thanks

PS: I did that in order to try detect stem cells colonies in images like that: sample

Because I thought that there will be more peaks and valleys inside the stem cells colony (the big thing in the middle). But I am afraid this is not a very good way since I seem to have as many peaks inside the cells colony than outside...

Upvotes: 1

Views: 4436

Answers (2)

DanielHsH
DanielHsH

Reputation: 4453

  1. If you want to find all the local peaks - here is extremely efficient algorithm: For every pixel check if it is >= of each of its 8 neighbor pixels. If it does, mark it as local maximum, otherwise not. You will get your solution in one pass over the image.
  2. If you don't need all the peaks but rather say 5 highest peaks than there is another solution: Find the highest peak and overwrite it with black color, and its 7x7 neighborhood. Now repeat those steps (finding global maximum and suppressing it). Iterate this process 5 times and you will get 5 highest peaks which has at least distance of 7 pixels between them. This algorithm is not very accurate but it is simple, fast and in many cases does the job.
  3. If the methods above are not good enough, you might need a heavy gun. Are you familiar with "morphological reconstruction"? If not, please Google search the term "flood fill" algorithm and read about it. It fills local valleys and peaks. After that read about morphological reconstruction. It can definitely solve your problem, but the running time might be slow if you are not using GPU.

Upvotes: 3

Mailerdaimon
Mailerdaimon

Reputation: 6080

One idea would be:

  • Define an Area in which only one Peak (PeakArea) is allowed and a threshold for a peak.
  • Slide a Window with the size of your Peak Area over the Picture.
  • If at least one Pixel in the Window exceeds the threshold: mark the maximum pixel in this area as peak and all other as not-a-peak.

otherwise you would find nearly every Pixel as a peak.

Upvotes: -1

Related Questions