C# 魔棒工具-漫水填充算法优化

2023-11-11 18:11

本文主要是介绍C# 魔棒工具-漫水填充算法优化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

之前在魔棒工具的文章里写过一个漫水填充算法,实际使用效果并不好。这是因为算法将图片转为灰度再去与种子点的灰度比较。可以想象,这种做法是不合理的(虽然通常都是这么做)。比如两个视觉上相差很明显的颜色,其灰度值很有可能一样或者相差不大(无论你怎么修改RGB三者权重,终归会遇到这种情况)。所以最好是对颜色的ARGB四个分值分开进行比较,如果速度允许,可以计算方差;如果希望速度快一点,就直接计算差值。

1.按灰度检测

        /// <summary>///  漫水填充 FloodFill()按灰度检测,FloodFill_argb()按四通道分量检测/// </summary>/// <param name="src">原图</param>/// <param name="location">检测点</param>/// <param name="fillColor">填充颜色</param>/// <param name="threshould">阈值</param>/// <returns>填充图,非填充部分为默认值</returns>unsafe public Bitmap FloodFill(Bitmap src, Point location, Color fillColor, int threshould){try{Bitmap srcbmp = src;Color backColor = srcbmp.GetPixel(location.X, location.Y);Bitmap dstbmp = new Bitmap(src.Width, src.Height);int w = srcbmp.Width;int h = srcbmp.Height;Stack<Point> fillPoints = new Stack<Point>(w * h);System.Drawing.Imaging.BitmapData srcbmpData = srcbmp.LockBits(new Rectangle(0, 0, srcbmp.Width, srcbmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);System.Drawing.Imaging.BitmapData dstbmpData = dstbmp.LockBits(new Rectangle(0, 0, dstbmp.Width, dstbmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);int stride = srcbmpData.Stride;byte* srcbuf = (byte*)srcbmpData.Scan0.ToPointer();byte* dstbuf = (byte*)dstbmpData.Scan0.ToPointer();int  cr = backColor.R,  cg = backColor.G, cb = backColor.B, ca = backColor.A;byte fcr = fillColor.R, fcg = fillColor.G, fcb = fillColor.B;if (location.X < 0 || location.X >= w || location.Y < 0 || location.Y >= h) return null;fillPoints.Push(new Point(location.X, location.Y));int[,] mask = new int[w, h];while (fillPoints.Count > 0){Point p = fillPoints.Pop();mask[p.X, p.Y] = 1;dstbuf[3 * p.X + p.Y * stride] = fcb;dstbuf[3 * p.X + 1 + p.Y * stride] =  fcg;dstbuf[3 * p.X + 2 + p.Y * stride] =  fcr;if (p.X > 0 && (mask[p.X - 1, p.Y] != 1)&& Math.Abs(cb - srcbuf[3 * (p.X - 1) + p.Y * stride]) + Math.Abs(cg - srcbuf[3 * (p.X - 1) + 1 + p.Y * stride]) + Math.Abs(cr - srcbuf[3 * (p.X - 1) + 2 + p.Y * stride]) < threshould){dstbuf[3 * (p.X - 1) + p.Y * stride] = fcb;dstbuf[3 * (p.X - 1) + 1 + p.Y * stride] = fcg;dstbuf[3 * (p.X - 1) + 2 + p.Y * stride] = fcr;fillPoints.Push(new Point(p.X - 1, p.Y));mask[p.X - 1, p.Y] = 1;}if (p.X < w - 1 && (mask[p.X + 1, p.Y] != 1)&& Math.Abs(cb - srcbuf[3 * (p.X + 1) + p.Y * stride]) + Math.Abs(cg - srcbuf[3 * (p.X + 1) + 1 + p.Y * stride]) + Math.Abs(cr - srcbuf[3 * (p.X + 1) + 2+p.Y * stride]) < threshould ){dstbuf[3 * (p.X + 1) + p.Y * stride] = fcb;dstbuf[3 * (p.X + 1) + 1 + p.Y * stride] = fcg;dstbuf[3 * (p.X + 1) + 2 + p.Y * stride] = fcr;fillPoints.Push(new Point(p.X + 1, p.Y));mask[p.X + 1, p.Y] = 1;}if (p.Y > 0 && (mask[p.X, p.Y - 1] != 1)&& Math.Abs(cb - srcbuf[3 * p.X + (p.Y - 1) * stride]) + Math.Abs(cg - srcbuf[3 * p.X + 1 + (p.Y - 1) * stride]) + Math.Abs(cr - srcbuf[3 * p.X +2+ (p.Y - 1) * stride]) < threshould){dstbuf[3 * p.X + (p.Y - 1) * stride] = fcb;dstbuf[3 * p.X + 1 + (p.Y - 1) * stride] = fcg;dstbuf[3 * p.X + 2 + (p.Y - 1) * stride] = fcr;fillPoints.Push(new Point(p.X, p.Y - 1));mask[p.X, p.Y - 1] = 1;}if (p.Y < h - 1 && (mask[p.X, p.Y + 1] != 1)&& Math.Abs(cb - srcbuf[3 * p.X + (p.Y + 1) * stride]) + Math.Abs(cg - srcbuf[3 * p.X +1+ (p.Y + 1) * stride]) + Math.Abs(cr - srcbuf[3 * p.X +2+ (p.Y + 1) * stride]) < threshould){dstbuf[3 * p.X + (p.Y + 1) * stride] = fcb;dstbuf[3 * p.X + 1 + (p.Y + 1) * stride] = fcg;dstbuf[3 * p.X + 2 + (p.Y + 1) * stride] = fcr;fillPoints.Push(new Point(p.X, p.Y + 1));mask[p.X, p.Y + 1] = 1;}}fillPoints.Clear();srcbmp.UnlockBits(srcbmpData);dstbmp.UnlockBits(dstbmpData);return dstbmp;}catch (Exception exp){System.Windows.MessageBox.Show(exp.Message);return null;}}
        public int GetGray(byte r, byte g, byte b){return (int)(r * 77 + g * 151 + b * 28) >> 8;//按权重计算灰度值}


2.按颜色ARGB四分量检测

这里直接计算差值儿没有计算方差,其实效果已经足够。

        unsafe public Bitmap FloodFill_argb(Bitmap src, Point location, Color fillColor, int threshould){try{Bitmap srcbmp = src;Color backColor = srcbmp.GetPixel(location.X, location.Y);Bitmap dstbmp = new Bitmap(src.Width, src.Height);int w = srcbmp.Width;int h = srcbmp.Height;Stack<Point> fillPoints = new Stack<Point>(w * h);System.Drawing.Imaging.BitmapData bmpData = srcbmp.LockBits(new Rectangle(0, 0, srcbmp.Width, srcbmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);System.Drawing.Imaging.BitmapData dstbmpData = dstbmp.LockBits(new Rectangle(0, 0, dstbmp.Width, dstbmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);int stride = bmpData.Stride;int stridedst = dstbmpData.Stride;byte* srcbuf = (byte*)bmpData.Scan0.ToPointer();int* dstbuf = (int*)dstbmpData.Scan0.ToPointer();int cr = backColor.R, cg = backColor.G, cb = backColor.B, ca = backColor.A;byte fcr = fillColor.R, fcg = fillColor.G, fcb = fillColor.B;int fc = fillColor.ToArgb();if (location.X < 0 || location.X >= w || location.Y < 0 || location.Y >= h) return null;fillPoints.Push(new Point(location.X, location.Y));int[,] mask = new int[w, h];while (fillPoints.Count > 0){Point p = fillPoints.Pop();mask[p.X, p.Y] = 1;dstbuf[  p.X + p.Y * w] = fc;if (p.X > 0 && (mask[p.X - 1, p.Y] != 1)&& Math.Abs(cb - srcbuf[4 * (p.X - 1) + p.Y * stride]) + Math.Abs(cg - srcbuf[4 * (p.X - 1) + 1 + p.Y * stride]) + Math.Abs(cr - srcbuf[4 * (p.X - 1) + 2 + p.Y * stride]) < threshould&& Math.Abs(ca - srcbuf[4 * (p.X - 1) +3+ p.Y * stride]) < threshould/2){dstbuf[  (p.X - 1) + p.Y * w] = fc;fillPoints.Push(new Point(p.X - 1, p.Y));mask[p.X - 1, p.Y] = 1;}if (p.X < w - 1 && (mask[p.X + 1, p.Y] != 1)&& Math.Abs(cb - srcbuf[4 * (p.X + 1) + p.Y * stride]) + Math.Abs(cg - srcbuf[4 * (p.X + 1) + 1 + p.Y * stride]) + Math.Abs(cr - srcbuf[4 * (p.X + 1) + 2 + p.Y * stride]) < threshould&& Math.Abs(ca - srcbuf[4 * (p.X + 1) + 3 + p.Y * stride]) < threshould / 2){dstbuf[  (p.X + 1) + p.Y * w] = fc;fillPoints.Push(new Point(p.X + 1, p.Y));mask[p.X + 1, p.Y] = 1;}if (p.Y > 0 && (mask[p.X, p.Y - 1] != 1)&& Math.Abs(cb - srcbuf[4 * p.X + (p.Y - 1) * stride]) + Math.Abs(cg - srcbuf[4 * p.X + 1 + (p.Y - 1) * stride]) + Math.Abs(cr - srcbuf[4 * p.X + 2 + (p.Y - 1) * stride]) < threshould&& Math.Abs(ca - srcbuf[4 * p.X +3+ (p.Y - 1) * stride]) < threshould / 2){dstbuf[  p.X + (p.Y - 1) * w] = fc ;fillPoints.Push(new Point(p.X, p.Y - 1));mask[p.X, p.Y - 1] = 1;}if (p.Y < h - 1 && (mask[p.X, p.Y + 1] != 1)&& Math.Abs(cb - srcbuf[4 * p.X + (p.Y + 1) * stride]) + Math.Abs(cg - srcbuf[4 * p.X + 1 + (p.Y + 1) * stride]) + Math.Abs(cr - srcbuf[4 * p.X + 2 + (p.Y + 1) * stride]) < threshould&& Math.Abs(ca - srcbuf[4 * p.X +3+ (p.Y + 1) * stride]) < threshould /2){dstbuf[  p.X + (p.Y + 1) *w] = fc;fillPoints.Push(new Point(p.X, p.Y + 1));mask[p.X, p.Y + 1] = 1;}}fillPoints.Clear();srcbmp.UnlockBits(bmpData);dstbmp.UnlockBits(dstbmpData);return dstbmp;}catch (Exception exp){System.Windows.MessageBox.Show(exp.Message);return null;}}

3.测试

是驴是马,拉出来遛一遛
下图中要处理的图片,向日葵的花心与花瓣,分别为橙色与黄色,灰度值相差很小,右上角有个透明区域,里面有个黑色方框。
使用


调整阈值,为了观察,把透明度也调整一下,点选花瓣


试一下透明区域


结果不错。



这篇关于C# 魔棒工具-漫水填充算法优化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/391938

相关文章

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

SQLite3命令行工具最佳实践指南

《SQLite3命令行工具最佳实践指南》SQLite3是轻量级嵌入式数据库,无需服务器支持,具备ACID事务与跨平台特性,适用于小型项目和学习,sqlite3.exe作为命令行工具,支持SQL执行、数... 目录1. SQLite3简介和特点2. sqlite3.exe使用概述2.1 sqlite3.exe

基于Python实现一个Windows Tree命令工具

《基于Python实现一个WindowsTree命令工具》今天想要在Windows平台的CMD命令终端窗口中使用像Linux下的tree命令,打印一下目录结构层级树,然而还真有tree命令,但是发现... 目录引言实现代码使用说明可用选项示例用法功能特点添加到环境变量方法一:创建批处理文件并添加到PATH1

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

MyBatis Plus 中 update_time 字段自动填充失效的原因分析及解决方案(最新整理)

《MyBatisPlus中update_time字段自动填充失效的原因分析及解决方案(最新整理)》在使用MyBatisPlus时,通常我们会在数据库表中设置create_time和update... 目录前言一、问题现象二、原因分析三、总结:常见原因与解决方法对照表四、推荐写法前言在使用 MyBATis

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

C#如何去掉文件夹或文件名非法字符

《C#如何去掉文件夹或文件名非法字符》:本文主要介绍C#如何去掉文件夹或文件名非法字符的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C#去掉文件夹或文件名非法字符net类库提供了非法字符的数组这里还有个小窍门总结C#去掉文件夹或文件名非法字符实现有输入字

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式

《C#实现将Office文档(Word/Excel/PDF/PPT)转为Markdown格式》Markdown凭借简洁的语法、优良的可读性,以及对版本控制系统的高度兼容性,逐渐成为最受欢迎的文档格式... 目录为什么要将文档转换为 Markdown 格式使用工具将 Word 文档转换为 Markdown(.

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插