Skip to content

Instantly share code, notes, and snippets.

@jeremy-rutman
Created December 10, 2019 08:26
Show Gist options
  • Select an option

  • Save jeremy-rutman/d2d5c1397780227a6439cf7c210fc1e0 to your computer and use it in GitHub Desktop.

Select an option

Save jeremy-rutman/d2d5c1397780227a6439cf7c210fc1e0 to your computer and use it in GitHub Desktop.
from 2d values to RGB
import cv2
import numpy as np
def get_rgb(two_d_pixels):
theta = np.arctan(two_d_pixels[1,:] / two_d_pixels[0,:])
r = np.sqrt(two_d_pixels[1,:]**2 + two_d_pixels[0,:]**2)
hue = (theta+3.14/2) * 128./(3.14) # 127 max for hue
sat = (r/np.sqrt(2)) * 255 #255 max for sat
H,W = two_d_pixels.shape[1:3]
retval = np.zeros([H,W,3],dtype = np.uint8)
retval[:,:,0] = hue
retval[:,:,1] = sat
retval[:, :, 2] = 255
retval = cv2.cvtColor(retval,cv2.COLOR_HSV2BGR)
return retval
two_d_image = np.mgrid[-1:1:0.1,-1:1:0.1]
rgb = get_rgb(two_d_image)
cv2.imshow('ok',rgb)
cv2.waitKey(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment