Skip to content

Instantly share code, notes, and snippets.

@GonzalezAnguita
Created May 24, 2017 23:15
Show Gist options
  • Save GonzalezAnguita/4c25ebef311ac05bfbdae6239f6e9ddc to your computer and use it in GitHub Desktop.
Save GonzalezAnguita/4c25ebef311ac05bfbdae6239f6e9ddc to your computer and use it in GitHub Desktop.
HSV Image Processing
+(int *) processImageWithHsv:(cv::Mat&)rgb_image {
cv::Mat hsv_image;
cvtColor(rgb_image, hsv_image, CV_RGB2HSV);
// Quanta Ratio
int scale = 10;
int images_amount = 1;
int image_channels[] = {0, 1, 2};
int hue_levels = 36, saturation_levels = 25, value_levels = 25;
int histSize[] = {hue_levels, saturation_levels, value_levels};
float hue_range[] = { 0, 180 };
float saturation_range[] = { 0, 256 };
float value_range[] = { 0, 256 };
const float* ranges[] = { hue_range, saturation_range, value_range };
bool clear_at_beggining = false;
bool uniform_histogram = true;
int histogram_dimensionality = 3;
cv::MatND histogram;
calcHist( &hsv_image,
images_amount,
image_channels,
cv::Mat(),
histogram,
histogram_dimensionality,
histSize,
ranges,
uniform_histogram,
clear_at_beggining );
int maxVal = 0;
int hue = 0;
int saturation = 0;
int value = 0;
for (int h = 0; h < hue_levels; h++) {
for (int s = 0; s < saturation_levels; s++) {
for (int v = 0; v < value_levels; v++) {
int binVal = histogram.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
static int int_hsv_list[3];
int_hsv_list[0] = hue;
int_hsv_list[1] = saturation;
int_hsv_list[2] = value;
cv::Mat3f hsv(cv::Vec3f(hue, saturation, value));
std::cout << "HSV: " << hsv << std::endl;
cv::Mat3f rgb_color;
cvtColor(hsv, rgb_color, CV_HSV2RGB);
std::cout << "RGB: " << rgb_color << std::endl;
return int_hsv_list;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment