我正在从下面的图片中创建一个hsv直方图。
- (void)processImageWithHsv:(Mat&)image;
{
Mat image_hsv;
cvtColor(image, image_hsv, CV_BGR2HSV);
int hbins = 50, sbins = 60;
int histSize[] = {hbins, sbins};
float hranges[] = { 0, 360 };
float sranges[] = { 0, 256 };
const float* ranges[] = { hranges, sranges };
MatND hist;
int channels[] = {0, 1};
calcHist( &image_hsv, 1, channels, Mat(), // do not use mask
hist, 2, histSize, ranges,
true, // the histogram is uniform
false );
double maxVal = 0;
minMaxLoc(hist, 0, &maxVal, 0, 0);
// ???: HOW Convert this information to colour value
}
但我不知道从hist
获得最主要的颜色值?
我应该使用maxVal
吗?
答案 0 :(得分:1)
你犯了一些错误:
value
但您告诉calcHist
使用色调和饱和度。你应该改变频道。hranges
错了:它应该是180。dims
应为1(不是2),因为您只需要value
直方图。在这些更正后maxVal
应包含最常见的value
值。
答案 1 :(得分:0)
- (void)processImageWithHsv:(Mat&)image;
{
Mat image_hsv;
cvtColor(image, image_hsv, CV_BGR2HSV);
// Quanta Ratio
int scale = 10;
int hbins = 36, sbins = 25, vbins = 25;
int histSize[] = {hbins, sbins, vbins};
float hranges[] = { 0, 180 };
float sranges[] = { 0, 256 };
float vranges[] = { 0, 256 };
const float* ranges[] = { hranges, sranges, vranges };
MatND hist;
int channels[] = {0, 1, 2};
calcHist( &image_hsv, 1, channels, Mat(), // do not use mask
hist, 3, histSize, ranges,
true, // the histogram is uniform
false );
int maxVal = 0;
int hue = 0;
int saturation = 0;
int value = 0;
for( int h = 0; h < hbins; h++ )
for( int s = 0; s < sbins; s++ )
for( int v = 0; v < vbins; v++ )
{
int binVal = hist.at<int>(h, s, v);
if(binVal > maxVal)
{
maxVal = binVal;
hue = h;
saturation = s;
value = v;
}
}
hue = hue * scale; // angle 0 - 360
saturation = saturation * scale; // 0 - 255
value = value * scale; // 0 - 255
}