简体   繁体   English

在Matlab中比较图像中RGB值的直方图

[英]Comparing Histograms of RGB values from image in Matlab

So I want the RGB values of an image placed into an histogram and then that histogram will be compared to other image's histogram. 因此,我希望将图像的RGB值放入直方图中,然后将该直方图与其他图像的直方图进行比较。 Currently this is the code: 当前这是代码:

if (size(cimg, 3) ~= 3) 
error('rgbhist:numberOfSamples', 'Input image must be RGB.') 
end
nBins = 256;
rHist = imhist(cimg(:,:,1), nBins); 
gHist = imhist(cimg(:,:,2), nBins); 
bHist = imhist(cimg(:,:,3), nBins);
hFig = figure;
%figure 

subplot(1,2,1);imshow(cimg) 
subplot(1,2,2);
hold on
h(1) = stem(1:256, rHist); %hold on 
h(2) = stem(1:256 + 1/3, gHist, 'g'); 
h(3) = stem(1:256 + 2/3, bHist, 'b'); 
hold off
set(h, 'marker', 'none') 
set(h(1), 'color', [1 0 0]) 
set(h(2), 'color', [0 1 0]) 
set(h(3), 'color', [0 0 1]) 
axis square

The code outputs the image along with its RGB histogram value, how can I use that histogram to compare it with other histograms so that I could potentially classify the image as having nearly the same colors as that of another image? 该代码将图像及其RGB直方图值一起输出,如何使用该直方图将其与其他直方图进行比较,以便我有可能将图像分类为与另一幅图像具有几乎相同的颜色?

You could use Kullback Leibler Divergence to calculate the distance between 2 histograms. 您可以使用Kullback Leibler Divergence来计算2个直方图之间的距离。
This is easy as you can treat the Histogram as a distribution. 这很容易,因为您可以将直方图视为分布。

Since the KL Divergence isn't symmetric one could compute it twice (Namely [X, Y] and [Y, X]) and take the average. 由于KL散度不是对称的,因此可以计算两次(即[X,Y]和[Y,X])并取平均值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM