I convert a picture's colors to LAB as follows:
import cv2
imbgr=cv2.imread('photo.jpg')
imlab=cv2.cvtColor(imbgr,cv2.COLOR_BGR2LAB)
cv2.imwrite('lab.jpg',imlab)
return the pixels with imlab [x, y], how to plot the graph with these values?
I convert a picture's colors to LAB as follows:
import cv2
imbgr=cv2.imread('photo.jpg')
imlab=cv2.cvtColor(imbgr,cv2.COLOR_BGR2LAB)
cv2.imwrite('lab.jpg',imlab)
return the pixels with imlab [x, y], how to plot the graph with these values?
Here are a few examples of how to show images and graph image data as 3-d data.
The first and second figures show the original BGR image and its individual channels as BGR, and then as LAB.
The third and fourth figures show a contour map and a surface plot using the first channels of the LAB image as 3-D data.
Aside: Notice that imshow() needs an RGB image. And, if desired, the contour plot can be made square, using the aspect keyword, aspect='equal' or, set_aspect().
import cv2
import numpy as np
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
# for the surface map
from mpl_toolkits.mplot3d import Axes3D
imbgr = cv2.imread('Mona_Lisa.jpg')
imrgb = cv2.cvtColor(imbgr, cv2.COLOR_BGR2RGB)
imlab=cv2.cvtColor(imbgr,cv2.COLOR_BGR2LAB)
# Show the original image and individual color channels
plt.figure(0)
plt.subplot(2,2,1)
plt.imshow( imrgb )
plt.subplot(2,2,2)
plt.imshow(imbgr[:,:,0], cmap='Blues')
plt.subplot(2,2,3)
plt.imshow(imbgr[:,:,1], cmap='Greens')
plt.subplot(2,2,4)
plt.imshow(imbgr[:,:,2], cmap='Reds')
plt.show()
# show the LAB space iamge
plt.figure(1)
plt.subplot(2,2,1)
plt.imshow( imrgb )
plt.subplot(2,2,2)
plt.imshow(imlab[:,:,0], cmap='Greys')
plt.subplot(2,2,3)
plt.imshow(imbgr[:,:,1], cmap='cool')
plt.subplot(2,2,4)
plt.imshow(imbgr[:,:,2], cmap='cool')
plt.show()
# contour map
plt.figure(2)
y = range( imlab.shape[0] )
x = range( imlab.shape[1] )
X, Y = np.meshgrid(x, y)
plt.contour( X, Y, imlab[:,:,0], 50 )
plt.show()
# surface map
plt.figure(3)
ax = plt.axes(projection='3d')
y = range( imlab.shape[0] )
x = range( imlab.shape[1] )
X, Y = np.meshgrid(x, y)
ax.plot_surface( X, Y, imlab[:,:,0] )
plt.show()
And here are the images generated by the code, as listed.
Figure(0) - original image and individual color channels
Figure(1) - LAB image and individual channels
Figure(2) - Contour graph of first LAB channel
Figure(3) - surface plot of first LAB channel