0

I have a grayscale image that has a lot of gradients. I'm only interested in the local "high spots" and want to get rid of everything that isn't the brightest nearby pixel. Specifically what I'm trying to do is erase (set to 0) any pixel where any of it's neighbors (preferably within a specified distance) have a higher value. It's okay if the check area is some sort of kernel (e.g. a 5x5 square) and not a circular area.

Is there some built-in way of doing this in OpenCV short of just looping over every pixel and doing the check myself?

Also if there's an existing term for what I'm asking for I would love to know what it is. I've been searching for answers to this but keep running into stuff about background removal because my search terms don't seem to be right.

Tsyvarev
  • 60,011
  • 17
  • 110
  • 153
Orion DeYoe
  • 102
  • 1
  • 11
  • 1
    You are looking for "local maxima". But I do not think OpenCV has a direct tool for that. But see (global thresholding) at https://www.pyimagesearch.com/2016/10/31/detecting-multiple-bright-spots-in-an-image-with-python-and-opencv/ and (template matching) https://www.pyimagesearch.com/2021/03/29/multi-template-matching-with-opencv/ and especially (non-maxima suppression) at https://www.pyimagesearch.com/2014/11/17/non-maximum-suppression-object-detection-python/ – fmw42 Apr 13 '21 at 00:12
  • 1
    You can try use absolute difference between image and dilate image (whith kernel 5x5). If the difference is zero, then we have it, most likely, the local maximum point. – Alex Alex Apr 13 '21 at 00:38
  • @AlexAlex Your approach is correct. For an integer-valued image though, I would just compare the dilated image to the original image for equality. – beaker Apr 13 '21 at 15:10

1 Answers1

0

I modified Otsu thresholding for images like you explain you can try it, enter image description here import cv2 import matplotlib.pyplot as plt

image = cv2.imread('200-01-L.jpg')
image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)

##### Flating the image
image_flat = image.reshape(image.shape[0], image.shape[1] * image.shape[2])

gray_image = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)

# Otsu's thresholding
thr, img_thre = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# Otsu's thresholding
thr, img_flat = cv2.threshold(image_flat, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

thr, Mod_Otsu = cv2.threshold(gray_image,thr,255, cv2.THRESH_BINARY)

fig, axes = plt.subplots(ncols=3, figsize=(10, 5))
ax = axes.ravel()

ax[0].imshow(image, cmap=plt.cm.gray)
ax[0].set_title('Original image')


ax[1].imshow(img_thre, cmap=plt.cm.gray)
ax[1].set_title('Otsu')

ax[2].imshow(Mod_Otsu, cmap=plt.cm.gray)
ax[2].set_title('Modefiyed Otsu')

plt.show()