当前位置:   article > 正文

图像滤镜艺术---保留细节的磨皮之C#程序实现_c# 磨皮

c# 磨皮

上一篇博文“保留细节的磨皮滤镜之PS实现”一文中,我简单介绍了本人自己总结的一种非常简单的磨皮滤镜,这个滤镜在磨光皮肤的同时,会保留很不错的细节,今天,我将介绍使用C#程序实现这个磨皮的过程。

这里,我们同样是使用ZPhotoEngine库来实现,毕竟这个库中实现的效果跟PS是几乎一模一样的,关于下载地址,文章最后会给出,废话不多说了,磨皮步骤如下:

一,对原图的副本a执行表面模糊,半径15;

二,对原图执行高反差保留,半径1.0;

三,对高反差结果与原图做线性光图层处理,50%透明度即可;

根据以上三步,我的磨皮类主要代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Drawing;
  6. using System.Drawing.Imaging;
  7. namespace TestDemo
  8. {
  9. unsafe class ImageFilter
  10. {
  11. ZPhotoEngineDll zp = new ZPhotoEngineDll();
  12. public Bitmap SoftSkinFilter(Bitmap src, int blurRadius)
  13. {
  14. //表面模糊图层
  15. Bitmap a = zp.SurfaceBlur(src, 28, blurRadius);
  16. //高反差图层
  17. Bitmap highPass = zp.HighPassProcess(src, 1.0f);
  18. BitmapData srcData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
  19. BitmapData dstData = highPass.LockBits(new Rectangle(0, 0, highPass.Width, highPass.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
  20. byte* p = (byte*)srcData.Scan0;
  21. byte* dstP = (byte*)dstData.Scan0;
  22. int offset = srcData.Stride - a.Width * 4;
  23. int temp = 0;
  24. for (int j = 0; j < a.Height; j++)
  25. {
  26. for (int i = 0; i < a.Width; i++)
  27. {
  28. Process image...
  29. //线性光图层混合
  30. temp = zp.ModeLinearLight(p[0], dstP[0]);
  31. //透明度50%
  32. dstP[0] = (byte)((p[0] + temp) >> 1);
  33. temp = zp.ModeLinearLight(p[1], dstP[1]);
  34. dstP[1] = (byte)((p[1] + temp) >> 1);
  35. temp = zp.ModeLinearLight(p[2], dstP[2]);
  36. dstP[2] = (byte)((p[2] + temp) >> 1);
  37. dstP += 4;
  38. p += 4;
  39. }
  40. dstP += offset;
  41. p += offset;
  42. }
  43. a.UnlockBits(srcData);
  44. highPass.UnlockBits(dstData);
  45. return highPass;
  46. }
  47. }
  48. }
界面部分主要代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Drawing.Imaging;
  10. namespace TestDemo
  11. {
  12. public partial class Form1 : Form
  13. {
  14. public Form1()
  15. {
  16. InitializeComponent();
  17. }
  18. #region 变量声明
  19. //图像路径
  20. private String curFileName = null;
  21. //当前图像变量
  22. private Bitmap curBitmap = null;
  23. //原始图像变量
  24. private Bitmap srcBitmap = null;
  25. //
  26. ImageFilter imfilter = new ImageFilter();
  27. #endregion
  28. #region 图像打开保存模块
  29. //打开图像函数
  30. public void OpenFile()
  31. {
  32. OpenFileDialog ofd = new OpenFileDialog();
  33. ofd.Filter = "所有图像文件 | *.bmp; *.pcx; *.png; *.jpg; *.gif;" +
  34. "*.tif; *.ico; *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf|" +
  35. "位图( *.bmp; *.jpg; *.png;...) | *.bmp; *.pcx; *.png; *.jpg; *.gif; *.tif; *.ico|" +
  36. "矢量图( *.wmf; *.eps; *.emf;...) | *.dxf; *.cgm; *.cdr; *.wmf; *.eps; *.emf";
  37. ofd.ShowHelp = true;
  38. ofd.Title = "打开图像文件";
  39. if (ofd.ShowDialog() == DialogResult.OK)
  40. {
  41. curFileName = ofd.FileName;
  42. try
  43. {
  44. curBitmap = (Bitmap)System.Drawing.Image.FromFile(curFileName);
  45. srcBitmap = new Bitmap(curBitmap);
  46. }
  47. catch (Exception exp)
  48. { MessageBox.Show(exp.Message); }
  49. }
  50. }
  51. //保存图像函数
  52. public void SaveFile()
  53. {
  54. SaveFileDialog sfd = new SaveFileDialog();
  55. sfd.Filter = "PNG文件(*.png)|*.png";
  56. if (sfd.ShowDialog() == DialogResult.OK)
  57. {
  58. pictureBox1.Image.Save(sfd.FileName, ImageFormat.Png);
  59. }
  60. }
  61. //打开图像
  62. private void openBtn_Click(object sender, EventArgs e)
  63. {
  64. OpenFile();
  65. if (curBitmap != null)
  66. {
  67. pictureBox1.Image = (Image)curBitmap;
  68. }
  69. }
  70. //保存图像
  71. private void saveBtn_Click(object sender, EventArgs e)
  72. {
  73. if (pictureBox1.Image != null)
  74. SaveFile();
  75. }
  76. #endregion
  77. //确定
  78. private void okBtn_Click(object sender, EventArgs e)
  79. {
  80. if (pictureBox1.Image != null)
  81. {
  82. int radius = Convert.ToInt32(textBox1.Text.ToString());
  83. if (radius >= 0 && radius <= 20)
  84. {
  85. pictureBox1.Image = (Image)imfilter.SoftSkinFilter(curBitmap, radius);
  86. }
  87. }
  88. }
  89. }
  90. }
程序界面如下:


最后,放上效果图:


原图                                                                                  C#程序效果图


PS效果图

大家可以对比一下,PS效果跟本文实现效果是一模一样的,差别几乎是肉眼看不到的呵呵。

最后,放上一些下载连接:

1,ZPhotoEngine库下载连接:点击打开链接

2,磨皮代码DEMO免费下载连接:点击打开链接


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/257671
推荐阅读
相关标签
  

闽ICP备14008679号