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

时间:2018-10-13 00:08:37

标签: python python-3.x opencv image-processing graph

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

退出: enter image description here

1 个答案:

答案 0 :(得分:2)

此处介绍了如何让filters亚历山大建议您来解决此问题:

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

将代码应用于answerthis image

时的结果