この投稿に似たようなことをしようとしています: HSV ヒストグラムから支配的な色の値を取得する
画像があり、そこからドミナント カラー (色相) を抽出したいと考えています。ヒストグラムを計算し、minMaxLoc から maxValue を取得するところまで来ました。しかし、Core.MinMaxLocResult から取得している数値はまったく意味がありません。806924 や 1067036 のようなものが表示されます。色相の期待値は 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");