Skip to content

Instantly share code, notes, and snippets.

@celoyd
Created April 19, 2019 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save celoyd/dfb4af2ba722d3b42b402d7b253cbfb3 to your computer and use it in GitHub Desktop.
Save celoyd/dfb4af2ba722d3b42b402d7b253cbfb3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# simple visualization of the Co/Cg plane of an image
# https://en.wikipedia.org/wiki/YCoCg
from skimage import io
import numpy as np
from sys import argv
src = io.imread(argv[1])
if src.dtype == np.uint16:
src = (src / 2**8).astype(np.uint8)
R, G, B = src[:,:,0], src[:,:,1], src[:,:,2]
Y = 0.25 * R + 0.5 * G + 0.25 * B
Cg = -0.25 * R + 0.5 * G - 0.25 * B + 0.5
Co = 0.5 * R - 0.5 * B + 0.5
Co = Co + 127
Cg = Cg + 127
Co = 255 - Co
dst = np.zeros((256, 256, 3), dtype=src.dtype)
for x in range(src.shape[0]):
for y in range(src.shape[1]):
dst[int(Cg[x, y]), int(Co[x, y])] = np.array([R[x, y], G[x, y], B[x, y]])
io.imsave(argv[2], dst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment