确定最多& 2d直方图中最不常见的颜色

时间:2016-09-01 07:51:35

标签: c++ opencv

如何从二维直方图中确定最常见/主导的颜色?我从HSV图像中产生了一个二维直方图。 2个通道/轴是色调和饱和通道。

在2d直方图上使用minMaxLoc()提供了值,但是当我绘制这些值时,它们都是相同的红色。

enter image description here

/// Using 50 bins for hue and 60 for saturation
int h_bins = 50; int s_bins = 60;
int histSize[] = { h_bins, s_bins };
float h_ranges[] = { 0, 180 };
float s_ranges[] = { 0, 256 };
const float* ranges[] = { h_ranges, s_ranges };
int channels[] = { 0, 1 };

calcHist(&hsv, 1, channels, Mat(), hist, 2, histSize, ranges, true, false);
// not normalising yet as that will affect the min max colour values right?

double min, max;
Point minPnt, maxPnt;
minMaxLoc(hist, &min, &max, &minPnt, &maxPnt);
printf("[%f,%f], [%d,%d], [%d,%d]\n", min, max, minPnt.x, minPnt.y, maxPnt.x, maxPnt.y);
// outputs: [0.000000, 3647.000000], [18, 0], [26, 28]

// Display Min and Max HSV 'colour' to visualise it. Both are red
// Do I need to compensate for the reduction in bins (h=50, s=60)?
Vec3b minHsv(18, 0, 255);
Vec3b maxHsv(26, 28, 255);
Mat minHsvImg(100, 100, CV_8UC3, minHsv);
Mat maxHsvImg(100, 100, CV_8UC3, maxHsv);
cvtColor(minHsvImg, minHsvImg, CV_BGR2HSV);
cvtColor(maxHsvImg, maxHsvImg, CV_BGR2HSV);
rectangle(minHsvImg, { 0, 0, 100, 100 }, minHsv, -1);
rectangle(maxHsvImg, { 0, 0, 100, 100 }, maxHsv, -1);

0 个答案:

没有答案