2016-04-18 46 views
3

我試圖從圖像中提取RGB分量並使用matplotlib繪製3D RGB histogrma。但我不知道我該怎麼做。如何從圖像中提取RGB並僅將RG繪製爲圖形? R爲X和G爲Y

這裏是我當前的代碼:

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() 

它僅適用於打印成的,也保存三次,每次像素,併爲matplotlib這是行不通的。

任何人都可以幫到我嗎?

回答

1

下面的代碼片段顯示圖像的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

如果你的目標是在三維可視化的紅色和綠色通道的強度的二維直方圖,然後你會發現這個代碼有用:

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

+0

我試過了,它像柱狀圖一樣返回給我一個輸出。但我想打印像這樣的圖像:http://machinethatsees.blogspot.com.br/2012/07/how-to-plot-color-in-3d-rgb-color-space.html,但在二維。因此,R代表X軸,G代表Y軸。你明白我了嗎?如果你能幫忙,我的電子郵件是:[email protected]。我們可以在這裏比這裏更容易溝通。謝謝你的幫助! – Lucas

0

我看不出在您的代碼示例中設置值R,g和b的位置。此外,它看起來像plot.plot(r,'ro',b,'b ^')不會繪製系列,而是一個點。

+0

我試圖顯示R和G distribuition爲曲線圖,利用matplotlib。 RGB值來自圖像的numpy數組。 – Lucas

相關問題