漫画滤镜
所谓漫画滤镜就是通过复杂的算法来模拟漫画的特点,从而使真实照片呈现出漫画的风格。要实现漫画的效果,首先要了解漫画的特点,漫画具有几个比较明显的特点如下:
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;
}
效果图如下:
|
图像滤镜艺术---漫画滤镜
最新推荐文章于 2024-10-07 14:07:13 发布