How to extract RGB from an image and plot only RG as a graph? R for X and G for Y

こ雲淡風輕ζ 提交于 2019-12-22 11:29:28

问题


I am trying to extract the RGB components from an image and plot the 3D RGB histogrma using matplotlib. But I dont know how can I do it.

Here's my current 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()

But it doesn't work!

So, I tried with three for:

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

It works only for the print into the for and also save three times for each pixel, and for matplotlib it doesn't work.

Anyone can help me?


回答1:


The following snippet displays a 3D scatter plot of the RGB colors of your 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)
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()

This is what you get when you run the code above (the result obviously depends on the particular image used):

If your goal is to visualize in 3D the 2D histogram of the intensities of the red and green channels, then you may find this code useful:

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

And here is the corresponding output:




回答2:


I don't see where the values R, g, and b are set in your code example. Also it looks like plot.plot(r,'ro',b,'b^') would not be plotting series but a single point.



来源:https://stackoverflow.com/questions/36700674/how-to-extract-rgb-from-an-image-and-plot-only-rg-as-a-graph-r-for-x-and-g-for

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!