简体   繁体   中英

Modeling HSV color space cone with Python and Matplotlib 3D

I want to create the HSV cone in python.

Tried to adapt the solution for MATLAB given in another question :

import numpy as np
import cv2

H = np.repeat([np.linspace(0, 179, 100)], 100, axis=0)
S = np.repeat([np.concatenate((np.linspace(0, 255, 50), np.linspace(255, 0, 50)))], 100, axis=0).transpose()
V = np.repeat([np.concatenate((np.ones(50)*255, np.linspace(255, 0, 50)))], 100, axis=0).transpose()

hsv = np.asarray(cv2.merge((H, S, V)), dtype=np.uint8)
C = cv2.cvtColor(hsv, cv2.COLOR_HSV2RGB)

theta = np.linspace(0, 2*np.pi, 100)
X = np.asarray([np.zeros(100), np.cos(theta), np.zeros(100)])
Y = np.asarray([np.zeros(100), np.sin(theta), np.zeros(100)])
Z = np.asarray([2*np.ones(100), 2*np.ones(100), np.zeros(100)])

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(X, Y, Z, color=C)

but got the error: "Invalid rgba arg ... length of rgba sequence should be either 3 or 4".

Tried to reshape C with C.reshape(3, 100, 100) but got the error: "Invalid rgba arg ... only length-1 arrays can be converted to Python scalars"

Without arg color=C I've got this:

HSV cone with ax.plot_surface(X, Y, Z)

Thanks!

The error already tells you that you can only have a sequence of length 3 or 4 as argument to color . The color argument basically expects one single color (which can be given as a list of values (R,G,B) .

Fortunately, there is another argument in plot_surface() which allows you to set the color of individual faces of the plot, namely facecolors .

ax.plot_surface(X, Y, Z, facecolors=C/255.)

While this will work and not throw an error, in the plot all colors are close to gray. This is in agreement with the array C, but I'm not sure if this is to be expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM