C# OpenCvSharp 去水印 图像修复

效果

项目

VS2022+.net4.8+OpenCvSharp4

C# OpenCvSharp 去水印 图像修复_第1张图片代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;

namespace OpenCvSharp_去水印_图像修复
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        Bitmap bmp;
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string imgPath = "";
        public Mat img, img0, inpainted, inpaintMask;
        int thickness = 10;
        string CommandText;
        InpaintMethod inpaintMethod;
        ListItem li;

        private void Form1_Load(object sender, EventArgs e)
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            this.UpdateStyles();

            //添加项:
            cBoxThresholdTypes.Items.Add(new ListItem("NS", InpaintMethod.NS));
            cBoxThresholdTypes.Items.Add(new ListItem("Telea", InpaintMethod.Telea));

            //设置选中项:
            cBoxThresholdTypes.SelectedIndex = 0;    //根据索引
        }

        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            thickness = trackBar2.Value;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            imgPath = ofd.FileName;
            bmp = new Bitmap(imgPath);
            pictureBox1.Image = bmp;
            img0 = Cv2.ImRead(imgPath);
            inpaintMask = new Mat(img0.Size(), MatType.CV_8U, Scalar.Black);
            pictureBox2.Image = BitmapConverter.ToBitmap(inpaintMask);
            pictureBox3.Image = null;
        }

        private void cBoxThresholdTypes_SelectedIndexChanged(object sender, EventArgs e)
        {
            Inpaint();
        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            Inpaint();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            img = img0.Clone();
            inpaintMask = new Mat(img0.Size(), MatType.CV_8U, Scalar.Black);
            inpainted = img0.EmptyClone();

            Window wImage = new Window("image", img);

            OpenCvSharp.Point prevPt = new OpenCvSharp.Point(-1, -1);
            wImage.SetMouseCallback((MouseEventTypes ev, int x, int y, MouseEventFlags flags, IntPtr userdata) =>
            {
                if (ev == MouseEventTypes.LButtonUp || (flags & MouseEventFlags.LButton) == 0)
                {
                    prevPt = new OpenCvSharp.Point(-1, -1);
                }
                else if (ev == MouseEventTypes.LButtonDown)
                {
                    prevPt = new OpenCvSharp.Point(x, y);
                }
                else if (ev == MouseEventTypes.MouseMove && (flags & MouseEventFlags.LButton) != 0)
                {
                    OpenCvSharp.Point pt = new OpenCvSharp.Point(x, y);
                    if (prevPt.X < 0)
                    {
                        prevPt = pt;
                    }
                    inpaintMask.Line(prevPt, pt, Scalar.White, thickness, LineTypes.AntiAlias, 0);
                    pictureBox2.Image = BitmapConverter.ToBitmap(inpaintMask);
                    img.Line(prevPt, pt, Scalar.White, thickness, LineTypes.AntiAlias, 0);
                    prevPt = pt;
                    wImage.ShowImage(img);
                }
            });

            try
            {
                for (; ; )
                {
                    switch ((char)Window.WaitKey(0))
                    {
                        case (char)27:    // exit
                            return;
                        case 'r':
                            inpaintMask.SetTo(Scalar.Black);
                            pictureBox2.Image = BitmapConverter.ToBitmap(inpaintMask);
                            img0.CopyTo(img);
                            wImage.ShowImage(img);
                            break;
                        case 'i':
                        case '\r':
                            Inpaint();
                            break;
                    }
                }
            }
            finally
            {

                Window.DestroyAllWindows();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Inpaint();
        }

        void Inpaint()
        {
            if (pictureBox1.Image == null)
            {
                return;
            }

            li = (ListItem)cBoxThresholdTypes.SelectedItem;
            inpaintMethod = (InpaintMethod)li.Value;

            Cv2.Inpaint(img, inpaintMask, inpainted, trackBar1.Value, inpaintMethod);
            pictureBox3.Image = BitmapConverter.ToBitmap(inpainted);
            CommandText = String.Format("Cv2.Inpaint(src, mask, dst,{0},{1})", trackBar1.Value, inpaintMethod.ToString());
            txtCommandText.Text = CommandText;
        }
    }
}

Demo下载 

你可能感兴趣的:(AI,C#,OpenCV,c#,开发语言,C#去水印,图像修复)