我正在尝试使用KNN方法根据人们的面部照片将他们分类为种族。我在纯白色背景255,255,255上有一个人脸数据集。
作为提要输入,我使用颜色直方图的值。我被告知我应该从直方图中删除背景颜色,以提高KNN的性能。
问题:当我从我的照片中创建一个忽略背景的面具时,直方图一点也不改变。
问:我不是很喜欢色彩理论,纯白颜色对颜色直方图的形状有影响吗?当我使用只有中心(如下图)的常规掩码时,直方图会发生变化。
这是我从图片中构造的面具,忽略了背景。
简单掩码测试掩码应用程序的正确性
直方图计数的源图像
这是我从没有任何遮罩的图片中得到的直方图,也是从我构造的无视白色的面具中得到的。
这是我用简单的面具剪切图片得到的直方图。直方图的变化,因此我认为我的计算直方图的方法是正确的。
计数直方图代码:
# loop over the image channels
for (chan, color) in zip(channels, colors):
# create a histogram for the current channel and
# concatenate the resulting histograms for each channel
hist_full = opencv.calcHist([chan], [0], mask, [bin_amount], [0, bin_amount])
# plot the histogram
plt.plot(hist_full, color=color)
plt.xlim([0, bin_amount])
plt.show()
创建掩码的代码:
mask = np.zeros(image.shape[:2], np.uint8)
# simple mask option
# mask[75:175, 75:175] = 255
# create a mask to ignore white background in the histogram
for row in range(0, len(image)):
for col in range(0, len(image[0])):
if (image[row][col] != np.asarray(prop.background)).all():
try:
mask[row][col] = 255
except IndexError:
print(col)
发布于 2017-03-27 21:38:15
请参阅:http://docs.opencv.org/2.4/modules/imgproc/doc/histograms.html
重要部分:
Python: cv2.calalHist(图像、通道、掩码、histSize、范围[、hist、累积])→hist 参数:.。范围-每个维度中直方图本边界的dims数组的数组。当直方图是均匀的(均匀=真),那么对于每个维度I,就足够指定第0直方图bin的下(包含)边界L_0和上(排他性)边界U_{\texttt{histSize}i-1}。
尝试更改代码的这一部分。
hist_full = opencv.calcHist([chan], [0], mask, [bin_amount], [0, bin_amount])
如下所示
hist_full = opencv.calcHist([chan], [0], mask, [bin_amount], [0, 256])
您需要在图片中指定实际值的范围(上边界独占)。很可能,现在您只计算值0-63,忽略直方图中的64-255。
https://stackoverflow.com/questions/43056066
复制相似问题