如何用图像中每个像素的颜色绘制图形?

卡洛斯·迭戈

我正在研究图像颜色识别,因此我将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()

出口: 在此处输入图片说明

乌尔里希·斯特恩

在这里,如何获得亚历山大建议您使用答案

# 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,并且对颜色数据的形状和类型很挑剔,因此需要重塑和划分。

这是将代码应用于此图像时的结果在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章