python - 如何使用图像每个像素的值绘制 3d 图形?

标签 python python-3.x opencv image-processing histogram

我按如下方式将图片的颜色转换为 LAB:

import cv2
imbgr=cv2.imread('photo.jpg')
imlab=cv2.cvtColor(imbgr,cv2.COLOR_BGR2LAB)
cv2.imwrite('lab.jpg',imlab)

用 imlab [x, y] 返回像素,如何用这些值绘制图形?

最佳答案

以下是如何将图像和图形图像数据显示为 3 维数据的几个示例。

第一张和第二张图显示了原始 BGR 图像及其各个 channel 作为 BGR,然后作为 LAB。

第三张和第四张图显示了使用 LAB 图像的第一个 channel 作为三维数据的等高线图和曲面图。

旁白:请注意 imshow() 需要一个 RGB 图像。并且,如果需要,可以使用 aspect 关键字 aspect='equal' 或 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()

这里是代码生成的图像,如所列。

图(0) - 原始图像和各个颜色 channel

enter image description here

图(1) - LAB 图像和各个 channel

enter image description here

图(2)-第一个LAB channel 等高线图

enter image description here

图(3) - 第一个实验室 channel 的表面图

enter image description here

关于python - 如何使用图像每个像素的值绘制 3d 图形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52781845/

相关文章:

python - 如何从 Python 中的线程获取返回值?

python - 使用python解析大(9GB)文件

linux - 如何使用 python 3.x 运行程序

python - 用于数独解题器的从水平行列表中获取部分的一种简短(呃)方法

python - Keras:ModelCheckpoint 保存到变量而不是文件?

numpy - 轴0超出维度0数组的范围

ios - opencv2.framework/opencv2(surf.o) 架构armv7的重复符号

python - 如何根据与其他匹配项的接近程度来过滤外围匹配项?

c++ - 在 boost Homebrew 软件安装中找不到 bjam

python - 如何使用 splinter 填充表格内的输入框?