gpt4 book ai didi

python - 如何从图像中提取 RGB 并仅将 RG 绘制为图形? R代表X,G代表Y

转载 作者:太空宇宙 更新时间:2023-11-03 21:11:31 27 4
gpt4 key购买 nike

我正在尝试从图像中提取 RGB 分量并使用 matplotlib 绘制 3D RGB 直方图。但我不知道我该怎么做。

这是我当前的代码:

import cv2
import numpy as np
from scipy import ndimage
from matplotlib import pyplot as plt

img_file = 'Paw03.png'
img = cv2.imread(img_file, cv2.IMREAD_COLOR) # rgb
#hsv_img = cv2.cvtColor(img,cv2.COLOR_BGR2HSV) # hsv

rows, cols, ch = img.shape

for x in range(rows):
for y in range(cols):
if (img[x, y, 1] == img[0, 255, 0]):
break;
else:
print "Pixel:", x, y
print "R:", R
print "G:", g
print "B:", b
print "\n"

plt.plot(r, 'ro', b, 'b^')
plt.xlim([0, 255])
plt.xlabel('Pixel')
plt.ylabel('Quantity')
plt.title('Distribution of RGB in the image')
plt.show()

但它不起作用!

所以,我尝试了三个:

import cv2
import numpy as np
from scipy import ndimage
from matplotlib import pyplot as plt

img_file = 'Paw03.png'
img = cv2.imread(img_file, cv2.IMREAD_COLOR) # rgb
#hsv_img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) # hsv

rows, cols, ch = img.shape

for x in range(rows):
for y in range(cols):
for z in range(ch)
if (img[x, y, z] == img[0, 255, 0]):
break;
else:
print "Pixel:", x, y
print "R:", R
print "G:", g
print "B:", b
print "\n"

plt.plot(r, 'ro', b, 'b^')
plt.xlim([0, 255])
plt.xlabel('Pixel')
plt.ylabel('Quantity')
plt.title('Distribution of RGB in the image')
plt.show()

它只适用于打印到 for 并且每个像素也保存三次,而对于 matplotlib 它不起作用。

谁能帮帮我?

最佳答案

以下代码片段显示了图像 RGB 颜色的 3D 散点图:

import numpy as np
import matplotlib.image as mpimg
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

img = mpimg.imread('Paw03.png')
pixels = img.shape[0]*img.shape[1]
channels = 3
data = np.reshape(img[:, :, :channels], (pixels, channels))

histo_rgb, _ = np.histogramdd(data, bins=256)
r, g, b = np.nonzero(histo_rgb)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter3D(r, g, b)
ax.set_xlabel('Red')
ax.set_ylabel('Green')
ax.set_zlabel('Blue')
plt.title('RGB colors')
plt.show()

这是您运行上面的代码时得到的结果(结果显然取决于所使用的特定图像): 3D scatter plot of the RGB colors of an image

如果您的目标是在 3D 中可视化红色和绿色 channel 强度的 2D 直方图,那么您可能会发现此代码很有用:

import numpy as np
import matplotlib.image as mpimg
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

img = mpimg.imread('Paw03.png')
pixels = img.shape[0]*img.shape[1]
channels = 3
data = np.reshape(img[:, :, :channels], (pixels, channels))

histo_rgb, _ = np.histogramdd(data, bins=256)
histo_rg = np.sum(histo_rgb, 2)
levels = np.arange(256)

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
for g in levels:
ax.bar(levels, histo_rg[:, g], zs=g, zdir='y', color='r')
ax.set_xlabel('Red')
ax.set_ylabel('Green')
ax.set_zlabel('Number of pixels')
plt.show()

这是相应的输出: 3D representation of the joint RG histogram of an image

关于python - 如何从图像中提取 RGB 并仅将 RG 绘制为图形? R代表X,G代表Y,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36700674/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com