AForge.NET实现图中找水印(或图像匹配)并用颜色填充

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using AForge;
using AForge.Imaging;

namespace ImageCleaner
{
    public partial class Window : Form
    {
        //变量
        List<string> filePaths;
        List<string> templatePaths;
        Color replaceColor;
        string outputPath;
        float mult = 0;

        public Window()
        {
            InitializeComponent();
            mult = trackBar.Value / 100f;
            templateList.MultiSelect = false;
            fileList.MultiSelect = false;
        }


        private void filesButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog filesDialog = new OpenFileDialog();
            filesDialog.Title = "Select images to work with";
            filesDialog.Multiselect = true;
            filesDialog.Filter = "Image files|*.png;*.jpg";

            if (filesDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    fileImage.Image = System.Drawing.Image.FromFile(filesDialog.FileName);
                    fileList.Items.Clear();
                    filePaths = new List<string>(filesDialog.FileNames);

                    foreach (var item in filePaths)
                    {
                        //ListViewItem path = new ListViewItem(item.Split('\\')[item.Split('\\').Length-1]);
                        ListViewItem path = new ListViewItem(item);
                        fileList.Items.Add(path);
                    }
                }
                catch
                {
                    MessageBox.Show("Something went wrong", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        Bitmap ConvertToFormat(System.Drawing.Image image, PixelFormat format)
        {
            Bitmap copy = new Bitmap(image.Width, image.Height, format);
            using (Graphics gr = Graphics.FromImage(copy))
                gr.DrawImage(image, new Rectangle(0, 0, copy.Width, copy.Height));

            return copy;
        }
        private void startButton_Click(object sender, EventArgs e)
        {
            if (CheckConditions())
            {
                progressBar.Value = 0;
                progressBar.Maximum = filePaths.Count;
                SolidBrush brush = new SolidBrush(replaceColor);

                foreach (var path in filePaths)
                {
                    Bitmap sourceImage = ConvertToFormat(System.Drawing.Image.FromFile(path), PixelFormat.Format24bppRgb);

                    foreach (string item in templatePaths)
                    {

                        // 创建模板匹配算法的实例
                        //(将相似度阈值设置为92.5%)

                        ExhaustiveTemplateMatching tm = new ExhaustiveTemplateMatching(mult);
                        //查找具有上述指定相似性的所有匹配项

                        TemplateMatch[] matchings = tm.ProcessImage(sourceImage, (Bitmap)System.Drawing.Image.FromFile(item));
                        //高亮发现的匹配

                        using (Graphics g = Graphics.FromImage(sourceImage))
                        {
                            foreach (TemplateMatch m in matchings)
                            {
                                //g.DrawRectangle(new Pen(replaceColor,1),m.Rectangle);
                                g.FillRectangle(brush, m.Rectangle);
                                //Drawing.Rectangle(data, m.Rectangle, Color.White);
                            }
                        }

                        // sourceImage.UnlockBits(data);

                    }
                    sourceImage.Save(outputPath + '\\' + path.Split('\\')[path.Split('\\').Length - 1]);
                    progressBar.Value++;
                }
                MessageBox.Show("处理完成!!!");
                progressBar.Value = 0;
            }
        }
        private bool CheckConditions()
        {
            string errorText = "";
            errorText = "No files selected";
            if (filePaths != null && filePaths.Count != 0)
            {
                errorText = "No template selected";
                if (templateList.Items.Count != 0)
                {
                    errorText = "No color selected";
                    if (replaceColor.Name != "0")
                    {
                        errorText = "No path selected";
                        if (outputPath != null)
                        {
                            return true;
                        }
                    }
                }
            }
            MessageBox.Show(errorText, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            errorLabel.Text = errorText;
            return false;
        }
        private void pathButton_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    outputPath = dialog.SelectedPath;
                    pathLabel.Text = outputPath;
                }
                catch
                {
                    MessageBox.Show("Something went wrong", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void templateButton_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "Select template image";
            fileDialog.Filter = "Image files|*.png;*.jpg";

            if (fileDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    templateImage.Image = System.Drawing.Image.FromFile(fileDialog.FileName);
                    templateList.Items.Clear();
                    templatePaths = new List<string>(fileDialog.FileNames);
                    foreach (var item in templatePaths)
                    {
                        //ListViewItem path = new ListViewItem(item.Split('\\')[item.Split('\\').Length-1]);
                        ListViewItem path = new ListViewItem(item);
                        templateList.Items.Add(path);
                    }
                }
                catch
                {
                    MessageBox.Show("Something went wrong", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void colorButton_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            if (colorDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    replaceColor = colorDialog.Color;
                    colorPanel.BackColor = replaceColor;
                }
                catch
                {
                    MessageBox.Show("Something went wrong", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        private void trackBar_Scroll(object sender, EventArgs e)
        {
            mult = trackBar.Value / 100f;
            percentLabel.Text = trackBar.Value.ToString() + '%';
        }

        private void fileList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (fileList.SelectedItems.Count > 0)
                fileImage.Image = System.Drawing.Image.FromFile(fileList.SelectedItems[0].Text);
        }

        private void templateList_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (templateList.SelectedItems.Count > 0)
                templateImage.Image = System.Drawing.Image.FromFile(templateList.SelectedItems[0].Text);
        }
    }
}

结果如图:

在这里插入图片描述在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用AForge.NET设置图像的增益可以通过以下步骤实现: 1. 首先,确保你已经安装了AForge.NET库,并在项目中引用了该库。 2. 创建一个Bitmap对象,用于加载和处理图像。例如: ```csharp Bitmap image = new Bitmap("image.jpg"); ``` 3. 创建一个ColorBalance对象,用于设置图像的增益。例如: ```csharp ColorBalance colorBalance = new ColorBalance(); ``` 4. 使用ColorBalance对象的属性来设置图像的增益。ColorBalance类提供了三个属性:Red, Green和Blue,分别表示红色、绿色和蓝色通道的增益。这些属性的值可以在-100到100之间进行调整,负值表示减少增益,正值表示增加增益。例如: ```csharp colorBalance.Red = 10; // 增加红色通道的增益 colorBalance.Green = -5; // 减少绿色通道的增益 colorBalance.Blue = 0; // 不改变蓝色通道的增益 ``` 5. 使用ColorBalance对象对图像进行增益调整。可以通过调用ApplyInPlace方法来直接修改原始图像,也可以创建一个新的Bitmap对象来保存修改后的图像。例如: ```csharp colorBalance.ApplyInPlace(image); // 直接修改原始图像 // 或者创建一个新的Bitmap对象保存修改后的图像 Bitmap processedImage = colorBalance.Apply(image); ``` 6. 最后,可以将修改后的图像保存到文件或者显示在界面上。例如: ```csharp processedImage.Save("processed_image.jpg"); ``` 以上就是使用AForge.NET设置图像的增益的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值