Python matplotlib.colors.hsv_to_rgb() Examples
The following are 30
code examples of matplotlib.colors.hsv_to_rgb().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
matplotlib.colors
, or try the search function
.
Example #1
Source File: plottools.py From vnpy_crypto with MIT License | 7 votes |
def rainbow(n): """ Returns a list of colors sampled at equal intervals over the spectrum. Parameters ---------- n : int The number of colors to return Returns ------- R : (n,3) array An of rows of RGB color values Notes ----- Converts from HSV coordinates (0, 1, 1) to (1, 1, 1) to RGB. Based on the Sage function of the same name. """ from matplotlib import colors R = np.ones((1,n,3)) R[0,:,0] = np.linspace(0, 1, n, endpoint=False) #Note: could iterate and use colorsys.hsv_to_rgb return colors.hsv_to_rgb(R).squeeze()
Example #2
Source File: sourcery.py From suite2p with GNU General Public License v3.0 | 6 votes |
def drawClusters(stat, ops): Ly = ops['Lyc'] Lx = ops['Lxc'] ncells = len(stat) r=np.random.random((ncells,)) iclust = -1*np.ones((Ly,Lx),np.int32) Lam = np.zeros((Ly,Lx)) H = np.zeros((Ly,Lx,1)) for n in range(ncells): isingle = Lam[stat[n]['ypix'],stat[n]['xpix']]+1e-4 < stat[n]['lam'] y = stat[n]['ypix'][isingle] x = stat[n]['xpix'][isingle] Lam[y,x] = stat[n]['lam'][isingle] #iclust[ypix,xpix] = n*np.ones(ypix.shape) H[y,x,0] = r[n]*np.ones(y.shape) S = np.ones((Ly,Lx,1)) V = np.maximum(0, np.minimum(1, 0.75 * Lam / Lam[Lam>1e-10].mean())) V = np.expand_dims(V,axis=2) hsv = np.concatenate((H,S,V),axis=2) rgb = hsv_to_rgb(hsv) return rgb
Example #3
Source File: pixelated_stem_tools.py From pyxem with GNU General Public License v3.0 | 6 votes |
def _get_rgb_phase_magnitude_array( phase, magnitude, rotation=None, magnitude_limits=None, max_phase=2 * np.pi ): phase = _find_phase(phase, rotation=rotation, max_phase=max_phase) phase = phase / (2 * np.pi) if magnitude_limits is not None: np.clip(magnitude, magnitude_limits[0], magnitude_limits[1], out=magnitude) magnitude_max = magnitude.max() if magnitude_max == 0: magnitude_max = 1 magnitude = magnitude / magnitude_max S = np.ones_like(phase) HSV = np.dstack((phase, S, magnitude)) RGB = hsv_to_rgb(HSV) return RGB
Example #4
Source File: io_utils.py From RefRESH with MIT License | 6 votes |
def flow_visualize(flow, max_range = 1e3): """ Original code from SINTEL toolbox, by Jonas Wulff. """ import matplotlib.colors as colors du = flow[:, :, 0] dv = flow[:, :, 1] [h,w] = du.shape max_flow = min(max_range, np.max(np.sqrt(du * du + dv * dv))) img = np.ones((h, w, 3), dtype=np.float64) # angle layer img[:, :, 0] = (np.arctan2(dv, du) / (2 * np.pi) + 1) % 1.0 # magnitude layer, normalized to 1 img[:, :, 1] = np.sqrt(du * du + dv * dv) / (max_flow + 1e-8) # phase layer #img[:, :, 2] = valid # convert to rgb img = colors.hsv_to_rgb(img) # remove invalid point img[:, :, 0] = img[:, :, 0] img[:, :, 1] = img[:, :, 1] img[:, :, 2] = img[:, :, 2] return img
Example #5
Source File: kaleidoscope.py From pywonderland with MIT License | 6 votes |
def main(imgsize): y, x = np.ogrid[6: -6: imgsize*2j, -6: 6: imgsize*2j] z = x + y*1j z = RiemannSphere(Klein(Mobius(Klein(z)))) # define colors in hsv space H = np.sin(z[0]*np.pi)**2 S = np.cos(z[1]*np.pi)**2 V = abs(np.sin(z[2]*np.pi) * np.cos(z[2]*np.pi))**0.2 HSV = np.stack((H, S, V), axis=2) # transform to rgb space img = hsv_to_rgb(HSV) fig = plt.figure(figsize=(imgsize/100.0, imgsize/100.0), dpi=100) ax = fig.add_axes([0, 0, 1, 1], aspect=1) ax.axis('off') ax.imshow(img) fig.savefig('kaleidoscope.png')
Example #6
Source File: flow.py From EVDodgeNet with BSD 3-Clause "New" or "Revised" License | 6 votes |
def colorize_image(self, flow_x, flow_y): if self.hsv_buffer is None: self.hsv_buffer = np.empty((flow_x.shape[0], flow_x.shape[1],3)) self.hsv_buffer[:,:,1] = 1.0 self.hsv_buffer[:,:,0] = (np.arctan2(flow_y,flow_x)+np.pi)/(2.0*np.pi) self.hsv_buffer[:,:,2] = np.linalg.norm( np.stack((flow_x,flow_y), axis=0), axis=0 ) # self.hsv_buffer[:,:,2] = np.log(1.+self.hsv_buffer[:,:,2]) # hopefully better overall dynamic range in final video flat = self.hsv_buffer[:,:,2].reshape((-1)) m = np.nanmax(flat[np.isfinite(flat)]) if not np.isclose(m, 0.0): self.hsv_buffer[:,:,2] /= m return colors.hsv_to_rgb(self.hsv_buffer)
Example #7
Source File: drawroi.py From suite2p with GNU General Public License v3.0 | 6 votes |
def create_masks_of_cells(self, mean_img): H = np.zeros_like(mean_img) S = np.zeros_like(mean_img) columncol = self.parent.colors['istat'][0] for n in np.arange(np.shape(self.parent.iscell)[0]): if self.parent.iscell[n] == 1: ypix = self.parent.stat[n]['ypix'].flatten() xpix = self.parent.stat[n]['xpix'].flatten() H[ypix, xpix] = np.random.rand() S[ypix, xpix] = 1 pix = np.concatenate(((H[:, :, np.newaxis]), S[:, :, np.newaxis], mean_img[:, :, np.newaxis]), axis=-1) pix = hsv_to_rgb(pix) return pix
Example #8
Source File: plotting.py From RGAN with MIT License | 6 votes |
def visualise_latent(Z, identifier): """ visualise a SINGLE point in the latent space """ seq_length = Z.shape[0] latent_dim = Z.shape[1] if latent_dim > 2: print('WARNING: Only visualising first two dimensions of latent space.') h = np.random.random() colours = np.array([hsv_to_rgb((h, i/seq_length, 0.96)) for i in range(seq_length)]) # plt.plot(Z[:, 0], Z[:, 1], c='grey', alpha=0.5) for i in range(seq_length): plt.scatter(Z[i, 0], Z[i, 1], marker='o', c=colours[i]) plt.savefig('./experiments/plots/' + identifier + '_Z.png') plt.clf() plt.close() return True # --- to do with the model --- #
Example #9
Source File: plottools.py From Splunking-Crime with GNU Affero General Public License v3.0 | 6 votes |
def rainbow(n): """ Returns a list of colors sampled at equal intervals over the spectrum. Parameters ---------- n : int The number of colors to return Returns ------- R : (n,3) array An of rows of RGB color values Notes ----- Converts from HSV coordinates (0, 1, 1) to (1, 1, 1) to RGB. Based on the Sage function of the same name. """ from matplotlib import colors R = np.ones((1,n,3)) R[0,:,0] = np.linspace(0, 1, n, endpoint=False) #Note: could iterate and use colorsys.hsv_to_rgb return colors.hsv_to_rgb(R).squeeze()
Example #10
Source File: compute_flow.py From mvsec with MIT License | 6 votes |
def colorize_image(self, flow_x, flow_y): if self.hsv_buffer is None: self.hsv_buffer = np.empty((flow_x.shape[0], flow_x.shape[1],3)) self.hsv_buffer[:,:,1] = 1.0 self.hsv_buffer[:,:,0] = (np.arctan2(flow_y,flow_x)+np.pi)/(2.0*np.pi) self.hsv_buffer[:,:,2] = np.linalg.norm( np.stack((flow_x,flow_y), axis=0), axis=0 ) # self.hsv_buffer[:,:,2] = np.log(1.+self.hsv_buffer[:,:,2]) # hopefully better overall dynamic range in final video flat = self.hsv_buffer[:,:,2].reshape((-1)) m = np.nanmax(flat[np.isfinite(flat)]) if not np.isclose(m, 0.0): self.hsv_buffer[:,:,2] /= m return colors.hsv_to_rgb(self.hsv_buffer)
Example #11
Source File: utils.py From Relational-NEM with MIT License | 5 votes |
def get_gamma_colors(nr_colors): hsv_colors = np.ones((nr_colors, 3)) hsv_colors[:, 0] = (np.linspace(0, 1, nr_colors, endpoint=False) + 2/3) % 1.0 color_conv = hsv_to_rgb(hsv_colors) return color_conv
Example #12
Source File: mosaicplot.py From Splunking-Crime with GNU Affero General Public License v3.0 | 5 votes |
def _single_hsv_to_rgb(hsv): """Transform a color from the hsv space to the rgb.""" from matplotlib.colors import hsv_to_rgb return hsv_to_rgb(array(hsv).reshape(1, 1, 3)).reshape(3)
Example #13
Source File: IDAtropy.py From IDAtropy with GNU General Public License v3.0 | 5 votes |
def gen_rand_colors(n_colors=20): """ https://martin.ankerl.com/2009/12/09/how-to-create-random-colors-programmatically/ """ colors = [] golden_ratio_conjugate = 0.618033988749895 h = random.random() for i in range(n_colors): h += golden_ratio_conjugate h %= 1 values = hsv_to_rgb([h, 0.3, 0.95]) res = "#" + ''.join(["%02X" % int(val*256) for val in values]) colors.append(res) return colors
Example #14
Source File: util.py From demo2program with MIT License | 5 votes |
def visualize_flow(x, y): img_batch = [] h, w = x.shape[-2:] for i in range(x.shape[1]): img_time_step = [] for j in range(x.shape[0]): du = x[j, i] dv = y[j, i] # valid = flow[:, :, 2] max_flow = max(np.max(du), np.max(dv)) img = np.zeros((h, w, 3), dtype=np.float64) # angle layer img[:, :, 0] = np.arctan2(dv, du) / (2 * np.pi) # magnitude layer, normalized to 1 img[:, :, 1] = np.sqrt(du * du + dv * dv) * 8 / max_flow # phase layer img[:, :, 2] = 8 - img[:, :, 1] # clip to [0,1] small_idx = img < 0 large_idx = img > 1 img[small_idx] = 0 img[large_idx] = 1 # convert to rgb img = cl.hsv_to_rgb(img) img_time_step.append(img) img_time_step = np.stack(img_time_step, axis=-1) img_time_step = np.transpose(img_time_step, [0, 1, 3, 2]) img_time_step = np.reshape(img_time_step, [h, w*x.shape[0], 3]) img_batch.append(img_time_step) return np.stack(img_batch, axis=0).astype(np.float32)
Example #15
Source File: utils.py From Neural-EM with MIT License | 5 votes |
def get_gamma_colors(nr_colors): hsv_colors = np.ones((nr_colors, 3)) hsv_colors[:, 0] = (np.linspace(0, 1, nr_colors, endpoint=False) + 2/3) % 1.0 color_conv = hsv_to_rgb(hsv_colors) return color_conv
Example #16
Source File: test_colors.py From ImageFusion with MIT License | 5 votes |
def test_rgb_hsv_round_trip(): for a_shape in [(500, 500, 3), (500, 3), (1, 3), (3,)]: np.random.seed(0) tt = np.random.random(a_shape) assert_array_almost_equal(tt, mcolors.hsv_to_rgb(mcolors.rgb_to_hsv(tt))) assert_array_almost_equal(tt, mcolors.rgb_to_hsv(mcolors.hsv_to_rgb(tt)))
Example #17
Source File: drawroi.py From suite2p with GNU General Public License v3.0 | 5 votes |
def __init__(self, iROI, parent=None, pos=None, diameter=None, color=None, yrange=None, xrange=None): # what type of ROI it is self.iROI = iROI self.xrange = xrange self.yrange = yrange if color is None: self.color = hsv_to_rgb(np.array([np.random.rand() / 1.4 + 0.1, 1, 1])) self.color = tuple(255 * self.color) else: self.color = color if pos is None: view = parent.p0.viewRange() imx = (view[0][1] + view[0][0]) / 2 imy = (view[1][1] + view[1][0]) / 2 dx = (view[0][1] - view[0][0]) / 4 dy = (view[1][1] - view[1][0]) / 4 if diameter is None: dx = np.maximum(3, np.minimum(dx, parent.Ly * 0.2)) dy = np.maximum(3, np.minimum(dy, parent.Lx * 0.2)) else: dx = diameter dy = diameter imx = imx - dx / 2 imy = imy - dy / 2 else: imy = pos[0] imx = pos[1] dy = pos[2] dx = pos[3] self.draw(parent, imy, imx, dy, dx) self.position(parent) self.ROI.sigRegionChangeFinished.connect(lambda: self.position(parent)) self.ROI.sigClicked.connect(lambda: self.position(parent)) self.ROI.sigRemoveRequested.connect(lambda: self.remove(parent))
Example #18
Source File: masks.py From suite2p with GNU General Public License v3.0 | 5 votes |
def rgb_masks(parent, col, c): for i in range(2): #S = np.expand_dims(parent.rois['Sroi'][i],axis=2) H = col[parent.rois['iROI'][i,0], :] #H = np.expand_dims(H,axis=2) #hsv = np.concatenate((H,S,S),axis=2) #rgb = (hsv_to_rgb(hsv)*255).astype(np.uint8) parent.colors['RGB'][i,c,:,:,:3] = H
Example #19
Source File: mapf_gym_cap.py From distributedRL_MAPF with MIT License | 5 votes |
def initColors(self): c={a+1:hsv_to_rgb(np.array([a/float(self.num_agents),1,1])) for a in range(self.num_agents)} return c
Example #20
Source File: mapf_gym.py From distributedRL_MAPF with MIT License | 5 votes |
def initColors(self): c={a+1:hsv_to_rgb(np.array([a/float(self.num_agents),1,1])) for a in range(self.num_agents)} return c
Example #21
Source File: flowlib.py From flownet2-tf with MIT License | 5 votes |
def visualize_flow(flow, mode='Y'): """ this function visualize the input flow :param flow: input flow in array :param mode: choose which color mode to visualize the flow (Y: Ccbcr, RGB: RGB color) :return: None """ if mode == 'Y': # Ccbcr color wheel img = flow_to_image(flow) plt.imshow(img) plt.show() elif mode == 'RGB': (h, w) = flow.shape[0:2] du = flow[:, :, 0] dv = flow[:, :, 1] valid = flow[:, :, 2] max_flow = max(np.max(du), np.max(dv)) img = np.zeros((h, w, 3), dtype=np.float64) # angle layer img[:, :, 0] = np.arctan2(dv, du) / (2 * np.pi) # magnitude layer, normalized to 1 img[:, :, 1] = np.sqrt(du * du + dv * dv) * 8 / max_flow # phase layer img[:, :, 2] = 8 - img[:, :, 1] # clip to [0,1] small_idx = img[:, :, 0:3] < 0 large_idx = img[:, :, 0:3] > 1 img[small_idx] = 0 img[large_idx] = 1 # convert to rgb img = cl.hsv_to_rgb(img) # remove invalid point img[:, :, 0] = img[:, :, 0] * valid img[:, :, 1] = img[:, :, 1] * valid img[:, :, 2] = img[:, :, 2] * valid # show plt.imshow(img) plt.show() return None
Example #22
Source File: pixelated_stem_tools.py From pyxem with GNU General Public License v3.0 | 5 votes |
def _get_rgb_phase_array(phase, rotation=None, max_phase=2 * np.pi, phase_lim=None): phase = _find_phase(phase, rotation=rotation, max_phase=max_phase) phase = phase / (2 * np.pi) S = np.ones_like(phase) HSV = np.dstack((phase, S, S)) RGB = hsv_to_rgb(HSV) return RGB
Example #23
Source File: color.py From phy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _random_color(h_range=(0., 1.), s_range=(.5, 1.), v_range=(.5, 1.)): """Generate a random RGB color.""" h, s, v = uniform(*h_range), uniform(*s_range), uniform(*v_range) r, g, b = hsv_to_rgb(np.array([[[h, s, v]]])).flat return r, g, b
Example #24
Source File: color.py From phy with BSD 3-Clause "New" or "Revised" License | 5 votes |
def _override_hsv(rgb, h=None, s=None, v=None): h_, s_, v_ = rgb_to_hsv(np.array([[rgb]])).flat h = h if h is not None else h_ s = s if s is not None else s_ v = v if v is not None else v_ r, g, b = hsv_to_rgb(np.array([[[h, s, v]]])).flat return r, g, b #------------------------------------------------------------------------------ # Colormap utilities #------------------------------------------------------------------------------
Example #25
Source File: flowlib.py From DF-Net with MIT License | 5 votes |
def visualize_flow(flow, mode='Y'): """ this function visualize the input flow :param flow: input flow in array :param mode: choose which color mode to visualize the flow (Y: Ccbcr, RGB: RGB color) :return: None """ if mode == 'Y': # Ccbcr color wheel img = flow_to_image(flow) plt.imshow(img) plt.show() elif mode == 'RGB': (h, w) = flow.shape[0:2] du = flow[:, :, 0] dv = flow[:, :, 1] valid = flow[:, :, 2] max_flow = max(np.max(du), np.max(dv)) img = np.zeros((h, w, 3), dtype=np.float64) # angle layer img[:, :, 0] = np.arctan2(dv, du) / (2 * np.pi) # magnitude layer, normalized to 1 img[:, :, 1] = np.sqrt(du * du + dv * dv) * 8 / max_flow # phase layer img[:, :, 2] = 8 - img[:, :, 1] # clip to [0,1] small_idx = img[:, :, 0:3] < 0 large_idx = img[:, :, 0:3] > 1 img[small_idx] = 0 img[large_idx] = 1 # convert to rgb img = cl.hsv_to_rgb(img) # remove invalid point img[:, :, 0] = img[:, :, 0] * valid img[:, :, 1] = img[:, :, 1] * valid img[:, :, 2] = img[:, :, 2] * valid # show plt.imshow(img) plt.show() return None
Example #26
Source File: test_colors.py From coffeegrindsize with MIT License | 5 votes |
def test_rgb_hsv_round_trip(): for a_shape in [(500, 500, 3), (500, 3), (1, 3), (3,)]: np.random.seed(0) tt = np.random.random(a_shape) assert_array_almost_equal(tt, mcolors.hsv_to_rgb(mcolors.rgb_to_hsv(tt))) assert_array_almost_equal(tt, mcolors.rgb_to_hsv(mcolors.hsv_to_rgb(tt)))
Example #27
Source File: FlowUtils.py From EVDodgeNet with BSD 3-Clause "New" or "Revised" License | 5 votes |
def visualize_flow(flow, mode='Y'): """ this function visualize the input flow :param flow: input flow in array :param mode: choose which color mode to visualize the flow (Y: Ccbcr, RGB: RGB color) :return: None """ if mode == 'Y': # Ccbcr color wheel img = flow_to_image(flow) plt.imshow(img) plt.show() elif mode == 'RGB': (h, w) = flow.shape[0:2] du = flow[:, :, 0] dv = flow[:, :, 1] valid = flow[:, :, 2] max_flow = max(np.max(du), np.max(dv)) img = np.zeros((h, w, 3), dtype=np.float64) # angle layer img[:, :, 0] = np.arctan2(dv, du) / (2 * np.pi) # magnitude layer, normalized to 1 img[:, :, 1] = np.sqrt(du * du + dv * dv) * 8 / max_flow # phase layer img[:, :, 2] = 8 - img[:, :, 1] # clip to [0,1] small_idx = img[:, :, 0:3] < 0 large_idx = img[:, :, 0:3] > 1 img[small_idx] = 0 img[large_idx] = 1 # convert to rgb img = cl.hsv_to_rgb(img) # remove invalid point img[:, :, 0] = img[:, :, 0] * valid img[:, :, 1] = img[:, :, 1] * valid img[:, :, 2] = img[:, :, 2] * valid # show plt.imshow(img) plt.show() return None
Example #28
Source File: circle_plot.py From pyrealtime with MIT License | 5 votes |
def update_fig(self, data): self.series[0].set_data(0, data) hue = self.hue_transfer_function(data) color = hsv_to_rgb([hue, 1, 0.75]) self.series[0].set_color(color) return self.series
Example #29
Source File: test_colors.py From twitter-stock-recommendation with MIT License | 5 votes |
def test_rgb_hsv_round_trip(): for a_shape in [(500, 500, 3), (500, 3), (1, 3), (3,)]: np.random.seed(0) tt = np.random.random(a_shape) assert_array_almost_equal(tt, mcolors.hsv_to_rgb(mcolors.rgb_to_hsv(tt))) assert_array_almost_equal(tt, mcolors.rgb_to_hsv(mcolors.hsv_to_rgb(tt)))
Example #30
Source File: flowlib.py From flownet2.pytorch with Apache License 2.0 | 5 votes |
def visualize_flow(flow, mode='Y'): """ this function visualize the input flow :param flow: input flow in array :param mode: choose which color mode to visualize the flow (Y: Ccbcr, RGB: RGB color) :return: None """ if mode == 'Y': # Ccbcr color wheel img = flow_to_image(flow) plt.imshow(img) plt.show() elif mode == 'RGB': (h, w) = flow.shape[0:2] du = flow[:, :, 0] dv = flow[:, :, 1] valid = flow[:, :, 2] max_flow = max(np.max(du), np.max(dv)) img = np.zeros((h, w, 3), dtype=np.float64) # angle layer img[:, :, 0] = np.arctan2(dv, du) / (2 * np.pi) # magnitude layer, normalized to 1 img[:, :, 1] = np.sqrt(du * du + dv * dv) * 8 / max_flow # phase layer img[:, :, 2] = 8 - img[:, :, 1] # clip to [0,1] small_idx = img[:, :, 0:3] < 0 large_idx = img[:, :, 0:3] > 1 img[small_idx] = 0 img[large_idx] = 1 # convert to rgb img = cl.hsv_to_rgb(img) # remove invalid point img[:, :, 0] = img[:, :, 0] * valid img[:, :, 1] = img[:, :, 1] * valid img[:, :, 2] = img[:, :, 2] * valid # show plt.imshow(img) plt.show() return None