发帖我指的是:How to extract RGB from an image and plot only RG as a graph? R for X and G for Y
我将它修改为2d并读取Y Cr Cb值,然而在3d中绘制它并不是什么大问题只是绘制Cb Cr值并根据我发布的边界进行绘制。
import numpy as np
import cv2
import matplotlib.image as mpimg
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
#Variables
Y_MIN = 80
Y_MAX = 255
Cb_MIN = 85
Cb_MAX = 135
Cr_MIN = 135
Cr_MAX = 180
img = mpimg.imread('Webcam.png')
YCR_CB = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
pixels = YCR_CB.shape[0]*YCR_CB.shape[1]
channels = 3
data = np.reshape(YCR_CB[:, :, :channels], (pixels, channels))
histo_rgb, _ = np.histogramdd(data, bins=256)
Y, Cb, Cr = np.nonzero(histo_rgb)
plt.scatter(Cb, Cr)
plt.title('Chrominance')
plt.show()
我想采取这个并使其绘制落入其中的(Cb,Cr)值 ((Y> Y_MIN)&(Cb> Cb_MIN)&(Cb< Cb_MAX)&(Cr> Cr_MIN)&(Cr< Cr_MAX)) 作为一种颜色,并绘制其他不属于这些边界的点作为另一种颜色。提前感谢您的帮助!
答案 0 :(得分:0)
请用以下代码替换代码的最后3行:
crit0 = Y > Y_MIN
crit1 = np.logical_and(Cb > Cb_MIN, Cb < Cb_MAX)
crit2 = np.logical_and(Cr > Cr_MIN, Cr < Cr_MAX)
tot_crit = np.logical_and(np.logical_and(crit0, crit1), crit2)
i = np.where(tot_crit)
j = np.where(np.invert(tot_crit))
plt.scatter(Cb[i], Cr[i])
plt.scatter(Cb[j], Cr[j], c='r')
plt.title('Chrominance')
plt.show()