卡通图像处理
Posted iamai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了卡通图像处理相关的知识,希望对你有一定的参考价值。
图像处理类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
namespace CartonImage
public static class Help
public static 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);
unsafe
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;
public static 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;
public static 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);
unsafe
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;
调用的方法:
System.Drawing.Bitmap result = Help.CartoonFilterProcess(ImgSource, (int)slider1.Value);
处理效果前的图像:
处理后的图像:
这个效果处理真的不怎么样,只对有些照片看起来凑合。
以上是关于卡通图像处理的主要内容,如果未能解决你的问题,请参考以下文章
OpenCV-Python实战(番外篇)——OpenCV实现图像卡通化
图像处理(二十一)基于数据驱动的人脸卡通动画生成-Siggraph Asia 2014
图像处理:Tiler制作你的专属卡通头像和LOGO(圣诞特别篇)