-1

I have plotted the following graph from a binary image which has only data value of 0 and 255. So, far I have used the following:

pic = cv2.imread("img.png")
pic = pic[:,:,0] #taking just one channel

count_non_zero = np.sum(pic==255, axis = 1)

plt.plot(count_non_zero)

That gives me the following graph where the x axis is the number of row, y axis is the number of columns and data points being the pixel values of 0 and 255:

Image

Graph:

2

Now, I am trying to print out the local maximum of each peaks sequentially (colored in black). This value is the number of columns where each peak is hitting. For that graph, it should print out 3 peak values around 590~600. Kindly please help me!

Been stuck here for a while now!

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • Can you provide the sample png image? (Also you can add images to a post b.t.w.) – Bill Feb 07 '23 at 19:02
  • Does this answer your question? [How to find peaks in 1d array](https://stackoverflow.com/questions/43289838/how-to-find-peaks-in-1d-array) – Michael Cao Feb 07 '23 at 19:02
  • @Bill, added a sample image! – Issac Boera Feb 07 '23 at 19:17
  • I've tweaked your question a little. you are asking to get the local maxima of an 1-D signal. all the rest is interesting and useful context, but secondary. – Christoph Rackwitz Feb 08 '23 at 08:52
  • here's a simple outline of something I always use for 2D data (heatmaps and such): apply grayscale dilation to get a map of the local maximum value per element (given some radius), then test each element for being *equal* to that. if it is, it's a candidate. adjacent elements may be local maxima, which I would handle with connected components analysis (probably exists for 1-D too). even non-adjacent elements within the radius can be maxima, if they're equal. you can either accept those or consider more logic for that. – Christoph Rackwitz Feb 08 '23 at 08:55
  • Does this answer your question? [Finding local maxima/minima with Numpy in a 1D numpy array](https://stackoverflow.com/questions/4624970/finding-local-maxima-minima-with-numpy-in-a-1d-numpy-array) – Christoph Rackwitz Feb 08 '23 at 08:56

1 Answers1

0

Use scipy.signal.find_peaks to detect the peaks and then to visualize them you can use this :

plt.plot(your_data) # initial series 
plt.plot(peaks_values, peaks_xaxis, "x") # detected by the scipy find_peaks function
plt.show()

This must give you the graph with the detected peaks like the image below :

enter image description here