如何重新着色图像?(见图)

Ver*_*cas 14 c# image colors image-processing

如何以编程方式实现这种颜色替换? 用蓝色代替黑色


所以这是我用来替换像素的函数:

Color.FromArgb(
    oldColorInThisPixel.R + (byte)((1 - oldColorInThisPixel.R / 255.0) * colorToReplaceWith.R),
    oldColorInThisPixel.G + (byte)((1 - oldColorInThisPixel.G / 255.0) * colorToReplaceWith.G),
    oldColorInThisPixel.B + (byte)((1 - oldColorInThisPixel.B / 255.0) * colorToReplaceWith.B)
    )
Run Code Online (Sandbox Code Playgroud)

谢谢,CodeInChaos!

Cod*_*aos 10

计算新像素的公式为:

newColor.R=OldColor;
newColor.G=OldColor;
newColor.B=255;
Run Code Online (Sandbox Code Playgroud)

推广到任意颜色:

我假设您要将白色映射为白色,将黑色映射到该颜色.所以公式是newColor=TargetColor+(White-TargetColor)*Input

newColor.R=OldColor+(1-oldColor/255.0)*TargetColor.R;
newColor.G=OldColor+(1-oldColor/255.0)*TargetColor.G;
newColor.B=OldColor+(1-oldColor/255.0)*TargetColor.B;
Run Code Online (Sandbox Code Playgroud)

然后迭代图像的像素(字节数组)并将它们写入新的RGB数组.有很多关于如何将图像复制到字节数组并对其进行操作的线程.


too*_*too 9

最简单的方法是使用ColorMatrix处理图像,您甚至可以处理所需效果的飞行预览 - 这是在图形编辑应用程序中制作了多少个滤色器.在这里这里,您可以在C#中使用Colormatrix找到颜色效果的介绍.通过使用ColorMatrix,您可以根据需要制作彩色滤镜,以及棕褐色,黑/白,反转,范围,亮度,对比度,亮度,水平(通过多次通过)等.

编辑:这是一个例子(更新 - 固定颜色矩阵将较暗的值转换为蓝色而不是之前的归零而不是蓝色部分 - 并且 - 添加0.5f为蓝色,因为在黑色上面的图片变为50%蓝色):

var cm = new ColorMatrix(new float[][]
{
  new float[] {1, 0, 0, 0, 0},
  new float[] {0, 1, 1, 0, 0},
  new float[] {0, 0, 1, 0, 0},
  new float[] {0, 0, 0, 1, 0},
  new float[] {0, 0, 0.5f, 0, 1}
});

var img = Image.FromFile("C:\\img.png");
var ia = new ImageAttributes();
ia.SetColorMatrix(cm);

var bmp = new Bitmap(img.Width, img.Height);
var gfx = Graphics.FromImage(bmp);
var rect = new Rectangle(0, 0, img.Width, img.Height);

gfx.DrawImage(img, rect, 0, 0, img.Width, img.Height, GraphicsUnit.Pixel, ia);

bmp.Save("C:\\processed.png", ImageFormat.Png);
Run Code Online (Sandbox Code Playgroud)