lundi 2 mars 2015

Opencv - Get common color area

I want to find area of common color on an image. For this, I read image and convert to hsv. Then I calculate histogram using h channel. I find maximum value in this image histogram object. I quantize the hue to 30 level. I use binary threshold but the result is not good for this task.


My code



cvtColor(src, hsv, CV_BGR2HSV);

// Quantize the hue to 30 levels
// and the saturation to 32 levels
int hbins = 30, sbins = 32;
int histSize[] = { hbins };
float hranges[] = { 0, 180 };
float sranges[] = { 0, 256 };
const float* ranges[] = { hranges, sranges };
MatND hist;
int channels[] = { 0};

calcHist(&hsv, 1, channels, Mat(), // do not use mask
hist,1, histSize, ranges,
true, // the histogram is uniform
false);

double maxVal = 0;
minMaxLoc(hist, 0, &maxVal, 0, 0);
cout << "\n maxVal " << maxVal << endl;

int scale = 10;
Mat histImg = Mat::zeros(1, hbins * 10, CV_8UC3);

int maxIntensity = -100;
int i = -1, j = -1;
for (int h = 0; h < hbins; h++) {
float binVal = hist.at<float>(h, 0);
int intensity = cvRound(binVal * 255 / maxVal);
if (maxIntensity < intensity) {
maxIntensity = intensity;
i = h;
}
}
namedWindow("Source", 1);
imshow("Source", src);

cout << "\n maxIntensityHueIndex " << i << endl;

Mat dst, dst2;
threshold(hsv, dst, i*(180 / hbins), 180, CV_THRESH_BINARY);

namedWindow("Dest", 1);
imshow("Dest", dst);


My input image is here


enter image description here


My result is enter image description here


I want to extract one of below image.


enter image description here enter image description here


Aucun commentaire:

Enregistrer un commentaire