首页 > 学院 > 开发设计 > 正文

opencv,图像处理,rgb转换为hsv空间

2019-11-06 06:23:19
字体:
来源:转载
供稿:网友

图像RGB空间转换为HSV空间。

 H参数表示色彩信息,即所处的光谱颜色的位置。该参数用一角度量来表示,红、绿、蓝分别相隔120度。互补色分别相差180度。 纯度S为一比例值,范围从0到1,它表示成所选颜色的纯度和该颜色最大的纯度之间的比率。S=0时,只有灰度。 V表示色彩的明亮程度,范围从0到1。有一点要注意:它和光强度之间并没有直接的联系。

 1: max=max(R,G,B)  2: min=min(R,G,B)  3: if R = max, H = (G-B)/(max-min)  4: if G = max, H = 2 + (B-R)/(max-min)  5: if B = max, H = 4 + (R-G)/(max-min)  6:  7: H = H * 60  8: if H < 0, H = H + 360  9: 10: V=max(R,G,B) 11: S=(max-min)/max

代码为:

//转换为HSVvoid Rgb2Hsv(Mat &image, Mat &hsv){	float maxV = 0,minV = 0;	float V = 0, S = 0, H = 0;	float v = 0, s = 0, h = 0;	float R = 0, G = 0, B = 0;	float delta = 0, tmp = 0;	for (int i = 0; i < image.rows; i++)	{		for (int j = 0; j < image.cols; j++)		{			R = image.at<Vec3b>(i, j)[0];			G = image.at<Vec3b>(i, j)[1];			B = image.at<Vec3b>(i, j)[2];			R = R / 255.0;			B = B / 255.0;			G = G / 255.0;			tmp = min(R, G);			minV = min(tmp, B);			tmp = max(R, G);			maxV = max(tmp, B);			V = maxV; // v			delta = maxV - minV;			if (maxV != 0)				S = delta / maxV; // s			else			{				S = 0;			}			if (R == maxV)				H = (G - B) / delta;			else if (G == maxV)				H = 2 + (B - R) / delta;			else				H = 4 + (R - G) / delta;			H *= 60;			if (H < 0)				H += 360;			h = H / 2;			s = S * 255.0;			v = V * 255.0;			hsv.at<Vec3b>(i, j).val[0] = h;			hsv.at<Vec3b>(i, j).val[1] = s;			hsv.at<Vec3b>(i, j).val[2] = v;		}	}}


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表