You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用HSV而非RGB调用setPixel?需过滤黄色调转为白色

问题解答

是的,必须把HSV(HSB)转换回RGB才能使用SetPixel方法。

原因很直接:.NET的Bitmap.SetPixel方法只接受基于RGB格式的Color对象。虽然你可以从现有Color中提取HSB参数,但.NET并没有提供直接通过HSB值创建Color的内置方法——所有创建Color的方式(比如Color.FromArgb、Color.FromRgb)都是基于RGB通道的。

结合你的需求(把黄色调转为白色),可以这样修改代码:

Bitmap bmap = new Bitmap(B2);

int width = bmap.Width;
int height = bmap.Height;

// 定义黄色的色相范围(可根据实际需求调整阈值)
float yellowHueMin = 40f;
float yellowHueMax = 60f;

for (int y = 0; y < height; y++)
{
    for (int x = 0; x < width; x++)
    {
        Color c = bmap.GetPixel(x, y);
        float h = c.GetHue();
        float s = c.GetSaturation();
        float brightness = c.GetBrightness();

        Color newColor;
        // 判断是否为有效黄色调:色相在范围内,且饱和度、亮度足够(过滤偏灰/偏白的近似色)
        if (h >= yellowHueMin && h <= yellowHueMax && s > 0.3f && brightness > 0.2f)
        {
            newColor = Color.White;
        }
        else
        {
            // 非黄色调直接保留原颜色;若需调整HSB参数,需自行实现HSB转RGB逻辑后再创建Color
            newColor = c;
        }

        bmap.SetPixel(x, y, newColor);
    }
}

如果之后需要调整其他HSB参数再应用到图像,就得自己实现HSB转RGB的工具方法,将HSB值转换成RGB三通道数值后,用Color.FromArgb创建新的Color对象,才能传给SetPixel。

内容的提问来源于stack exchange,提问作者Sauce

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 04:31:05