当前位置:   article > 正文

C# OpenCvSharp DNN Image Retouching

C# OpenCvSharp DNN Image Retouching

目录

介绍

模型

项目

效果

代码

下载


C# OpenCvSharp DNN Image Retouching

介绍

github地址:https://github.com/hejingwenhejingwen/CSRNet

(ECCV 2020) Conditional Sequential Modulation for Efficient Global Image Retouching

模型

Model Properties
-------------------------
---------------------------------------------------------------

Inputs
-------------------------
name:input
tensor:Float[1, 3, 360, 640]
---------------------------------------------------------------

Outputs
-------------------------
name:output
tensor:Float[1, 3, 360, 640]
---------------------------------------------------------------

项目

效果

代码

using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Numerics;
using System.Reflection;
using System.Windows.Forms;

namespace OpenCvSharp_DNN_Demo
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";

        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;

        string modelpath;

        int inpHeight;
        int inpWidth;

        Net opencv_net;
        Mat BN_image;

        Mat image;
        Mat result_image;

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = fileFilter;
            if (ofd.ShowDialog() != DialogResult.OK) return;

            pictureBox1.Image = null;
            pictureBox2.Image = null;
            textBox1.Text = "";

            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            modelpath = "model/csrnet_360x640.onnx";

            inpHeight = 360;
            inpWidth = 640;

            opencv_net = CvDnn.ReadNetFromOnnx(modelpath);

            image_path = "test_img/0014.jpg";
            pictureBox1.Image = new Bitmap(image_path);

        }

        private unsafe void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            textBox1.Text = "检测中,请稍等……";
            pictureBox2.Image = null;
            Application.DoEvents();

            image = new Mat(image_path);

            int srch = image.Rows;
            int srcw = image.Cols;


            BN_image = CvDnn.BlobFromImage(image, 1 / 255.0, new OpenCvSharp.Size(inpWidth, inpHeight), new Scalar(0, 0, 0), true, false);

            //配置图片输入数据
            opencv_net.SetInput(BN_image);

            //模型推理,读取推理结果
            Mat[] outs = new Mat[1] { new Mat() };
            string[] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();

            dt1 = DateTime.Now;

            opencv_net.Forward(outs, outBlobNames);

            dt2 = DateTime.Now;

            float* pdata = (float*)outs[0].Data;
            int out_h = outs[0].Size(2);
            int out_w = outs[0].Size(3);
            int channel_step = out_h * out_w;
            float[] data = new float[channel_step * 3];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = pdata[i] * 255;

                if (data[i] < 0)
                {
                    data[i] = 0;
                }
                else if (data[i] > 255)
                {
                    data[i] = 255;
                }
            }

            float[] temp_r = new float[out_h * out_w];
            float[] temp_g = new float[out_h * out_w];
            float[] temp_b = new float[out_h * out_w];

            Array.Copy(data, temp_r, out_h * out_w);
            Array.Copy(data, out_h * out_w, temp_g, 0, out_h * out_w);
            Array.Copy(data, out_h * out_w * 2, temp_b, 0, out_h * out_w);

            Mat rmat = new Mat(out_h, out_w, MatType.CV_32F, temp_r);
            Mat gmat = new Mat(out_h, out_w, MatType.CV_32F, temp_g);
            Mat bmat = new Mat(out_h, out_w, MatType.CV_32F, temp_b);

            result_image = new Mat();
            Cv2.Merge(new Mat[] { bmat, gmat, rmat }, result_image);

            Cv2.Resize(result_image, result_image, new OpenCvSharp.Size(srcw, srch));

            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
            textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
        }

        private void pictureBox2_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox2.Image);
        }

        private void pictureBox1_DoubleClick(object sender, EventArgs e)
        {
            Common.ShowNormalImg(pictureBox1.Image);
        }
    }
}
 

  1. using OpenCvSharp;
  2. using OpenCvSharp.Dnn;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Drawing;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Linq.Expressions;
  9. using System.Numerics;
  10. using System.Reflection;
  11. using System.Windows.Forms;
  12. namespace OpenCvSharp_DNN_Demo
  13. {
  14. public partial class frmMain : Form
  15. {
  16. public frmMain()
  17. {
  18. InitializeComponent();
  19. }
  20. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  21. string image_path = "";
  22. DateTime dt1 = DateTime.Now;
  23. DateTime dt2 = DateTime.Now;
  24. string modelpath;
  25. int inpHeight;
  26. int inpWidth;
  27. Net opencv_net;
  28. Mat BN_image;
  29. Mat image;
  30. Mat result_image;
  31. private void button1_Click(object sender, EventArgs e)
  32. {
  33. OpenFileDialog ofd = new OpenFileDialog();
  34. ofd.Filter = fileFilter;
  35. if (ofd.ShowDialog() != DialogResult.OK) return;
  36. pictureBox1.Image = null;
  37. pictureBox2.Image = null;
  38. textBox1.Text = "";
  39. image_path = ofd.FileName;
  40. pictureBox1.Image = new Bitmap(image_path);
  41. image = new Mat(image_path);
  42. }
  43. private void Form1_Load(object sender, EventArgs e)
  44. {
  45. modelpath = "model/csrnet_360x640.onnx";
  46. inpHeight = 360;
  47. inpWidth = 640;
  48. opencv_net = CvDnn.ReadNetFromOnnx(modelpath);
  49. image_path = "test_img/0014.jpg";
  50. pictureBox1.Image = new Bitmap(image_path);
  51. }
  52. private unsafe void button2_Click(object sender, EventArgs e)
  53. {
  54. if (image_path == "")
  55. {
  56. return;
  57. }
  58. textBox1.Text = "检测中,请稍等……";
  59. pictureBox2.Image = null;
  60. Application.DoEvents();
  61. image = new Mat(image_path);
  62. int srch = image.Rows;
  63. int srcw = image.Cols;
  64. BN_image = CvDnn.BlobFromImage(image, 1 / 255.0, new OpenCvSharp.Size(inpWidth, inpHeight), new Scalar(0, 0, 0), true, false);
  65. //配置图片输入数据
  66. opencv_net.SetInput(BN_image);
  67. //模型推理,读取推理结果
  68. Mat[] outs = new Mat[1] { new Mat() };
  69. string[] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();
  70. dt1 = DateTime.Now;
  71. opencv_net.Forward(outs, outBlobNames);
  72. dt2 = DateTime.Now;
  73. float* pdata = (float*)outs[0].Data;
  74. int out_h = outs[0].Size(2);
  75. int out_w = outs[0].Size(3);
  76. int channel_step = out_h * out_w;
  77. float[] data = new float[channel_step * 3];
  78. for (int i = 0; i < data.Length; i++)
  79. {
  80. data[i] = pdata[i] * 255;
  81. if (data[i] < 0)
  82. {
  83. data[i] = 0;
  84. }
  85. else if (data[i] > 255)
  86. {
  87. data[i] = 255;
  88. }
  89. }
  90. float[] temp_r = new float[out_h * out_w];
  91. float[] temp_g = new float[out_h * out_w];
  92. float[] temp_b = new float[out_h * out_w];
  93. Array.Copy(data, temp_r, out_h * out_w);
  94. Array.Copy(data, out_h * out_w, temp_g, 0, out_h * out_w);
  95. Array.Copy(data, out_h * out_w * 2, temp_b, 0, out_h * out_w);
  96. Mat rmat = new Mat(out_h, out_w, MatType.CV_32F, temp_r);
  97. Mat gmat = new Mat(out_h, out_w, MatType.CV_32F, temp_g);
  98. Mat bmat = new Mat(out_h, out_w, MatType.CV_32F, temp_b);
  99. result_image = new Mat();
  100. Cv2.Merge(new Mat[] { bmat, gmat, rmat }, result_image);
  101. Cv2.Resize(result_image, result_image, new OpenCvSharp.Size(srcw, srch));
  102. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
  103. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
  104. }
  105. private void pictureBox2_DoubleClick(object sender, EventArgs e)
  106. {
  107. Common.ShowNormalImg(pictureBox2.Image);
  108. }
  109. private void pictureBox1_DoubleClick(object sender, EventArgs e)
  110. {
  111. Common.ShowNormalImg(pictureBox1.Image);
  112. }
  113. }
  114. }

下载

源码下载

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/148326
推荐阅读
相关标签
  

闽ICP备14008679号