2016-04-18 6 views
3

Ich versuche, die RGB-Komponenten aus einem Bild zu extrahieren und die 3D-RGB-Histogrma mit Matplotlib zu plotten. Aber ich weiß nicht, wie ich es tun kann.Wie kann man RGB aus einem Bild extrahieren und nur RG als Graph darstellen? R für X und G für Y

Hier ist mein aktueller Code:

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

Aber es funktioniert nicht!

Also habe ich versucht, mit drei für:

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

Es funktioniert nur für den Druck in die für und auch dreimal für jedes Pixel speichern, und für matplotlib funktioniert es nicht.

Jeder kann mir helfen?

Antwort

1

Der folgende Ausschnitt zeigt ein 3D-Streudiagramm der RGB-Farben des Bildes:

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

Dies ist, was Sie erhalten, wenn Sie den Code oben laufen (das Ergebnis offensichtlich auf dem bestimmte Bild hängt verwendet): 3D scatter plot of the RGB colors of an image

Wenn Ihr Ziel in 3D das 2D-Histogramm der Intensitäten der roten und grünen Kanäle sichtbar zu machen ist, dann können Sie diesen Code nützlich finden:

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

Und hier ist der entsprechende Ausgang: 3D representation of the joint RG histogram of an image

+0

Ich habe versucht, es gab mir eine Ausgabe wie ein Histogramm zurück. Aber ich möchte wie dieses Bild drucken: http://machinethatsees.blogspot.com.br/2012/07/how-to-plot-color-in-3d-rgb-color-space.html aber in 2D. Daher gilt R für die X-Achse und G für die Y-Achse. Hast du mich verstanden? Wenn Sie mir helfen können, lautet meine E-Mail-Adresse: [email protected] Wir können dort leichter kommunizieren als hier. Danke für deine Hilfe! – Lucas

0

Ich sehe nicht, wo die Werte R, g und b in Ihrem Codebeispiel festgelegt sind. Außerdem sieht es so aus, als würde plot.plot (r, 'ro', b, 'b ^') keine Serie, sondern einen einzigen Punkt darstellen.

+0

Ich versuche, die R und G distribuition als Graph zu zeigen, mit matplotlib. Die RGB-Werte stammen aus dem numpy-Array des Bildes. – Lucas