我正在制作一個程式,我想取一張圖片,將其調色板減少到60種顏色的預設調色板,然后添加抖動效果。這似乎涉及到兩件事:
在閱讀了色差之后,我想我將使用CIE94或CIEDE2000演算法來從我的串列中找到最接近的顏色。我還決定使用相當常見的Floyd-Steinberg 抖動演算法來實作抖動效果。
在過去的兩天里,我撰寫了我自己的這些演算法的版本,從互聯網上的例子中提取了其他版本的演算法,首先在Java和現在的C#中都進行了嘗試,幾乎每一次的輸出影像都有相同的問題。它的某些部分看起來非常好,有正確的顏色,而且抖動正常,但其他部分(有時是整個影像)最終變得太亮了,完全是白色的,或者全部模糊不清。通常,較暗的影像或影像中較暗的部分會變得很好,但任何明亮的部分或顏色較淺的部分都會被調得很亮。下面是一個存在這些問題的輸入和輸出影像的例子:
輸入:
<輸入:
]3
輸出:

我確實有一個想法,可能是什么原因導致了這種情況。當一個像素通過 "最接近的顏色 "功能被發送時,我讓它輸出其RGB值,似乎其中一些像素的R值(以及潛在的其他值)被推得比它們應該的值高很多,甚至有時超過255,如截圖中所示。這種情況不會發生在影像中最早的像素上,只發生在那些已經有多個像素并且已經有一定亮度的像素上。這使我相信是抖動/錯誤演算法造成的,而不是顏色轉換或色差演算法。如果這就是問題所在,那么我將如何去解決這個問題呢?
這是我正在使用的相關代碼和函式。在這一點上,它混合了我寫的東西和我在圖書館或其他 StackOverflow 帖子中發現的東西。我相信主要的抖動演算法和 C3 類基本上是從這個 Github 頁面中直接復制的(當然,為了與 C# 一起使用而進行了更改)
class Program
{
public static C3[] palette = new C3[] {
new C3(196, 76, 86)。
new C3(186, 11, 39)。
new C3(113, 0, 32)。
new C3(120, 41, 56)。
new C3(203, 125, 84)。
new C3(205, 90, 40)。
new C3(175, 50, 33)。
new C3(121, 61, 54)。
//etc...調色板總共有60種顏色。
//每個物件包含一個r、g和b值。
};
static void Main(string[] args)?
{
//原始影像的路徑和抖動影像的輸出路徑。
string path = @"C:UsersBillehBawbDesktop"/span>;
string imgPath = path "amy.jpg"。
string createPath = path "amydithered.jpg";
//拉出原始影像,運行抖動功能,然后保存新影像。
Bitmap img = new Bitmap(imgPath)。
Bitmap dithered = floydSteinbergDithering(img);
dithered.Save(createPath, ImageFormat.Jpeg)。
}
//回圈查看影像中的每個像素,用像素的顏色填充一個2D陣列,然后再次回圈查看,將顏色改為調色板中的一種,并進行抖動演算法。
private static Bitmap floydSteinbergDithering(Bitmap img)
{
int w = img.Width;
int h = img.Height;
C3[,] d = new C3[h, w];
for (int y = 0; y < h; y )
{
for (int x = 0; x < w; x )
{
d[y, x] = new C3(img.GetPixel(x, y).ToArgb() );
}
}
for (int y = 0; y < img.Height; y )
{
for (int x = 0; x < img.Width; x )
{
C3 oldColor = d[y, x];
C3 newColor = findClosestPaletteColor(oldColor, palette);
img.SetPixel(x, y, newColor.toColor())。
C3 err = oldColor.sub(newColor);
if (x 1 < w)
{
d[y, x 1] = d[y, x 1].add(err.mul(7.0 / 16) 。)
}
if (x - 1 >= 0 & & y 1 < h)
{
d[y 1, x - 1] = d[y 1, x - 1] 。 add(err.mul(3.0 / 16) 。)
}
if (y 1 < h)
{
d[y 1, x] = d[y 1, x].add(err.mul(5.0 / 16) 。)
}
if (x 1 < w && y 1 < h)
{
d[y 1, x 1] = d[y 1, x 1] 。 add(err.mul(1.0 / 16) 。)
}
}
}
return img;
}
//回圈查看調色板,將輸入的像素和調色板顏色轉換為LAB格式,找出所有顏色的差異,并選擇差異最小的調色板顏色。
private static C3 findClosestPaletteColor(C3 c, C3[] palette)
{
double[] pixelLab = rgbToLab(c.toColor().R, c.toColor().G, c.toColor().B)。
double minDist = Double.MaxValue;
int colorIndex = 0;
for (int i = 0; i < palette.Length; i )
{
double[] colors = rgbToLab(palette[i].toColor().R, palette[i].toColor().G, palette[i].toColor() .B)。
double dist = labDist(pixelLab[0], pixelLab[1] 。pixelLab[2], colors[0], colors[1], colors[2])。)
if (dist < minDist)
{
colorIndex = i;
minDist = dist;
}
}
return palette[colorIndex]。
}
//用CIE94演算法找到兩組LAB顏色之間的deltaE/difference。
public static double labDist(double l1, double a1, double b1, double l2。double a2, double b2)
{
var deltaL = l1 - l2。
var deltaA = a1 - a2;
var deltaB = b1 - b2;
var c1 = Math.Sqrt(Math.Pow(a1, 2) Math.Pow(b1, 2))
var c2 = Math.Sqrt(Math.Pow(a2, 2) Math.Pow(b2, 2) )。
var deltaC = c1 - c2。
var deltaH = Math.Pow(deltaA, 2) Math.Pow(deltaB, 2) - Math.Pow(deltaC, 2) 。
deltaH = deltaH < 0 ? 0 : Math.Sqrt(deltaH)。
double sl = 1.0;
double kc = 1.0;
double kh = 1.0;
double Kl = 1.0;
double K1 = .045;
double K2 = 0.015;
var sc = 1.0 K1 * c1。
var sh = 1.0 K2*c1;
var i = Math.Pow(deltaL / (Kl * sl), 2)
Math.Pow(deltaC / (kc * sc), 2)
Math.Pow(deltaH / (kh * sh), 2) 。
var finalResult = i < 0 ? 0 : Math.Sqrt(i)。
return finalResult。
}
//將RGB顏色轉換為XYZ格式,然后轉換為LAB格式,這樣就可以進行色差演算法了。
public static double[] rgbToLab(intR, int G, int B)。
{
float[] xyz = new float[3] 。
float[] lab = new float[3] 。
float[] rgb = new float[3] 。
rgb[0] = R / 255.0f。
rgb[1] = G / 255.0f。
rgb[2] = B / 255.0f。
if (rgb[0] > .04045f)
{
rgb[0] = (float)Math.Pow((rgb[0] 0.055) / 1.055, 2.4)。)
}
else; }
{
rgb[0] = rgb[0] / 12.92f;
}
if (rgb[1] > .04045f)
{
rgb[1] = (float)Math.Pow((rgb[1] 0.055) / 1.055, 2.4)。)
}
else; }
{
rgb[1] = rgb[1] / 12.92f;
}
if (rgb[2] > .04045f)
{
rgb[2] = (float)Math.Pow((rgb[2] 0.055) / 1.055, 2.4)。)
}
else; }
{
rgb[2] = rgb[2] / 12.92f;
}
rgb[0] = rgb[0] * 100.0f;
rgb[1] = rgb[1] * 100.0f;
rgb[2] = rgb[2] * 100.0f;
xyz[0] = ((rgb[0] * . 412453f) (rgb[1] * .357580f) (rgb[2] * .180423f))。
xyz[1] = ((rgb[0] * 。 212671f) (rgb[1] * .715160f) (rgb[2] * .072169f))。
xyz[2] = ((rgb[0] * . 019334f) (rgb[1] * .119193f) (rgb[2] * .950227f)。
xyz[0] = xyz[0] / 95.047f。
xyz[1] = xyz[1] / 100.0f;
xyz[2] = xyz[2] / 108.883f;
if (xyz[0] > .008856f)
{
xyz[0] = (float)Math. Pow(xyz[0], (1.0 / 3.0) )。)
}
else; }
{
xyz[0] = (xyz[0] * 7.787f) (16.0f / 116.0f) 。
}
if (xyz[1] > .008856f)
{
xyz[1] = (float)Math.Pow(xyz[1], 1.0 /3.0) 。
}
else; }
{
xyz[1] = (xyz[1] * 7.787f) (16.0f / 116.0f) 。
}
if (xyz[2] > .008856f)
{
xyz[2] = (float)Math.Pow(xyz[2], 1.0 / 3.0) 。
}
else; }
{
xyz[2] = (xyz[2] * 7.787f) (16.0f / 116.0f) 。
}
lab[0] = (116.0f * xyz[1] ) - 16.0f;
lab[1] = 500.0f * (xyz[0] - xyz[1] )。)
lab[2] = 200.0f * (xyz[1] - xyz[2] )。)
return new double[] { lab[0] 。lab[1], lab[2] };
}
這里是C3類,它基本上只是帶有一些數學函式的顏色類,以使它更容易進行抖動
。class C3
{
int r, g, b;
public C3(int c) /span>
{
顏色 color = Color.FromArgb(c);
r = color.R。
g = color.G;
b = color.B。
}
public C3(intr, int g, int b)?
{
this.r = r。
this.g = g。
this.b = b;
}
public C3 add(C3 o)
{
return new C3(r o.r, g o.g, b o.b)。
}
public int clamp(int c)
{
return Math.Max(0, Math.Min(255, c))。
}
public int diff(C3 o)
{
int Rdiff = o.r - r。
int Gdiff = o.g - g。
int Bdiff = o.b - b。
int distanceSquared = Rdiff * Rdiff Gdiff * Gdiff Bdiff * Bdiff。
return distanceSquared。
}
public C3 mul(double)
{
return new C3((int)(d * r)。(int)(d * g), (int)(d * b)。
}
public C3 sub(C3 o)
{
return new C3(r - o.r, g - o.g, b - o.b)。
}
public Color toColor()
{
return Color.FromArgb(camp(r), clamp(g), clamp(b))。
}
public int toRGB()
{
return toColor().ToArgb()。
}
}
對于大量的代碼轉儲感到抱歉,這些函式只是相當大,我想提供我所能提供的一切。如果有人對更簡單或不同的方法有任何建議,或者你知道如何解決我遇到的問題,請讓我知道。我已經為我想要的結果嘗試了相當多的不同演算法,但我沒有能夠讓它們中的任何一個做我想要的事情。如果有任何幫助或想法,我們將不勝感激,謝謝!
uj5u.com熱心網友回復:
似乎當你在floydSteinbergDithering()中把錯誤轉移到鄰居處時,r,g,b值從未被夾緊,直到你把它們投回Color。
由于你使用的是int而不是byte,所以不能防止r、g和b溢位到負值或大于255的大值。
你應該考慮將 r、g 和 b 實作為屬性,當它們被設定為 0-255 時,它們將被夾緊。
這將確保它們的值永遠不會超出你的預期范圍(0 - 255)。
class C3
{
private int r;
public int R
{
get => r;
set
{
r = clamp(value)。
}
}
private int g。
public int G
{
get => g;
set
{
g = clamp(value)。
}
}
private int b。
public int B
{
get => b。
set
{
b = clamp(value)。
}
}
//其余的類。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/325797.html
標籤:
上一篇:如何在每次發射事件時創建一個訊息
