Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Replace a range of colors with a specific color in python

I have a image with white background, grey and black part, I want to segment three parts into different color.

so far I could segment the black and grey part with inRange, but I wonder Is there a python API that substitute one color with another color, for example to substitute the black part with red color.

image description

Replace a range of colors with a specific color in python

I have a image with white background, grey and black part, I want to segment three parts into different color.

so far I could segment the black and grey part with inRange, but I wonder Is there a python API that substitute one color with another color, for example to substitute the black part with red color.

image descriptionoriginal image

Replace a range of colors with a specific color in python

I have a image with white background, grey and black part, I want to segment three parts into different color.

so far I could segment the black and grey part with inRange, but I wonder Is there a python API that substitute one color with another color, for example to substitute the black part with red color.

original image

enter code here

frame = cv2.imread("/home/ad/Desktop/1.png") cv2.imshow('frame',frame)

image after inrange

enter code here

lower_black = np.array([0,0,0], dtype = "uint16") upper_black = np.array([70,70,70], dtype = "uint16") black_mask = cv2.inRange(frame, lower_black, upper_black) cv2.imshow('mask0',black_mask)

image description

black_mask[np.where((black_mask == [0] ).all(axis = 1))] = [255]
cv2.imshow('mask1',black_mask)

Replace a range of colors with a specific color in python

I have a image with white background, grey and black part, I want to segment three parts into different color.

so far I could segment the black and grey part with inRange, but I wonder Is there a python API that substitute one color with another color, for example to substitute the black part with red color.

original image

enter code here

frame = cv2.imread("/home/ad/Desktop/1.png") cv2.imshow('frame',frame)

image after inrange

enter code here

lower_black = np.array([0,0,0], dtype = "uint16") upper_black = np.array([70,70,70], dtype = "uint16") black_mask = cv2.inRange(frame, lower_black, upper_black) cv2.imshow('mask0',black_mask)

cv2.imshow('mask0',black_mask)

image description

black_mask[np.where((black_mask == [0] ).all(axis = 1))] = [255]
cv2.imshow('mask1',black_mask)

Replace a range of colors with a specific color in python

I have a image with white background, grey and black part, I want to segment three parts into different color.

so far I could segment the black and grey part with inRange, but I wonder Is there a python API that substitute one color with another color, for example to substitute the black part with red color.

original image


original image

 frame = cv2.imread("/home/ad/Desktop/1.png")
   cv2.imshow('frame',frame)

cv2.imshow('frame',frame)

image after inrange image after inrange

lower_black = np.array([0,0,0], dtype = "uint16")
upper_black = np.array([70,70,70], dtype = "uint16")
black_mask = cv2.inRange(frame, lower_black, upper_black)
cv2.imshow('mask0',black_mask)

image description


 substitute the black color to white color
    black_mask[np.where((black_mask == [0] ).all(axis = 1))] = [255]
cv2.imshow('mask1',black_mask)
cv2.imshow('mask1',black_mask)

However, in the last image when trying to substitute the black color into the white color, only a traction of black color has been transformed, there are some parts of the black part remains to be black

Replace a range of colors with a specific color in python

I have a image with white background, grey and black part, I want to segment three parts into different color.

so far I could segment the black and grey part with inRange, but I wonder Is there a python API that substitute one color with another color, for example to substitute the black part with red color.

original image
original image

   frame = cv2.imread("/home/ad/Desktop/1.png")
   cv2.imshow('frame',frame)

image after inrange image after inrange

lower_black = np.array([0,0,0], dtype = "uint16")
upper_black = np.array([70,70,70], dtype = "uint16")
black_mask = cv2.inRange(frame, lower_black, upper_black)
cv2.imshow('mask0',black_mask)

image description substitute the black color to white color color

black_mask[np.where((black_mask == [0] ).all(axis = 1))] = [255]
    cv2.imshow('mask1',black_mask)

cv2.imshow('mask1',black_mask)

However, in the last image when trying to substitute the black color into the white color, only a traction of black color has been transformed, there are some parts of the black part remains to be black