Reputation: 2036
I would like to ask you, how can I convert BGR image loaded by OpenCV (3.4.5) in python to the color space defined by the following formula:
I have following idea, buy its not correct.
import cv2
imageSource = cv2.imread("test.jpg")
A = np.array([
[ 0.06, 0.63 , 0.27],
[ 0.3 , 0.04 , -0.35],
[ 0.34, -0.6 , 0.17]
])
vis0 = cv2.multiply(imageSource, A)
Upvotes: 2
Views: 1491
Reputation: 30200
You could do something like:
import cv2
import numpy as np
# This is just a simple helper function that takes a matrix, converts it to
# the BGR colorspace (if necessary), shows it with .imshow() and waits using
# .waitKey() -- feel free to ignore it.
def show(*, img_bgr=None, img_rgb=None):
if img_bgr is None:
img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR)
cv2.imshow("title", img_bgr)
cv2.waitKey()
transform = np.array([
[ 0.06, 0.63 , 0.27],
[ 0.3 , 0.04 , -0.35],
[ 0.34, -0.6 , 0.17]
])
img_bgr = cv2.imread("lenna.png",)
# The image will be in BGR order, we want RGB order
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
# This does three things:
# - Transforms the pixels according to the transform matrix
# - Rounds the pixel values to integers
# - Coverts the datatype of the matrix to 'uint8' show .imshow() works
img_trans = np.rint(img_rgb.dot(transform.T)).astype('uint8')
show(img_bgr=img_bgr)
show(img_rgb=img_trans)
Which, from:
produces:
Note: If you were looking for something like:
Then drop the transpose (...dot(transform.T)...
-> ...dot(transform)...
)
Upvotes: 1