Skip to content

Instantly share code, notes, and snippets.

@asanakoy
Last active August 30, 2023 09:23
Show Gist options
  • Save asanakoy/c82420afdab4d5eaa78c9f9481e462e1 to your computer and use it in GitHub Desktop.
Save asanakoy/c82420afdab4d5eaa78c9f9481e462e1 to your computer and use it in GitHub Desktop.
Convert from RGB to SUV color space
import numpy as np
from scipy.signal import convolve
from numpy import linalg
# implementation of RGB2SUV http://vision.ucsd.edu/~spmallick/research/suv/index.html
def get_rot_mat(rot_v, unit=None):
'''
takes a vector and returns the rotation matrix required to align the unit vector(2nd arg) to it
'''
if unit is None:
unit = [1.0, 0.0, 0.0]
rot_v = rot_v/np.linalg.norm(rot_v)
uvw = np.cross(rot_v, unit) #axis of rotation
rcos = np.dot(rot_v, unit) #cos by dot product
rsin = np.linalg.norm(uvw) #sin by magnitude of cross product
#normalize and unpack axis
if not np.isclose(rsin, 0):
uvw = uvw/rsin
u, v, w = uvw
# Compute rotation matrix
# Artsiom: I haven't checked that this is correct
R = (
rcos * np.eye(3) +
rsin * np.array([
[ 0, -w, v],
[ w, 0, -u],
[-v, u, 0]
]) +
(1.0 - rcos) * uvw[:,None] * uvw[None,:]
)
return R
def RGBToSUV(I_rgb,rot_vec):
'''
your implementation which takes an RGB image and a vector encoding the orientation of S channel wrt to RGB
'''
I_suv=np.zeros(I_rgb.shape)
R=get_rot_mat(rot_vec,unit=None)
for i in range(I_rgb.shape[0]):
for j in range(I_rgb.shape[1]):
I_suv[i,j,:]=np.matmul(R,I_rgb[i,j,:])
S=I_suv[:,:,0]
G=np.sqrt(I_suv[:,:,1]**2 + I_suv[:,:,2]**2)
return (S,G)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment