我希望能够从一张图片中提取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()
但是它不起作用!
因此,我尝试了三个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()
它只适用于循环中的打印,并且每个像素需要保存三次,对于matplotlib无效。
有人可以帮我吗?