banner
Matrix

Matrix

Abyss
email
github

Image Color Conversion

One requirement is to convert the red part of an image to the blue part. Initially, one approach could be to solve this by exchanging channels, but this may result in some mixed color transformations. For example, orange turning into purple (yellow + red || yellow-green + blue).

Another approach is color space transformation. When a problem cannot be solved within one space, it can be transformed through coordinate transformation to another space for solving, and then transformed back. In the other space, our operations will not impose unexpected operations on the original space.

Implementation code:

import cv2
import numpy as np

image = cv2.imread('C:/Users/admin/Desktop/test.png')

HSVImage = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

lowerRed1 = np.array([0, 64, 50])
upperRed1 = np.array([10, 255, 255])
lowerRed2 = np.array([160, 64, 50])
upperRed2 = np.array([180, 255, 255])

redMask1 = cv2.inRange(HSVImage, lowerRed1, upperRed1)
redMask2 = cv2.inRange(HSVImage, lowerRed2, upperRed2)
fullMask = redMask1 + redMask2

blueHue = 60
HSVImage[:, :, 0] = np.where(fullMask == 255, blueHue, HSVImage[:, :, 0])

finalImage = cv2.cvtColor(HSVImage, cv2.COLOR_HSV2BGR)

cv2.imwrite('C:/Users/admin/Desktop/out.png', finalImage)
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.