Skip to content

Instantly share code, notes, and snippets.

@celoyd
Last active February 10, 2023 16:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save celoyd/16d075620273e35c71c3d41a524d4cf1 to your computer and use it in GitHub Desktop.
Save celoyd/16d075620273e35c71c3d41a524d4cf1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# simple visualization of the Cb/Cr plane of an image
# python vectorscope.py input.png output.png
# (supports whatever image formats your skimage does)
# bugs include: everything is in painter order
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.299 * R) + (0.587 * G) + (0.114 * B)
Cb = (-0.169 * R) - (0.331 * G) + (0.499 * B) + 128
Cr = (0.499 * R) - (0.418 * G) - (0.0813 * B) + 128
# traditional vectorscope orientation:
Cr = 256 - Cr
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(Cr[x, y]), int(Cb[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