使用带抗锯齿功能的C#更改防锯齿图像的背景颜色 [英] Change background colour of an anti-aliased image using C# with anti-aliasing

查看:574
本文介绍了使用带抗锯齿功能的C#更改防锯齿图像的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张图片,我需要更改背景颜色(例如,将示例图片的背景更改为蓝色)。



但是,图片是反-aliased,所以我不能简单地用不同的颜色替换背景颜色。



我尝试过的一种方法是创建第二个图像,它仅仅是背景并且改变它的颜色并将两个图像合并成一个,但这不起作用,因为两个图像之间的边界是模糊的。



有没有办法做到这一点,或者其他方式来实现这一点,我没有考虑过?



示例图片

解决方案摘要

图像中的每个像素都是一个(R,G,B)向量,其中每个分量的范围是[0 ,1]。你需要一个变换T,它将在下列约束条件下将图像中的所有像素转换为新的(R',G',B'):




  • black应该保持黑色


    • T(0,0,0)=(0,0,0)





    • $ b
    • T(1,1, 1)= C *




一个简单的方法是选择在变换T之后:

$ $ $ $ $ $ $ $ $ $ $ $ c $ T $(c)= C *。* c(其中。*表示单元乘法)

这只是标准的图像倍增。





如果您不担心性能,可以使用(非常慢)方法 GetPixel 和<$在您的位图上使用c $ c> SetPixel 对其中的每个像素应用此变换。如果不清楚如何做到这一点,只需在评论中这样说,我会为该部分添加详细的解释。



比较



将此与LarsTech提供的方法进行比较。这里介绍的方法在顶部; LarsTech提出的方法在底部。注意底部图标上的不良边缘效应(边缘上出现白色阴影)。



这里是两者的图像差异:

< img src =https://i.stack.imgur.com/PAZfm.pngalt =差异>



反思



如果您的源图像具有透明(即透明白色)背景和黑色前景(如您的示例中所示),那么您可以简单地进行变换T(a,r,g,b)= a,0,0,0),然后根据您想要的任何背景颜色绘制您的图像,如LarsTech建议的那样。

I have an image where I need to change the background colour (E.g. changing the background of the example image below to blue).

However, the image is anti-aliased so I cannot simply do a replace of the background colour with a different colour.

One way I have tried is creating a second image that is just the background and changing the colour of that and merging the two images into one, however this does not work as the border between the two images is fuzzy.

Is there any way to do this, or some other way to achieve this that I have no considered?

Example image

解决方案

Abstractly

Each pixel in your image is a (R, G, B) vector, where each component is in the range [0, 1]. You want a transform, T, that will convert all of the pixels in your image to a new (R', G', B') under the following constraints:

  • black should stay black
    • T(0, 0, 0) = (0, 0, 0)
  • white should become your chosen color C*
    • T(1, 1, 1) = C*

A straightforward way to do this is to choose the following transform T:

T(c) = C* .* c  (where .* denotes element-wise multiplication)

This is just standard image multiplication.

Concretely

If you're not worried about performance, you can use the (very slow) methods GetPixel and SetPixel on your Bitmap to apply this transform for each pixel in it. If it's not clear how to do this, just say so in a comment and I'll add a detailed explanation for that part.

Comparison

Compare this to the method presented by LarsTech. The method presented here is on the top; the method presented by LarsTech is on the bottom. Notice the undesirable edge effects on the bottom icon (white haze on the edges).

And here is the image difference of the two:

Afterthought

If your source image has a transparent (i.e. transparent-white) background and black foreground (as in your example), then you can simply make your transform T(a, r, g, b) = (a, 0, 0, 0) then draw your image on top of whatever background color you want, as LarsTech suggested.

这篇关于使用带抗锯齿功能的C#更改防锯齿图像的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆