简体   繁体   中英

3D surface plot of colorspace in python

My computer setup is Mac Mojave 10.14.4. I am new to Python so I am using Jupyter Lab so that I can understand what each part is producing so please can you respond similarly.

I want to produce a 3d surface plot of a digitally printed fabric sample with z-axis plotting the color space.

Here is the hardcopy file.

[ 测试样品点打印 ]

Here is the 3dContour plot of the same test fabric

超棉地块

img = cv2.imread(testROYGBIVB.jpg) img - cv2.cvtColor(img,  
cv2.COLOR_BGR2HSV)

plt.imshow(img)
img0 = img
img0.shape
(70, 90,3)

x, y, z = img0.T
x = np.linspace(0, 7, 70) #start, step, total
y = np.linspace(0, 9, 90)
X, Y = np.meshgrid(x, y) 
Z = np.invert(z) #makes it easier to view

font = {'family': 'sans-serif',
    'color':  'black',
    'weight': 'normal',
    'size': 16,
    }

 fig = plt.figure()

 ax = plt.axes(projection='3d')
 ax.contour3D(X, Y, Z, 256, cmap='cubehelix_r')
 ax.set_xlabel('x',fontdict=font)
 ax.set_ylabel('y',fontdict=font)
 ax.set_zlabel('z',fontdict=font); #RGB values
 ax.set_title('Ultra Cotton',fontdict=font);

 plt.tight_layout()
 plt.savefig('UltaCotton.png')
 ax.view_init(60, 35)
 fig

[ 来自 Abov 的精彩 ]

My question is this - the color space values of my plot are HSV. I can split these values as seen below to create a scatter.

But I would like to maintain the rod structure from the contour but with the color of the rods matching the defined color space HSV as seen in the scatter.

I would like my contour plot and my scatter plot to have a hybrid baby.

FYI - the z values were inverted so that the top surface would be easily visible.

Can this be done? Thanks

flags = [i for i in dir(cv2) if i.startswith('COLOR_')]
len(flags)
258
flags[40]
'COLOR_BGR2RGB'

hsv_img = cv2.cvtColor(img, cv2.COLOR_RGB2HSV)
pixel_colors = img.reshape((np.shape(img)[0]*np.shape(img)[1], 3))
norm = colors.Normalize(vmin=-1.,vmax=1.)
norm.autoscale(pixel_colors)
pixel_colors = norm(pixel_colors).tolist()

h, s, v = cv2.split(hsv_img)
fig = plt.figure()
axis = fig.add_subplot(1, 1, 1, projection="3d")
axis.scatter(h.flatten(), s.flatten(), v.flatten(), facecolors=pixel_colors,    marker=".")
axis.set_xlabel("Hue")
axis.set_ylabel("Saturation")
axis.set_zlabel("Value")
plt.show()

![X,Y,Z 拆分为 Hue Sat 和 Val ] 4

plt.tight_layout()
plt.savefig('filename.png')
axis.view_init(45, 35)
#ax.set_title('Ultra Cotton');
plt.tight_layout()
plt.savefig('filenameView.png')
fig

[ 和以前一样更好的视角 ]

Like the others, I'm confused by what you are trying to achieve.

Is this anything like what you had in mind?

img = plt.imread('Jb2Y5.jpg')
nx,ny,_ = img.shape
X, Y = np.meshgrid(np.linspace(0,ny,ny),np.linspace(0,nx,nx)) 
fig, (ax1, ax2, ax3) = plt.subplots(1,3,subplot_kw=dict(projection='3d'), figsize=(10,3))
ax1.plot_surface(X,Y, img[:,:,0], cmap="Reds", alpha=0.5)
ax1.set_title('RED')
ax2.plot_surface(X,Y, img[:,:,1], cmap='Greens', alpha=0.5)
ax2.set_title('GREEN')
ax3.plot_surface(X,Y, img[:,:,2], cmap='Blues', alpha=0.5)
ax3.set_title('BLUE')

在此处输入图片说明

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