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

博客展示了一段原创代码,实现了将RGB值转换为饱和度(S)和颜色值(C)的功能。代码通过遍历图像像素,计算每个像素的最大值和最小值,进而得出饱和度和颜色值,并将结果存储在相应的矩阵中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
        }
    }
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值