I would like to threshold an image, but instead of the output being black and white I would like it to be white and some other color. I was able to achieve this using a nested for-loop however this is slow and I was wondering if anyone knows any method of doing this efficiently using CV2 functionality.
img = cv2.imread("Naas.png", 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
threshold, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
# Changing black to green and converting from Grayscale to RGB
lis = []
for i in thresh:
for j in i:
if j == 0:
lis.append((0, 255, 0))
else:
lis.append((255, 255, 255))
img = np.array(lis, dtype = "uint8")
img = img.reshape(thresh.shape[0], thresh_inv.shape[], 3)
This loop changes any black pixels to green in the thresholded image.