Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

[Test Sample Dots Print]

Here is the 3dContour plot of the same test fabric

Ultra Cotton Plot

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

[Awesomeness from 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 split for Hue Sat and 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

[same as before better viewing angle]

like image 630
Krackle Avatar asked Mar 19 '26 09:03

Krackle


1 Answers

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')

enter image description here

like image 134
Diziet Asahi Avatar answered Mar 20 '26 21:03

Diziet Asahi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!