• C# OpenCvSharp DNN Low Light image Enhancement


    目录

    介绍

    效果

    模型信息

    项目

    代码

    下载


    C# OpenCvSharp DNN Low Light image Enhancement

    介绍

    github地址:https://github.com/zhenqifu/PairLIE

      

    效果

    模型信息

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

    Inputs
    -------------------------
    name:input
    tensor:Float[1, 3, 512, 512]
    name:exposure
    tensor:Float[1]
    ---------------------------------------------------------------

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

    项目

    代码

    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/pairlie_512x512.onnx";
    46. inpHeight = 512;
    47. inpWidth = 512;
    48. opencv_net = CvDnn.ReadNetFromOnnx(modelpath);
    49. image_path = "test_img/1.png";
    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. opencv_net.SetInput(BN_image, "input");
    66. Mat one = new Mat(1,1,MatType.CV_32F,new float[] { 0.5f});
    67. Mat exposure = CvDnn.BlobFromImage(one);
    68. opencv_net.SetInput(exposure, "exposure");
    69. //模型推理,读取推理结果
    70. Mat[] outs = new Mat[1] { new Mat() };
    71. string[] outBlobNames = opencv_net.GetUnconnectedOutLayersNames().ToArray();
    72. dt1 = DateTime.Now;
    73. opencv_net.Forward(outs, outBlobNames);
    74. dt2 = DateTime.Now;
    75. float* pdata = (float*)outs[0].Data;
    76. int out_h = outs[0].Size(2);
    77. int out_w = outs[0].Size(3);
    78. int channel_step = out_h * out_w;
    79. float[] data = new float[channel_step * 3];
    80. for (int i = 0; i < data.Length; i++)
    81. {
    82. data[i] = pdata[i] * 255;
    83. if (data[i] < 0)
    84. {
    85. data[i] = 0;
    86. }
    87. else if (data[i] > 255)
    88. {
    89. data[i] = 255;
    90. }
    91. }
    92. float[] temp_r = new float[out_h * out_w];
    93. float[] temp_g = new float[out_h * out_w];
    94. float[] temp_b = new float[out_h * out_w];
    95. Array.Copy(data, temp_r, out_h * out_w);
    96. Array.Copy(data, out_h * out_w, temp_g, 0, out_h * out_w);
    97. Array.Copy(data, out_h * out_w * 2, temp_b, 0, out_h * out_w);
    98. Mat rmat = new Mat(out_h, out_w, MatType.CV_32F, temp_r);
    99. Mat gmat = new Mat(out_h, out_w, MatType.CV_32F, temp_g);
    100. Mat bmat = new Mat(out_h, out_w, MatType.CV_32F, temp_b);
    101. result_image = new Mat();
    102. Cv2.Merge(new Mat[] { bmat, gmat, rmat }, result_image);
    103. result_image.ConvertTo(result_image, MatType.CV_8UC3);
    104. Cv2.Resize(result_image, result_image, new OpenCvSharp.Size(srcw, srch));
    105. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
    106. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
    107. }
    108. private void pictureBox2_DoubleClick(object sender, EventArgs e)
    109. {
    110. Common.ShowNormalImg(pictureBox2.Image);
    111. }
    112. private void pictureBox1_DoubleClick(object sender, EventArgs e)
    113. {
    114. Common.ShowNormalImg(pictureBox1.Image);
    115. }
    116. }
    117. }

    下载

    源码下载

  • 相关阅读:
    计算机大一新生,想卷却找不到方向,恳请前辈指指路?
    基于安卓android微信小程序的小型企业人力资源管理系统-人事考勤app
    计算机毕业设计Java教评系统(源码+系统+mysql数据库+lw文档)
    初识进程以及父子进程
    Rust基础学习-Rust宏
    Linux - Python安装
    JAVA基础——编译器报告的错误信息总结
    9.30 校招 实习 内推 面经
    python的类属性和实例属性
    opencv识别一张图片的多个红框,并截取红框的内容
  • 原文地址:https://blog.csdn.net/weixin_46771779/article/details/136338426