处理原理

找到图片边缘像素的平均颜色值,以及边缘像素点与均色值之间的最大差值,然后整个图片中只保留颜色差值大于最大差值的点,即可。

源码

Bitmap m = pictureBox1.Image as Bitmap;
            if (m == null) return;

double targetWidth = 500;
            double scale = targetWidth / m.Width;
            Bitmap newM = new Bitmap((int)(m.Width * scale), (int)(m.Height * scale));
            var g = Graphics.FromImage(newM);
            g.DrawImage(m,
                new Rectangle(0, 0, newM.Width, newM.Height),
                new Rectangle(0, 0, m.Width, m.Height ),
                GraphicsUnit.Pixel);

//背景平均色
            Color avgColorRow = avgColorInRow(newM, 0, 1, 2, newM.Height - 1, newM.Height - 2, newM.Height - 3);
            Color avgColorCol = avgColorInCol(newM, 0, 1, 2, newM.Width - 1, newM.Width - 2, newM.Width - 3);
            Color avgColor = Color.FromArgb((avgColorRow.R + avgColorCol.R) / 2,
                (avgColorRow.G + avgColorCol.G) / 2,
                (avgColorRow.B + avgColorCol.B) / 2);

//背景最大差值
            var maxDifferRow = maxAvgColorInRow(newM, avgColor, 0, 1, 2, newM.Height - 1, newM.Height - 2, newM.Height - 3);
            var maxDifferCol = maxAvgColorInCol(newM, avgColor, 0, 1, 2, newM.Width - 1, newM.Width - 2, newM.Width - 3);
            var maxDiffer = Math.Max(maxDifferRow, maxDifferCol);

//只保留高于最大差值的点
            Dictionary<Point, Color> validPs = new Dictionary<Point, Color>();
            for (int i = 0; i < newM.Width; i++)
            {
                for (int j = 0; j < newM.Height; j++)
                {
                    var clr = newM.GetPixel(i, j);
                    var differ = Math.Abs(clr.R + clr.G + clr.B - avgColor.R - avgColor.G - avgColor.B);
                    if (differ > maxDiffer)
                    {
                        validPs.Add(new Point(i, j), label1.BackColor);
                    }
                }
            }
            //只保留有效区域
            int minX = validPs.Min(a => a.Key.X);
            int maxX = validPs.Max(a => a.Key.X);
            int minY = validPs.Min(a => a.Key.Y);
            int maxY = validPs.Max(a => a.Key.Y);

Bitmap rstM = new Bitmap(maxX - minX + 1, maxY - minY + 1);
            g = Graphics.FromImage(rstM);

List<PointColor> objs = new List<PointColor>();
            foreach (var p in validPs)
            {
                rstM.SetPixel(p.Key.X - minX, p.Key.Y - minY, p.Value);
                objs.Add(new PointColor()
                {
                    Color = p.Value,
                    X = p.Key.X - minX,
                    Y = p.Key.Y - minY
                }
                 );
            }
            if (drawObjs.ContainsKey(textBox1.Text))
                drawObjs.Remove(textBox1.Text);
            drawObjs.Add(textBox1.Text, objs);

pictureBox1.Image = rstM;

}

private Color avgColorInRow(Bitmap newM, params int[] rowindex)
        {
            //首行平均色
            int avgR = 0, avgG = 0, avgB = 0;

for (int r = 0; r < rowindex.Length; r++)
            {
                for (int c = 0; c < newM.Width; c++)
                {
                    var clr = newM.GetPixel(c, rowindex[r]);
                    avgR += clr.R;
                    avgG += clr.G;
                    avgB += clr.B;
                }

}
            return Color.FromArgb(avgR / (newM.Width * rowindex.Length),
                avgG / (newM.Width * rowindex.Length),
                avgB / (newM.Width * rowindex.Length));
        }
         
        private Color avgColorInCol(Bitmap newM, params int[] colindex)
        {
            //首行平均色
            int avgR = 0, avgG = 0, avgB = 0;
            for (int c = 0; c < colindex.Length; c++)
            {
                for (int r = 0; r < newM.Height; r++)
                {
                    var clr = newM.GetPixel(colindex[c], r);
                    avgR += clr.R;
                    avgG += clr.G;
                    avgB += clr.B;
                }
            }
            return Color.FromArgb(avgR / (newM.Height * colindex.Length),
                avgG / (newM.Height * colindex.Length),
                avgB / (newM.Height * colindex.Length));
        }

private int maxAvgColorInRow(Bitmap newM, Color avgColor, params int[] rowIndex)
        {
            int maxDiffer = 0;
            for (int r = 0; r < rowIndex.Length; r++)
            {
                for (int c = 0; c < newM.Width; c++)
                {
                    var clr = newM.GetPixel(c, rowIndex[r]);
                    var differ = Math.Abs(clr.R + clr.G + clr.B - avgColor.R - avgColor.G - avgColor.B);
                    if (differ > maxDiffer)
                        maxDiffer = differ;
                }
            }
            return maxDiffer;
        }

private int maxAvgColorInCol(Bitmap newM, Color avgColor, params int[] colIndex)
        {
            int maxDiffer = 0;
            for (int c = 0; c < colIndex.Length; c++)
            {
                for (int r = 0; r < newM.Height; r++)
                {
                    var clr = newM.GetPixel(colIndex[c], r);
                    var differ = Math.Abs(clr.R + clr.G + clr.B - avgColor.R - avgColor.G - avgColor.B);
                    if (differ > maxDiffer)
                        maxDiffer = differ;
                }
            }
            return maxDiffer;
        }

C# GDI 手绘图片转化为电子版处理相关推荐

  1. 手写签名转化为电子版

    用手机拍摄手写签名 .进行裁剪为合适大小 重新着色 设置为透明色 Ctrl+c复制图片 Ctrl+Alt+V 选择位图 将图片设置为 浮于文字上方 8.最终得到的结果

  2. 用Python将GIF图片转化成手绘图

    今天带大家来将GIF图片转化成一张好看的手绘图片 用的是PIL读取图片 和numpy处理图片 完整代码如下: from PIL import Image import numpy as npvec_e ...

  3. python 归一化_只需 45 秒,Python 给故宫画一组手绘图!

    作者 | 丁彦军 责编 | 伍杏玲 13日早晨,当北京市民拉开窗帘时发现,窗外雪花纷纷扬扬在空中飘落,而且越下越大,树上.草地.屋顶.道路上,都落满雪花.京城银装素裹,这是今冬以来北京迎来的第二场降雪 ...

  4. 只需 45 秒,Python 给故宫画一组手绘图!

    作者 | 丁彦军 责编 | 屠敏 13日早晨,当北京市民拉开窗帘时发现,窗外雪花纷纷扬扬在空中飘落,而且越下越大,树上.草地.屋顶.道路上,都落满雪花.京城银装素裹,这是今冬以来北京迎来的第二场降雪. ...

  5. numpy生成手绘图代码原理的一些解释

    代码如下: from PIL import Image import numpy as np #主要实现一下四点 #1)手绘图是灰度图像 #2)手绘图的边界线条比较重 #3)相同或相近色彩趋于白色 # ...

  6. python 画八角形步骤_只需 45 秒,Python 给故宫画一组手绘图!

    作者 |   丁彦军 责编 | 屠敏 13 日早晨,当北京市民拉开窗帘时发现,窗外雪花纷纷扬扬在空中飘落,而且越下越大,树上.草地.屋顶.道路上,都落满雪花.京城银装素裹,这是今冬以来北京迎来的第二场 ...

  7. Python手绘图了解一下!

    13日早晨,当北京市民拉开窗帘时发现,窗外雪花纷纷扬扬在空中飘落,而且越下越大,树上.草地.屋顶.道路上,都落满雪花.京城银装素裹,这是今冬以来北京迎来的第二场降雪. 一下雪,北京就变成了北平,故宫就 ...

  8. 给手绘图着色(添加颜色或色彩):CVPR2020论文点评

    给手绘图着色(添加颜色或色彩):CVPR2020论文点评 Learning to Shade Hand-drawn Sketches 论文链接:https://arxiv.org/pdf/2002.1 ...

  9. 这12张手绘图,让我彻底搞懂了微服务架构!

    点击上方"方志朋",选择"设为星标" 回复"666"获取新整理的面试文章 作者:tengshe789 juejin.im/post/5c0b ...

最新文章

  1. minor gc和Major GC,Full GC的触发条件
  2. 树莓派该文件名_树莓派学习笔记(2):常用linux命令
  3. 计算机ppt制作教案,全国计算机一级B考试基础篇制作人徐守威PPT教案学习.pptx
  4. 如何让插件加载到Qt Designer
  5. 英特尔开源WebRTC开发套件OWT
  6. 提升体验-支持Chrome Custom Tabs
  7. 汉字和utf编码转换
  8. android xml黑体字_如何在 Android 上使用思源黑体作为系统字体?
  9. Idea 我的快捷键总结
  10. 解决luyten 启动报错:this application requires a java runtime
  11. 【CCNA第二天】路由器密码破解及恢复
  12. AC97 与 HD audio的区别
  13. flink 时间语义、水位线(Watermark)、生成水位线、水位线的传递
  14. JD如何获取cookie
  15. 在浏览器中使用百度地图的定位服务获得经纬度
  16. 2016中国方案商大会在京成功召开
  17. 关于手机唯一识别码的研究meid和imei
  18. 年轻计算机科学家,15岁最年轻科学家具体什么情况? 谈方琳个人资料介绍有何成就...
  19. python随机生成无序列表_python实现无序列表:链表
  20. componentWillReceiveProps为什么deprecated

热门文章

  1. 浏览器的input禁用输入法
  2. 浏览器网页视频怎么快速下载到本地?
  3. 测试硬盘,ssd,优盘读取速度
  4. 现场工程师出手-PCAPHub与云SSH隧道稳妥实现异地LAN IIoT联测
  5. CAD 批量打印,输出pdf,plt的工具
  6. 百度网盘免费扩容2T的内幕
  7. 多屏互动之Windows与Mac下的非自带的远程桌面应用
  8. Luxurious Houses - CodeForces - 581B
  9. 佛祖,你为什么不帮我
  10. 类型的Overflow与underflow