我正在尝试做与这篇文章类似的事情:从 HSV 直方图中获取主要颜色值
我有一张图像,想从中提取主色(色调)。我已经到了计算直方图的地步,并从 minMaxLoc 获得了 maxValue。但是我从 Core.MinMaxLocResult 检索的数字完全没有意义。我得到了 806924 和 1067036 之类的东西;Hue 的期望值不应该在 0 到 180 之间吗?
直方图应该归一化吗?怎么会这样?我见过像“equalizeHist”和“normalize”这样的方法,但我真的不知道如何使用它们以及它们将如何帮助我。
另外,一旦我得到了一个合理的“最常见的”色调数字,我如何将其转换为实际的颜色阴影(比如“绿色是这张图片中最常见的颜色”)?有标准的色调范围吗?比如 0-10 是红色,10-20 是紫色,等等?
更新:这是我的代码:
private void processImage(String imgFilename) {
channels = new MatOfInt[] { new MatOfInt(0), new MatOfInt(1),
new MatOfInt(2) };
histSize = new MatOfInt(histSizeNum);
hRanges = new MatOfFloat(0f, 180f); // hue varies from 0 to 179
// use openCV stuff to convert from RGB to HLS space
Mat src = Highgui.imread(imgFilename);
Mat hls = new Mat();// destination color space
Imgproc.cvtColor(src, hls, Imgproc.COLOR_RGB2HLS);
Core.split(hls, hlsChannels);
Mat hue = hlsChannels.get(0);
Mat lum = hlsChannels.get(1);
Mat sat = hlsChannels.get(2);
// we compute the histogram from the 0-th channel for hue
Imgproc.calcHist(Arrays.asList(hls), channels[0], new Mat(), hist,
histSize, hRanges);
Core.normalize(hist, hist, 0,2, Core.NORM_MINMAX, -1, new Mat());
Core.MinMaxLocResult result = Core.minMaxLoc(hist);
// max value should contain the most-recurring Hue value.
double mostOccurringHue = result.maxVal;
double leastOccurringHue = result.minVal;
//double mostOccurringHue = result.maxLoc.x;
//double leastOccurringHue = result.minLoc.x;
// print out for sanity checking
System.out.println("MAX HUE = " + Double.toString(mostOccurringHue) +"\n");
System.out.println("MIN HUE = " + Double.toString(leastOccurringHue) +"\n");