Skip to content

Instantly share code, notes, and snippets.

@gordinmitya
Created October 26, 2020 11:09
Show Gist options
  • Save gordinmitya/d2e79741f7986ed247063738857b5f1b to your computer and use it in GitHub Desktop.
Save gordinmitya/d2e79741f7986ed247063738857b5f1b to your computer and use it in GitHub Desktop.
rgb, ycbcr, opencv, numpy, skimage
import numpy as np
import cv2
import skimage.color
import torch
rgb2ycbcr = np.array([[65.481, 128.553, 24.966],
[-37.797, -74.203, 112.0],
[112.0, -93.786, -18.214]]).T
# from scipy import linalg
# rgb_from_ycbcr = linalg.inv(ycbcr_from_rgb)
ycbcr2rgb = np.array([[4.56621005e-03, 1.18087999e-09, 6.25892897e-03],
[4.56621005e-03, -1.53632369e-03, -3.18811095e-03],
[4.56621005e-03, 7.91071623e-03, 1.19774970e-08]])
img = (np.ones((2,2,3)) * 255).astype(np.uint8)
img[..., 0] *= 1
img[..., 1] *= 2
img[..., 2] *= 3
# img = cv2.imread('../images/butterfly.png')
mul = np.matmul(img / 255.0, rgb2ycbcr)
mul[..., 0] += 16
mul[..., 1] += 128
mul[..., 2] += 128
print('matmul', mul)
print('ski', skimage.color.rgb2ycbcr(img))
print('opencv', cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb))
@gordinmitya
Copy link
Author

output

matmul [[[234.30005882 127.41256078 128.51064314]
  [234.30005882 127.41256078 128.51064314]]
 [[234.30005882 127.41256078 128.51064314]
  [234.30005882 127.41256078 128.51064314]]]

ski [[[234.30005882 127.41256078 128.51064314]
  [234.30005882 127.41256078 128.51064314]]
 [[234.30005882 127.41256078 128.51064314]
  [234.30005882 127.41256078 128.51064314]]]

opencv [[[254 129 127]
  [254 129 127]]
 [[254 129 127]
  [254 129 127]]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment