2

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?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Tecnologia da Net
  • 215
  • 3
  • 7
  • 23
  • A very similar question (but where the OP got a bit farther than you) was just asked a little while ago---see [here](https://stackoverflow.com/questions/52788407/how-to-plot-graphics-with-the-colors-of-each-pixel-in-the-image). This will plot all the markers in the correct place, and then you can follow my comment on that question to see how to map the marker colors to the image color there as well. – alkasm Oct 13 '18 at 01:29
  • Okay, but the question that sent me is just the graph, and you can not get the colors – Tecnologia da Net Oct 13 '18 at 13:16
  • I answered the question Alexander pointed out. Does the answer solve your problem? – Ulrich Stern Oct 15 '18 at 13:11

1 Answers1

6

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

enter image description here

Figure(1) - LAB image and individual channels

enter image description here

Figure(2) - Contour graph of first LAB channel

enter image description here

Figure(3) - surface plot of first LAB channel

enter image description here

DrM
  • 2,404
  • 15
  • 29