我正在研究图像颜色识别,因此我将 RGB 图像转换为 Lab,因为它是最接近人类视觉的颜色空间。之后,我获取实验室的 3 个通道中的每一个,并希望在 3D 图形中绘制我在转换图像中识别出的颜色变化。如何使用图像的颜色绘制图形?
import cv2
import numpy as np
import urllib
import mpl_toolkits.mplot3d.axes3d as p3
import matplotlib.pyplot as plt
# Load an image that contains all possible colors.
request = urllib.urlopen('IMD021.png')
image_array = np.asarray(bytearray(request.read()), dtype=np.uint8)
image = cv2.imdecode(image_array, cv2.CV_LOAD_IMAGE_COLOR)
lab_image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
l_channel,a_channel,b_channel = cv2.split(lab_image)
fig = plt.figure()
ax = p3.Axes3D(fig)
ax.scatter(l_channel, a_channel, b_channel, marker='o', facecolors=cv2.cvtColor(image, cv2.COLOR_BGR2RGB).reshape(-1,3)/255.)
ax.set_xlabel('L')
ax.set_ylabel('A')
ax.set_zlabel('B')
fig.add_axes(ax)
#plt.savefig('plot-15.png')
plt.show()
Exit:
![enter image description here](https://i.stack.imgur.com/7B9DE.png)
这里如何获得answer亚历山大建议针对您的情况进行工作:
# only change to question's code is the ax.scatter() line:
ax.scatter(l_channel, a_channel, b_channel, marker='o',
facecolors=cv2.cvtColor(image, cv2.COLOR_BGR2RGB).reshape(-1,3)/255.)
注:facecolors
参数需要 RGB,而不是 OpenCV 的 BGR,并且对颜色数据的形状和类型很挑剔,因此需要重塑和划分。
Here the result when the code is applied to this image:![enter image description here](https://i.stack.imgur.com/OP4ZM.png)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)