From How to change black color to Red with OpenCV Python? I found this code
import numpy as np
import imutils
import cv2
img_rgb = cv2.imread('black.png')
Conv_hsv_Gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(Conv_hsv_Gray, 0, 255,cv2.THRESH_BINARY_INV |cv2.THRESH_OTSU)
img_rgb[mask == 255] = [0, 0, 255]
cv2.imshow("imgOriginal", img_rgb) # show windows
cv2.imshow("mask", mask) # show windows
cv2.waitKey(0)
is there any way i could change the line
img_rgb[mask == 255] = [0, 0, 255]
or something else to make it change a range of colors? for example:
([255, 255, 0], [255, 55, 10])
Yes you can.
First, you must create a mask of the color range to change, and the answer for that is the inRange function of OpenCV.
Then, via numpy, you can say where the mask is not 0 paint them in my image red. This is the code for that:
import numpy as np
import cv2
# load image and set the bounds
img = cv2.imread("D:\\debug\\HLS.png")
lower =(255, 55, 0) # lower bound for each channel
upper = (255, 255, 10) # upper bound for each channel
# create the mask and use it to change the colors
mask = cv2.inRange(img, lower, upper)
img[mask != 0] = [0,0,255]
# display it
cv2.imshow("frame", img)
cv2.waitKey(0)
If you want actually to get a range of colors (e.g. all blue colors) it is better to use HLS or HSV color spaces.