BGR to Gray conversion function own implementation in OpenCV:
Mat convertBGRtoGray(Mat image){
vector<Mat> channels(3);
split(image, channels);
Mat GrayScale = channels[2] * 0.2999 + channels[1] * 0.587 + channels[0] * 0.114;
return GrayScale;
}
BGR to HSV conversion function own implementation in OpenCV:
Mat convertBGRtoHSV(Mat image){
Mat hsvOut = image.clone();
image.convertTo(image, CV_32FC3, 1/255.0);
for (int i = 0; i < image.cols; i ++)
{
for (int j = 0; j < image.rows; j++)
{
Vec3f bgr_ch = image.at<Vec3f>(j, i);
float b = bgr_ch.val[0];
float g = bgr_ch.val[1];
float r = bgr_ch.val[2];
// h, s, v = hue, saturation, value
double v = max(r, max(g, b)); // maximum of r, g, b
double cmin = min(r, min(g, b)); // minimum of r, g, b
double diff = v - cmin; // diff of cmax and cmin.
double h = -1, s = -1;
// if cmax equal zero
if (v == 0)
s = 0;
else
s = (diff / v);
if (v == cmin)
h = 0;
else if (v == r)
h = 60 * ((g - b) / diff);
else if (v == g)
h = 120 + (60 * ((b - r) / diff));
else if (v == b)
h = 240 + (60 * ((r - g) / diff));
if (h < 0)
{
h = h + 360;
}
Vec3b& hsv_ch = hsvOut.at<Vec3b>(j, i);
hsv_ch[0] = h/2;
hsv_ch[1] = s*255;
hsv_ch[2] = v*255;
}
}
return hsvOut;
}
No comments:
Post a Comment