Insane max Hue ranges?
I don't why.. but for some reason is my histogram showing that the max hue value of my image is around 339498 which doesn't make sense for me..
I am trying to create a mask, that recognizes the dominant color of the picture, and based on that creates a mask. I've converted my image to HSV , and created a histogram based on the Hue channel, for which i look at the highest value.
then I manually check each Hue-value in the picture, to create a binary image of the things that contains the dominant color.
But for some reason is my max Hue value too high no idea why.. here is the code.
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(int argc, const char * argv[]) {
std::cout << "Hello, World!\n";
Mat src;
src = imread("/Users/x/Desktop/IMG_3607.jpg");
Mat src_HSV;
cvtColor(src, src_HSV, CV_RGB2HSV);
//vector<Mat> HSV_planes;
//split(src_HSV, HSV_planes);
int hbins = 30;
int histSize[] = {hbins};
float hranges[] = { 0, 180 };
const float* ranges[] = { hranges };
Mat hist;
int channels[] = {0};
calcHist(&src_HSV, 1, channels, Mat(), hist, 1, histSize, ranges,true,false);
double maxVal=0;
minMaxLoc(hist, 0, &maxVal, 0, 0);
cout << "highest Hue value: " << maxVal << endl;
Mat histImg = Mat::zeros(hbins*10,hbins*10, CV_8UC3);
Mat result(src_HSV.rows,src_HSV.cols,CV_8UC1);
for (int i = 0; i<src_HSV.rows; i++) {
for (int j = 0; j<src_HSV.cols; j++) {
Vec3b HSV = src_HSV.at<Vec3b>(i,j);
if (HSV[0]==maxVal)
{
cout << "HSV Lower: " << HSV[0] << endl;
result.at<uchar>(i,j) = 0;
}
else
{
cout << "HSV Higher': " << HSV[0] << endl;
result.at<uchar>(i,j) = 255;
}
}
}
/// Draw for each channel
for( int h = 0; h < hbins; h++ )
for( int s = 0; s < hbins; s++ )
{
float binVal = hist.at<float>(h, s);
int intensity = cvRound(binVal*255/maxVal);
rectangle( histImg, Point(h*10, s*10),
Point( (h+1)*10 - 1, (s+1)*10 - 1),
Scalar::all(intensity),
CV_FILLED );
}
while (waitKey(1)) {
imshow("Hue-channel", src_HSV);
//imshow("S-channel", HSV_planes[1]);
//imshow("V-channel", HSV_planes[2]);
imshow("histimage",histImg);
imshow("result", result);
}
return 0;
}
test image
@215 I do not think that there is a problem in your solution. Considering that you have an image of 816 x 612 dimensions (i.e. your test image above) it gives us 499392 pixels values. Now taking out the pixels values of the non-dominant colours, a value of 339498 pixels of the same/dominant colour = the highest peak in your histogram seems quite possible to me.
So the value i receive isn't the highest Hue value, but "only" the amount of pixels which have the highest hue value.. How do i convert that number into a Hue value..
just get the .col value of your hist image at that spot = peak. Since your hist image ranges between 0-180 you should get one of these values.
I am not quite sure how you are going to do that.. At the moment am i just saving a point which contains the highest value, and thresholding it based on that.