WinForm(C#) 人脸修复

public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            this.WindowState = FormWindowState.Maximized;
        }
        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        Bitmap originalImage;
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            SelectImage();
        }
        private void SelectImage()
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;
            if (pictureBox1.Image != null)
                pictureBox1.Image.Dispose();
            image_path = ofd.FileName;
            originalImage = new Bitmap(image_path);
            pictureBox1.Invalidate();
            ofd.Dispose();

            if (target != null)
                target.Dispose();
            target = null;
        }

        int modelSize = 512;
        string model_path;
        SessionOptions options;
        InferenceSession onnx_session;
        Tensor<float> input_tensor;
        List<NamedOnnxValue> input_container;
        private void FrmMain_Load(object sender, EventArgs e)
        {
            toolStripButton3.SelectedIndex = 2;
            rect = new Rectangle((pictureBox1.Width - 514)>>1, (pictureBox1.Height - 514) >> 1, 514, 514);
            // 创建输出会话,用于输出模型读取信息
            options = new SessionOptions();
            options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
            //设置为CPU上运行
            options.AppendExecutionProvider_CPU(0);

        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(image_path))
            {
                MessageBox.Show("请加载图片");
                return;
            }
            if (target==null || pictureBox2.Image==null)
            {
                MessageBox.Show("请移动红框选择区域");
                return;
            }

            if (target.Height != modelSize && target.Width != modelSize)
            {
                MessageBox.Show($"请选择图像大小为{modelSize}×{modelSize}");
                return;
            }
            WaitAction(() =>
            {
               

                if (pictureBox2.Image != null)
                    pictureBox2.Image.Dispose();
                pictureBox2.Image = new Bitmap(repaired_image.ToMemoryStream());
                repaired_image.Dispose();
                image.Dispose();
            });
        }

        private void toolStripButton3_SelectedIndexChanged(object sender, EventArgs e)
        {
            var startupPath = Application.StartupPath;

            model_path = startupPath + $"\\model\\{toolStripButton3.SelectedItem.ToString()}.onnx";
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            SelectImage();
            
        }

        private Rectangle rect;
        private bool dragging = false;
        private System.Drawing.Point dragStart = System.Drawing.Point.Empty;
        private System.Drawing.Point downStart= System.Drawing.Point.Empty;
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (rect.Contains(e.Location))
            {
                dragging = true;
                dragStart = new System.Drawing.Point(e.X - rect.X, e.Y - rect.Y);
            }
            else
            {
                downStart = e.Location;
            }
        }
        int width = 512;
        int height = 512;
        
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                rect.X = e.X - dragStart.X;
                rect.Y = e.Y - dragStart.Y;
                pictureBox1.Invalidate();
            }
            else
            {
                m_ptCanvasX =e.Location.X - downStart.X;
                m_ptCanvasY =  e.Location.Y - downStart.Y;
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (dragging)
            {
                CreateBmp();
            }
            dragging = false;
            downStart= System.Drawing.Point.Empty;
        }
        private void CreateBmp()
        {
            if (originalImage == null) return;
            var bmp = pictureBox1.Handle.CaptureControl(pictureBox1.Width, pictureBox1.Height);
            if (bmp == null) return;

            if (target != null)
                target.Dispose();
            target = new Bitmap(rect.Width - 2, rect.Height - 2);
            using (Graphics graphics = Graphics.FromImage(target))
            {
                graphics.DrawImage(bmp, new Rectangle(0, 0, target.Width, target.Height),
                                    new Rectangle(rect.X + 2, rect.Y + 2, rect.Width - 2, rect.Height - 2), GraphicsUnit.Pixel);
            }
            // 将截取的图像显示在另一个PictureBox中
            if (pictureBox2.Image != null)
                pictureBox2.Image.Dispose();
            pictureBox2.Image = (Bitmap)target.Clone();
            bmp.Dispose();
        }
        Bitmap target = null;
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (originalImage != null)
                Test111(e, originalImage);
            
            e.Graphics.DrawRectangle(Pens.Red, rect);
        }

        private float scaleFactor = 1.0f; // 缩放因子
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            scaleFactor += 0.1f;
            pictureBox1.Invalidate();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            scaleFactor -= 0.1f;
            if (scaleFactor < 0.0f) scaleFactor = 0.0f;
            pictureBox1.Invalidate();
        }
        int x=0, y=0;
        int m_ptCanvasX, m_ptCanvasY;
        int dw, dh;

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(image_path) || pictureBox2.Image == null)
            {
                MessageBox.Show("请先修复图片");
                return;
            }
            using (FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog())
            {
                folderBrowserDialog.Description = "请选择一个文件夹";
                DialogResult dialogResult = folderBrowserDialog.ShowDialog();

                if (dialogResult == DialogResult.OK)
                {
                    string selectedFolderPath = folderBrowserDialog.SelectedPath;
                    string path=Path
                        .Combine(selectedFolderPath, DateTime.Now.ToString("yyyyMMddHHmmss")+Path.GetExtension(image_path));
                    pictureBox2.Image.Save(path);
                    MessageBox.Show("保存成功");
                }
            }
        }

        private void Test111(PaintEventArgs e, Bitmap bitmap)
        {
            BufferedGraphicsContext GraphicsContext = BufferedGraphicsManager.Current;
            BufferedGraphics myBuffer = GraphicsContext.Allocate(e.Graphics, e.ClipRectangle);
            Graphics g = myBuffer.Graphics;
            int destWidth = pictureBox1.Width;
            int destHeight = pictureBox1.Height;
            int srcWidth = bitmap.Width;
            int srcHeight = bitmap.Height;
            float scale = scaleFactor;
            int width = (int)(srcWidth * scale);
            int height = (int)(srcHeight * scale);
            dw = width; dh=height;
             x = (destWidth - width) >> 1; 
             y = (destHeight - height) >> 1;
            //g.TranslateTransform(m_ptCanvasX, m_ptCanvasY);
            g.ScaleTransform(scaleFactor, scaleFactor);
            g.DrawImage(bitmap, x, y, width, height);
            myBuffer.Render(e.Graphics);
            g.Dispose();
            myBuffer.Dispose();
        }
        private void WaitAction(Action action)
        {
            using(FrmWait fw=new FrmWait())
            {
                ((Action)(() =>
                {
                    action();
                })).BeginInvoke(new AsyncCallback((ar) => (ar.AsyncState as FrmWait).DialogResult = DialogResult.OK), fw);
                fw.StartPosition = FormStartPosition.CenterScreen;
                fw.ShowDialog();
            }
        }
    }

可根据ROI框手动选择512*512的人脸修复

相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-07 18:56:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-07 18:56:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-06-07 18:56:03       87 阅读
  4. Python语言-面向对象

    2024-06-07 18:56:03       96 阅读

热门阅读

  1. MTK默认开启Modem Log

    2024-06-07 18:56:03       32 阅读
  2. 在Linux环境中通过Docker运行swift

    2024-06-07 18:56:03       32 阅读
  3. 记录使用pytorch训练crnn

    2024-06-07 18:56:03       26 阅读
  4. Docker 教程-介绍-2

    2024-06-07 18:56:03       28 阅读
  5. elementary OS 8的新消息

    2024-06-07 18:56:03       33 阅读
  6. Spark任务调度

    2024-06-07 18:56:03       29 阅读
  7. SparkStreaming编程-DStream创建

    2024-06-07 18:56:03       26 阅读
  8. 【AIGC调研系列】chatTTS与GPT-SoVITS的对比优劣势

    2024-06-07 18:56:03       26 阅读
  9. Advantages of high pressure cleaners

    2024-06-07 18:56:03       30 阅读