影象特效---漫畫濾鏡
阿新 • • 發佈:2019-01-02
漫畫濾鏡
所謂漫畫濾鏡就是通過複雜的演算法來模擬漫畫的特點,從而使真實照片呈現出漫畫的風格。要實現漫畫的效果,首先要了解漫畫的特點,漫畫具有幾個比較明顯的特點如下:
1,顏色泛用
漫畫中,使用的顏色比較單一,一般不會超過7種顏色,不像真實照片那樣,具有豐富的顏色種類;
2,邊緣較強
漫畫中,所有事物都有比較明顯的黑色邊緣,給人一種分割分明且突出的感覺;
3,形象誇張,內容概括
漫畫中,一般描述的人物表情,效果等等都十分誇張,而且內容高度概括,讓人浮想聯翩;
以上3點就是漫畫的特點,其中,第三點是由漫畫的本質決定的,這一點,我們真實的照片是無法實現的,因此,這一點我們在這裡不用考慮。對於1和2兩點,我們均可以使用相應的影象演算法,來模擬實現,具體如下:
對於顏色泛用,表現在影象演算法中,實際上就是減少顏色的種類,對顏色種類進行降維,這一點,我們可使用顏色聚類演算法,雙邊濾波演算法等等,其中,雙邊濾波在使顏色平坦的同時,還可以保留邊緣資訊。
對於邊緣保留,我們可以使用邊緣運算元來實現,比如拉普拉斯運算元,Sobel,Roberts,Prewitt運算元等等,也可以使用原圖-高斯模糊效果圖來實現,這樣可以保留更加完整的邊緣輪廓資訊。
以上是本人對漫畫演算法的分析,在這裡,本人使用的演算法如下:
第一,使用上一篇水彩畫濾鏡演算法,獲得單一顏色的影象P,這裡設定油畫演算法閾值為10,使P具有10種顏色描述。
第二,使用Sobel運算元進行邊緣檢測,這裡選用這個演算法,主要是為了兼顧速度和邊緣效果。
第三,設定邊緣強度控制變數edgeIntensity(edgeIntensity>=0),來控制邊緣強度調節,公式如下:
Pedge(x,y) = Min(255, Pedge(x,y)+edgeIntensity);
第四,邊緣融合,將第一和第三步驟中得到的結果圖進行 融合,來獲得最終的漫畫效果,公式如下:
Result(x,y) = Pedge(x,y)*P(x,y)/255;
以上就是演算法的整個過程,這裡放上核心程式碼如下:
private Bitmap SobelEdgeDetect(Bitmap src,int edgeIntensity)
{
int w = src.Width;
int h = src.Height;
Bitmap a = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
BitmapData srcData = src.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
BitmapData dstData = a.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
byte* pIn = (byte*)srcData.Scan0.ToPointer();
byte* pOut = (byte*)dstData.Scan0.ToPointer();
byte* p;
int offset = srcData.Stride - w * 4;
int r0, r1, r2, r3, r4, r5, r6, r7, r8;
int g1, g2, g3, g4, g5, g6, g7, g8, g0;
int b1, b2, b3, b4, b5, b6, b7, b8, b0;
double vR, vG, vB;
int stride = srcData.Stride;
int []lightMap={0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 20, 22, 23, 24, 25, 26, 27, 28, 29, 31, 32, 33, 34, 35, 36, 37, 39, 40, 41, 42, 43, 44, 45, 47, 48, 49, 50, 51, 52, 53, 54, 56, 57, 58, 59, 60, 61, 62, 64, 65, 66, 67,
68, 69, 70, 71, 73, 74, 75, 76, 77, 78, 79, 81, 82, 83, 84, 85, 86, 87, 88, 90, 91, 92, 93, 94, 95, 96, 98, 99, 100, 101, 102, 103, 104, 105, 107, 108, 109, 110, 111, 112, 113, 115, 116, 117, 118, 119, 120, 121, 123, 124, 125, 126, 127, 128, 129, 130, 132,
133, 134, 135, 136, 137, 138, 140, 141, 142, 143, 144, 145, 146, 147, 149, 150, 151, 152, 153, 154, 155, 156, 158, 159, 160, 161, 162, 163, 164, 165, 166, 168, 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 181, 182, 183, 184, 185, 186, 187, 188, 189,
190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 212, 213, 214, 215, 216, 217, 218, 219, 220, 220, 221, 222, 223, 224, 225, 225, 226, 227, 228, 229, 229, 230, 231, 232, 233, 233, 234, 235, 235,
236, 237, 238, 238, 239, 240, 240, 241, 242, 242, 243, 243, 244, 245, 245, 246, 246, 247, 248, 248, 249, 249, 250, 250, 251, 251, 252, 252, 253, 253, 253, 254, 254, 255, 255};
int gray = 0;
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
if (i == 0 || i == w - 1 || j == 0 || j == h - 1)
{
pOut[0] = (byte)0;
pOut[1] = (byte)0;
pOut[2] = (byte)0;
pOut[3] = (byte)255;
}
else
{
//左上
p = pIn - stride - 4;
r1 = p[2];
g1 = p[1];
b1 = p[0];
//正上
p = pIn - stride;
r2 = p[2];
g2 = p[1];
b2 = p[0];
//右上
p = pIn - stride + 4;
r3 = p[2];
g3 = p[1];
b3 = p[0];
//左
p = pIn - 4;
r4 = p[2];
g4 = p[1];
b4 = p[0];
//右
p = pIn + 4;
r5 = p[2];
g5 = p[1];
b5 = p[0];
//左下
p = pIn + stride - 4;
r6 = p[2];
g6 = p[1];
b6 = p[0];
//正下
p = pIn + stride;
r7 = p[2];
g7 = p[1];
b7 = p[0];
// 右下
p = pIn + stride + 4;
r8 = p[2];
g8 = p[1];
b8 = p[0];
//中心點
p = pIn;
r0 = p[2];
g0 = p[1];
b0 = p[0];
//使用模板
vR = (double)(Math.Abs(r1 + 2 * r4 + r6 - r3 - 2 * r5 - r8) + Math.Abs(r1 + 2 * r2 + r3 - r6 - 2 * r7 - r8));
vG = (double)(Math.Abs(g1 + 2 * g4 + g6 - g3 - 2 * g5 - g8) + Math.Abs(g1 + 2 * g2 + g3 - g6 - 2 * g7 - g8));
vB = (double)(Math.Abs(b1 + 2 * b4 + b6 - b3 - 2 * b5 - b8) + Math.Abs(b1 + 2 * b2 + b3 - b6 - 2 * b7 - b8));
vB = Math.Min(255, Math.Max(0, vB));
vG = Math.Min(255, Math.Max(0, vG));
vR = Math.Min(255, Math.Max(0, vR));
gray = lightMap[255 - (int)(vB + vG + vR) / 3];
gray = Math.Min(255, gray + edgeIntensity);
pOut[0] = (byte)gray;
pOut[1] = (byte)gray;
pOut[2] = (byte)gray;
pOut[3] = (byte)255;
}
pIn += 4;
pOut += 4;
}
pIn += offset;
pOut += offset;
}
a.UnlockBits(dstData);
src.UnlockBits(srcData);
return a;
}
private Bitmap OilpaintFilterProcess(Bitmap srcBitmap, int radius, int smooth)
{
if (radius == 0)
return srcBitmap;
smooth = smooth < 1 ? 1 : smooth;
smooth = Math.Max(1, smooth);
Bitmap a = new Bitmap(srcBitmap);
int w = srcBitmap.Width;
int h = srcBitmap.Height;
if (radius > Math.Min(w, h) / 2)
radius = (int)(Math.Min(w, h) / 2 - 0.5);
System.Drawing.Imaging.BitmapData srcData = a.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
IntPtr ptr = srcData.Scan0;
int bytes = h * srcData.Stride;
byte[] srcValues = new byte[bytes];
System.Runtime.InteropServices.Marshal.Copy(ptr, srcValues, 0, bytes);
byte[] tempValues = (byte[])srcValues.Clone();
int stride = srcData.Stride;
int i, j, k;
int unit = 4;
int[] gray_bt = new int[smooth];
int[] r_bt = new int[smooth];
int[] g_bt = new int[smooth];
int[] b_bt = new int[smooth];
int[] gray_bt_src = new int[smooth];
int[] r_bt_src = new int[smooth];
int[] g_bt_src = new int[smooth];
int[] b_bt_src = new int[smooth];
int r, g, b;
int gray = 0, bt_index = 0, max = 0, maxindex = 0;
i = 0;
bool frist = true;
int pos = 0;
for (j = 0; j < h; j++)
{
if (frist)
{
for (int m = -radius; m <= radius; m++)
{
for (int n = -radius; n <= radius; n++)
{
pos = Math.Abs(n) * unit + Math.Abs(m) * stride;
b = srcValues[pos++];
g = srcValues[pos++];
r = srcValues[pos];
gray = (b + g + r) / 3;
bt_index = gray * smooth >> 8;
gray_bt_src[bt_index]++;
b_bt_src[bt_index] += b;
g_bt_src[bt_index] += g;
r_bt_src[bt_index] += r;
}
}
Array.Copy(gray_bt_src, gray_bt, smooth);
Array.Copy(b_bt_src, b_bt, smooth);
Array.Copy(g_bt_src, g_bt, smooth);
Array.Copy(r_bt_src, r_bt, smooth);
max = 0;
maxindex = 0;
for (k = 0; k < smooth; k++)
{
if (max < gray_bt[k])
{
max = gray_bt[k];
maxindex = k;
}
}
pos = j * stride;
tempValues[pos++] = (byte)(b_bt[maxindex] / max);
tempValues[pos++] = (byte)(g_bt[maxindex] / max);
tempValues[pos] = (byte)(r_bt[maxindex] / max);
frist = false;
}
else
{
for (int m = -radius; m <= radius; m++)
{
pos = Math.Abs(m) * unit + Math.Abs(j - radius - 1) * stride;
b = srcValues[pos++];
g = srcValues[pos++];
r = srcValues[pos];
gray = (b + g + r) / 3;
bt_index = gray * smooth >> 8;
gray_bt_src[bt_index]--;
b_bt_src[bt_index] -= b;
g_bt_src[bt_index] -= g;
r_bt_src[bt_index] -= r;
pos = Math.Abs(m) * unit + Math.Abs(j + radius) % h * stride;
b = srcValues[pos++];
g = srcValues[pos++];
r = srcValues[pos];
gray = (b + g + r) / 3;
bt_index = gray * smooth >> 8;
gray_bt_src[bt_index]++;
b_bt_src[bt_index] += b;
g_bt_src[bt_index] += g;
r_bt_src[bt_index] += r;
}
Array.Copy(gray_bt_src, gray_bt, smooth);
Array.Copy(b_bt_src, b_bt, smooth);
Array.Copy(g_bt_src, g_bt, smooth);
Array.Copy(r_bt_src, r_bt, smooth);
}
for (i = 1; i < w; i++)
{
for (int m = -radius; m <= radius; m++)
{
pos = Math.Abs(i - radius - 1) * unit + Math.Abs(j + m) % h * stride;
b = srcValues[pos++];
g = srcValues[pos++];
r = srcValues[pos];
gray = (b + g + r) / 3;
bt_index = gray * smooth >> 8;
gray_bt[bt_index]--;
b_bt[bt_index] -= b;
g_bt[bt_index] -= g;
r_bt[bt_index] -= r;
pos = Math.Abs(i + radius) % w * unit + Math.Abs(j + m) % h * stride;
b = srcValues[pos++];
g = srcValues[pos++];
r = srcValues[pos];
gray = (b + g + r) / 3;
bt_index = gray * smooth >> 8;
gray_bt[bt_index]++;
b_bt[bt_index] += b;
g_bt[bt_index] += g;
r_bt[bt_index] += r;
}
max = 0;
maxindex = 0;
for (k = 0; k < smooth; k++)
{
if (max < gray_bt[k])
{
max = gray_bt[k];
maxindex = k;
}
}
pos = i * unit + j * stride;
tempValues[pos++] = (byte)(b_bt[maxindex] / max);
tempValues[pos++] = (byte)(g_bt[maxindex] / max);
tempValues[pos] = (byte)(r_bt[maxindex] / max);
}
}
srcValues = (byte[])tempValues.Clone();
System.Runtime.InteropServices.Marshal.Copy(srcValues, 0, ptr, bytes);
a.UnlockBits(srcData);
return a;
}
private Bitmap CartoonFilterProcess(Bitmap src, int edgeIntensity)
{
Bitmap edgeBitmap = SobelEdgeDetect(src, edgeIntensity);
Bitmap dst = OilpaintFilterProcess(new Bitmap(src),12,10);;
int w = dst.Width;
int h = dst.Height;
BitmapData dstData = dst.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
BitmapData edgeData = edgeBitmap.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
byte* pEdge = (byte*)edgeData.Scan0;
byte* pDst = (byte*)dstData.Scan0;
int offset = dstData.Stride - w * 4;
for (int j = 0; j < h; j++)
{
for (int i = 0; i < w; i++)
{
pDst[0] = (byte)(pDst[0] * pEdge[0] / 255);
pDst[1] = (byte)(pDst[1] * pEdge[1] / 255);
pDst[2] = (byte)(pDst[2] * pEdge[2] / 255);
pDst[3] = (byte)255;
pEdge += 4;
pDst += 4;
}
pEdge += offset;
pDst += offset;
}
dst.UnlockBits(dstData);
edgeBitmap.UnlockBits(edgeData);
return dst;
}
效果圖如下:
原圖
效果圖