'How to detect peak local maxima or blob detection from an image (.png) or a .gif?

I want to find the peak local maxima from the 2D array rather than image. So, the following script has used to find the local maxima in the 2D array.

from scipy import ndimage as ndi
import matplotlib.pyplot as plt
from skimage.feature import peak_local_max
from skimage import data, img_as_float

# Input of a 2D array/ 
im = np.real(test_data) 

# Comparison between image_max and im to find the coordinates of local maxima
coordinates = peak_local_max(im, min_distance=8,  num_peaks=2) #, threshold_abs = 0.0004) #
# Display results
#plt.figure()
fig, axes = plt.subplots(1, 2, figsize=(13, 8), sharex=True, sharey=True)
ax = axes.ravel()
ax[0].imshow(im, cmap='seismic')
ax[0].axis('on')
ax[0].set_title('Original')

ax[1].imshow(im, cmap='seismic')
ax[1].autoscale(False)
ax[1].plot( coordinates[:, 1], coordinates[:, 0], 'k.', marker='o', markersize=12)
ax[1].axis('on')
ax[1].set_title('Peak_local_max')
fig.tight_layout()
plt.show()

The "test_data" file is found in this link https://www.file.io/LvDn/download/WqdRCoW2dUPV

This 2D array works fine for me as shown in Fig. 01 Local maxima detected. The "black spot" detects the local maxima in the 2D array.

However, when I want to detect "local maxima" or "blob detection" from some *.png images or .gif files then I don't detect the local maxima. I've used the following python script but could not succeed as in Fig. 02Local maxima wrongly detected. The "black spot" wrongly detected the local maxima in the image. I don't understand how to fix the issue. Can anybody please help?

from PIL import Image
#Reading the image
image = Image.open('sens9.png')
image_gray = image.convert('L')
im = np.array(image_gray) # ndarray
print('IMage shape', im.shape)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source