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

相关文章

C#中读取XML文件的四种常用方法

《C#中读取XML文件的四种常用方法》Xml是Internet环境中跨平台的,依赖于内容的技术,是当前处理结构化文档信息的有力工具,下面我们就来看看C#中读取XML文件的方法都有哪些吧... 目录XML简介格式C#读取XML文件方法使用XmlDocument使用XmlTextReader/XmlTextWr

Java数字转换工具类NumberUtil的使用

《Java数字转换工具类NumberUtil的使用》NumberUtil是一个功能强大的Java工具类,用于处理数字的各种操作,包括数值运算、格式化、随机数生成和数值判断,下面就来介绍一下Number... 目录一、NumberUtil类概述二、主要功能介绍1. 数值运算2. 格式化3. 数值判断4. 随机

Deepseek使用指南与提问优化策略方式

《Deepseek使用指南与提问优化策略方式》本文介绍了DeepSeek语义搜索引擎的核心功能、集成方法及优化提问策略,通过自然语言处理和机器学习提供精准搜索结果,适用于智能客服、知识库检索等领域... 目录序言1. DeepSeek 概述2. DeepSeek 的集成与使用2.1 DeepSeek API

使用Navicat工具比对两个数据库所有表结构的差异案例详解

《使用Navicat工具比对两个数据库所有表结构的差异案例详解》:本文主要介绍如何使用Navicat工具对比两个数据库test_old和test_new,并生成相应的DDLSQL语句,以便将te... 目录概要案例一、如图两个数据库test_old和test_new进行比较:二、开始比较总结概要公司存在多

Go Mongox轻松实现MongoDB的时间字段自动填充

《GoMongox轻松实现MongoDB的时间字段自动填充》这篇文章主要为大家详细介绍了Go语言如何使用mongox库,在插入和更新数据时自动填充时间字段,从而提升开发效率并减少重复代码,需要的可以... 目录前言时间字段填充规则Mongox 的安装使用 Mongox 进行插入操作使用 Mongox 进行更

Tomcat高效部署与性能优化方式

《Tomcat高效部署与性能优化方式》本文介绍了如何高效部署Tomcat并进行性能优化,以确保Web应用的稳定运行和高效响应,高效部署包括环境准备、安装Tomcat、配置Tomcat、部署应用和启动T... 目录Tomcat高效部署与性能优化一、引言二、Tomcat高效部署三、Tomcat性能优化总结Tom

Java中基于注解的代码生成工具MapStruct映射使用详解

《Java中基于注解的代码生成工具MapStruct映射使用详解》MapStruct作为一个基于注解的代码生成工具,为我们提供了一种更加优雅、高效的解决方案,本文主要为大家介绍了它的具体使用,感兴趣... 目录介绍优缺点优点缺点核心注解及详细使用语法说明@Mapper@Mapping@Mappings@Co

C#比较两个List集合内容是否相同的几种方法

《C#比较两个List集合内容是否相同的几种方法》本文详细介绍了在C#中比较两个List集合内容是否相同的方法,包括非自定义类和自定义类的元素比较,对于非自定义类,可以使用SequenceEqual、... 目录 一、非自定义类的元素比较1. 使用 SequenceEqual 方法(顺序和内容都相等)2.

使用Python实现图片和base64转换工具

《使用Python实现图片和base64转换工具》这篇文章主要为大家详细介绍了如何使用Python中的base64模块编写一个工具,可以实现图片和Base64编码之间的转换,感兴趣的小伙伴可以了解下... 简介使用python的base64模块来实现图片和Base64编码之间的转换。可以将图片转换为Bas

使用Java实现一个解析CURL脚本小工具

《使用Java实现一个解析CURL脚本小工具》文章介绍了如何使用Java实现一个解析CURL脚本的工具,该工具可以将CURL脚本中的Header解析为KVMap结构,获取URL路径、请求类型,解析UR... 目录使用示例实现原理具体实现CurlParserUtilCurlEntityICurlHandler