图像RGB空间转HSV空间,C++

void Rgb_to_sv_New(Mat &samples, Mat &perPixelS, Mat &perPixelC)
{
    int a = 0, b = 0, c = 0;
    int cmax = 0, cmin = 0;
    float saturation = 0.0,cvalue=0.0;
    assert(perPixelC.total() == perPixelS.total());
    assert(perPixelC.channels() == perPixelS.channels());
    for (int i = 0;i < samples.rows;i++)
    {
        uchar* srcP = samples.ptr<uchar>(i);
        float* dstS = perPixelS.ptr<float>(i);
        float* dstC = perPixelC.ptr<float>(i);
        for (int j = 0,k=0;j < samples.cols*CHANNELS,k< perPixelS.cols*perPixelS.channels();k++,j=j+3)
        {
            cmax = max(srcP[j], max(srcP[j + 1], srcP[j + 2]));
            cmin = min(srcP[j], min(srcP[j + 1], srcP[j + 2]));
            saturation = cmax == 0 ? 0.0 : (float)(cmax - cmin) / (float)cmax;
            cvalue = (float)cmax / 255.0;
            dstS[k] = saturation;
            dstC[k] = cvalue;
        }
    }
}

代码为本人原创,转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C语言程序,用于将RGB颜色空间图像换为HSV和HSL颜色空间: ```c #include <stdio.h> void rgb_to_hsv(float r, float g, float b, float* h, float* s, float* v) { float min, max, delta; min = r < g ? r : g; min = min < b ? min : b; max = r > g ? r : g; max = max > b ? max : b; *v = max; // v delta = max - min; if (max != 0) *s = delta / max; // s else { // r = g = b = 0 // s = 0, v is undefined *s = 0; *h = -1; return; } if (r == max) *h = (g - b) / delta; // between yellow & magenta else if (g == max) *h = 2 + (b - r) / delta; // between cyan & yellow else *h = 4 + (r - g) / delta; // between magenta & cyan *h *= 60; // degrees if (*h < 0) *h += 360; } void rgb_to_hsl(float r, float g, float b, float* h, float* s, float* l) { float min, max, delta; min = r < g ? r : g; min = min < b ? min : b; max = r > g ? r : g; max = max > b ? max : b; *l = (max + min) / 2; if (max == min) { *s = 0; *h = 0; // achromatic } else { delta = max - min; *s = *l > 0.5 ? delta / (2 - max - min) : delta / (max + min); if (r == max) *h = (g - b) / delta; // between yellow & magenta else if (g == max) *h = 2 + (b - r) / delta; // between cyan & yellow else *h = 4 + (r - g) / delta; // between magenta & cyan *h *= 60; // degrees if (*h < 0) *h += 360; } } int main() { float r, g, b; float h, s, v, l; printf("Enter RGB values (0-255): "); scanf("%f %f %f", &r, &g, &b); rgb_to_hsv(r / 255, g / 255, b / 255, &h, &s, &v); printf("HSV values: %f %f %f\n", h, s, v); rgb_to_hsl(r / 255, g / 255, b / 255, &h, &s, &l); printf("HSL values: %f %f %f\n", h, s, l); return 0; } ``` 该程序将RGB颜色值作为输入,并计算相应的HSV和HSL值。可以使用该程序将RGB颜色空间图像换为HSV和HSL颜色空间图像

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值