How to identify nearby pixels with similar intensities in image?

Viewed 109

I have an image which is as given below and I have created the mask for that image (the red dots are generated from the mask). The points in the mask should ideally cover the entire black dots on the image but as I just have one co-ordinate for each black patch, the resultant image looks as given below.

enter image description here

How do I identify the surrounding pixels (greys and lighter blacks) and mark them as well? Is there any way or method that I can look up and implement.

1 Answers

If I'm understanding you correctly, you want to convert all the surrounding gray and dark pixels to red. If so, here's an approach using OpenCV. The idea is to load the image, convert to grayscale, then Otsu's threshold to obtain a 1-channel binary image. This will give us a mask where we can use np.where to color pixels red where there are white pixels on the mask. Here's the results:

Binary mask

enter image description here

Result

enter image description here

import cv2
import numpy as np

# Load image, grayscale, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY_INV)[1]

# Color pixels red where there are white pixels on the mask
image[np.where(thresh==255)] = [0,0,255]

# Display
cv2.imshow('thresh', thresh)
cv2.imshow('image', image)
cv2.waitKey()
Related