首页 最新 热门 推荐

  • 首页
  • 最新
  • 热门
  • 推荐

C# CvDnn部署CoupledTPS实现旋转图像矫正

  • 25-02-19 03:41
  • 3427
  • 11581
blog.csdn.net

C# CvDnn部署CoupledTPS实现旋转图像矫正

目录

说明

效果

模型信息

项目

代码

下载


说明

TPAMI2024 - Semi-Supervised Coupled Thin-Plate Spline Model for Rotation Correction and Beyond

github地址:https://github.com/nie-lang/CoupledTPS

代码实现参考:https://github.com/hpc203/CoupledTPS-opencv-dnn

效果

模型信息

feature_extractor.onnx

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

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

Outputs
-------------------------
name:feature
tensor:Float[1, 256, 24, 32]
---------------------------------------------------------------

regressnet.onnx

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

Inputs
-------------------------
name:feature
tensor:Float[1, 256, 24, 32]
---------------------------------------------------------------

Outputs
-------------------------
name:mesh_motion
tensor:Float[1, 7, 9, 2]
---------------------------------------------------------------

项目

代码

Form1.cs

using OpenCvSharp;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

namespace Onnx_Demo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        Mat image;

        CoupledTPS_RotationNet rotationNet;
        int iter_num = 3;

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.InitialDirectory =Application.StartupPath+"\\test_img\\";
            ofd.Filter = fileFilter;
            
            if (ofd.ShowDialog() != DialogResult.OK) return;
            pictureBox1.Image = null;
            image_path = ofd.FileName;
            pictureBox1.Image = new Bitmap(image_path);
            textBox1.Text = "";
            image = new Mat(image_path);
            pictureBox2.Image = null;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (image_path == "")
            {
                return;
            }
            button2.Enabled = false;
            pictureBox2.Image = null;
            textBox1.Text = "";
            Application.DoEvents();
            //读图片
            image = new Mat(image_path);
            dt1 = DateTime.Now;
            Mat result_image = rotationNet.detect(image, iter_num);
            dt2 = DateTime.Now;
            Cv2.CvtColor(result_image, result_image, ColorConversionCodes.BGR2RGB);
            pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
            textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
            button2.Enabled = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            rotationNet = new CoupledTPS_RotationNet("model/feature_extractor.onnx", "model/regressnet.onnx");
            image_path = "test_img/00150_-8.4.jpg";
            pictureBox1.Image = new Bitmap(image_path);
            image = new Mat(image_path);
        }

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

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

        SaveFileDialog sdf = new SaveFileDialog();
        private void button3_Click(object sender, EventArgs e)
        {
            if (pictureBox2.Image == null)
            {
                return;
            }
            Bitmap output = new Bitmap(pictureBox2.Image);
            sdf.Title = "保存";
            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
            if (sdf.ShowDialog() == DialogResult.OK)
            {
                switch (sdf.FilterIndex)
                {
                    case 1:
                        {
                            output.Save(sdf.FileName, ImageFormat.Jpeg);
                            break;
                        }
                    case 2:
                        {
                            output.Save(sdf.FileName, ImageFormat.Png);
                            break;
                        }
                    case 3:
                        {
                            output.Save(sdf.FileName, ImageFormat.Bmp);
                            break;
                        }
                    case 4:
                        {
                            output.Save(sdf.FileName, ImageFormat.Emf);
                            break;
                        }
                    case 5:
                        {
                            output.Save(sdf.FileName, ImageFormat.Exif);
                            break;
                        }
                    case 6:
                        {
                            output.Save(sdf.FileName, ImageFormat.Gif);
                            break;
                        }
                    case 7:
                        {
                            output.Save(sdf.FileName, ImageFormat.Icon);
                            break;
                        }

                    case 8:
                        {
                            output.Save(sdf.FileName, ImageFormat.Tiff);
                            break;
                        }
                    case 9:
                        {
                            output.Save(sdf.FileName, ImageFormat.Wmf);
                            break;
                        }
                }
                MessageBox.Show("保存成功,位置:" + sdf.FileName);
            }
        }
    }
}

  1. using OpenCvSharp;
  2. using System;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Windows.Forms;
  6. namespace Onnx_Demo
  7. {
  8. public partial class Form1 : Form
  9. {
  10. public Form1()
  11. {
  12. InitializeComponent();
  13. }
  14. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  15. string image_path = "";
  16. DateTime dt1 = DateTime.Now;
  17. DateTime dt2 = DateTime.Now;
  18. Mat image;
  19. CoupledTPS_RotationNet rotationNet;
  20. int iter_num = 3;
  21. private void button1_Click(object sender, EventArgs e)
  22. {
  23. OpenFileDialog ofd = new OpenFileDialog();
  24. ofd.InitialDirectory =Application.StartupPath+"\\test_img\\";
  25. ofd.Filter = fileFilter;
  26. if (ofd.ShowDialog() != DialogResult.OK) return;
  27. pictureBox1.Image = null;
  28. image_path = ofd.FileName;
  29. pictureBox1.Image = new Bitmap(image_path);
  30. textBox1.Text = "";
  31. image = new Mat(image_path);
  32. pictureBox2.Image = null;
  33. }
  34. private void button2_Click(object sender, EventArgs e)
  35. {
  36. if (image_path == "")
  37. {
  38. return;
  39. }
  40. button2.Enabled = false;
  41. pictureBox2.Image = null;
  42. textBox1.Text = "";
  43. Application.DoEvents();
  44. //读图片
  45. image = new Mat(image_path);
  46. dt1 = DateTime.Now;
  47. Mat result_image = rotationNet.detect(image, iter_num);
  48. dt2 = DateTime.Now;
  49. Cv2.CvtColor(result_image, result_image, ColorConversionCodes.BGR2RGB);
  50. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
  51. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
  52. button2.Enabled = true;
  53. }
  54. private void Form1_Load(object sender, EventArgs e)
  55. {
  56. rotationNet = new CoupledTPS_RotationNet("model/feature_extractor.onnx", "model/regressnet.onnx");
  57. image_path = "test_img/00150_-8.4.jpg";
  58. pictureBox1.Image = new Bitmap(image_path);
  59. image = new Mat(image_path);
  60. }
  61. private void pictureBox1_DoubleClick(object sender, EventArgs e)
  62. {
  63. Common.ShowNormalImg(pictureBox1.Image);
  64. }
  65. private void pictureBox2_DoubleClick(object sender, EventArgs e)
  66. {
  67. Common.ShowNormalImg(pictureBox2.Image);
  68. }
  69. SaveFileDialog sdf = new SaveFileDialog();
  70. private void button3_Click(object sender, EventArgs e)
  71. {
  72. if (pictureBox2.Image == null)
  73. {
  74. return;
  75. }
  76. Bitmap output = new Bitmap(pictureBox2.Image);
  77. sdf.Title = "保存";
  78. sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp|Images (*.emf)|*.emf|Images (*.exif)|*.exif|Images (*.gif)|*.gif|Images (*.ico)|*.ico|Images (*.tiff)|*.tiff|Images (*.wmf)|*.wmf";
  79. if (sdf.ShowDialog() == DialogResult.OK)
  80. {
  81. switch (sdf.FilterIndex)
  82. {
  83. case 1:
  84. {
  85. output.Save(sdf.FileName, ImageFormat.Jpeg);
  86. break;
  87. }
  88. case 2:
  89. {
  90. output.Save(sdf.FileName, ImageFormat.Png);
  91. break;
  92. }
  93. case 3:
  94. {
  95. output.Save(sdf.FileName, ImageFormat.Bmp);
  96. break;
  97. }
  98. case 4:
  99. {
  100. output.Save(sdf.FileName, ImageFormat.Emf);
  101. break;
  102. }
  103. case 5:
  104. {
  105. output.Save(sdf.FileName, ImageFormat.Exif);
  106. break;
  107. }
  108. case 6:
  109. {
  110. output.Save(sdf.FileName, ImageFormat.Gif);
  111. break;
  112. }
  113. case 7:
  114. {
  115. output.Save(sdf.FileName, ImageFormat.Icon);
  116. break;
  117. }
  118. case 8:
  119. {
  120. output.Save(sdf.FileName, ImageFormat.Tiff);
  121. break;
  122. }
  123. case 9:
  124. {
  125. output.Save(sdf.FileName, ImageFormat.Wmf);
  126. break;
  127. }
  128. }
  129. MessageBox.Show("保存成功,位置:" + sdf.FileName);
  130. }
  131. }
  132. }
  133. }

CoupledTPS_RotationNet.cs

  1. using OpenCvSharp;
  2. using OpenCvSharp.Dnn;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace Onnx_Demo
  6. {
  7. public class CoupledTPS_RotationNet
  8. {
  9. int input_height = 384;
  10. int input_width = 512;
  11. int grid_h = 6;
  12. int grid_w = 8;
  13. Mat grid = new Mat();
  14. Mat W_inv = new Mat();
  15. Net feature_extractor;
  16. Net regressNet;
  17. public CoupledTPS_RotationNet(string modelpatha, string modelpathb)
  18. {
  19. feature_extractor = CvDnn.ReadNet(modelpatha);
  20. regressNet = CvDnn.ReadNet(modelpathb);
  21. tps2flow.get_norm_rigid_mesh_inv_grid(ref grid, ref W_inv, input_height, input_width, grid_h, grid_w);
  22. }
  23. unsafe public Mat detect(Mat srcimg, int iter_num)
  24. {
  25. Mat img = new Mat();
  26. Cv2.Resize(srcimg, img, new Size(input_width, input_height));
  27. img.ConvertTo(img, MatType.CV_32FC3, 1.0 / 127.5d, -1.0d);
  28. Mat input_tensor = CvDnn.BlobFromImage(img);
  29. feature_extractor.SetInput(input_tensor);
  30. Mat[] feature_oris = new Mat[1] { new Mat() };
  31. string[] outBlobNames = feature_extractor.GetUnconnectedOutLayersNames().ToArray();
  32. feature_extractor.Forward(feature_oris, outBlobNames);
  33. Mat feature = feature_oris[0].Clone();
  34. int[] shape = { 1, 2, input_height, input_width };
  35. Mat flow = Mat.Zeros(MatType.CV_32FC1, shape);
  36. List<Mat> flow_list = new List<Mat>();
  37. for (int i = 0; i < iter_num; i++)
  38. {
  39. regressNet.SetInput(feature);
  40. Mat[] mesh_motions = new Mat[1] { new Mat() };
  41. regressNet.Forward(mesh_motions, regressNet.GetUnconnectedOutLayersNames().ToArray());
  42. float* offset = (float*)mesh_motions[0].Data;
  43. Mat tp = new Mat();
  44. tps2flow.get_ori_rigid_mesh_tp(ref tp, offset, input_height, input_width, grid_h, grid_w);
  45. Mat T = W_inv * tp; //_solve_system
  46. T = T.T(); //舍弃batchsize
  47. Mat T_g = T * grid;
  48. Mat delta_flow = new Mat();
  49. tps2flow._transform(T_g, grid, input_height, input_width, ref delta_flow);
  50. if (i == 0)
  51. {
  52. flow += delta_flow;
  53. }
  54. else
  55. {
  56. Mat warped_flow = new Mat();
  57. grid_sample.warp_with_flow(flow, delta_flow, ref warped_flow);
  58. flow = delta_flow + warped_flow;
  59. }
  60. flow_list.Add(flow.Clone());
  61. if (i < (iter_num - 1))
  62. {
  63. int fea_h = feature.Size(2);
  64. int fea_w = feature.Size(3);
  65. float scale_h = (float)fea_h / flow.Size(2);
  66. float scale_w = (float)fea_w / flow.Size(3);
  67. Mat down_flow = new Mat();
  68. upsample.UpSamplingBilinear(flow, ref down_flow, fea_h, fea_w, true, scale_h, scale_w);
  69. for (int h = 0; h < fea_h; h++)
  70. {
  71. for (int w = 0; w < fea_w; w++)
  72. {
  73. float* p_w = (float*)down_flow.Ptr(0, 0, h);
  74. float temp_w = p_w[w];
  75. temp_w = temp_w * scale_w;
  76. p_w[w] = temp_w;
  77. float* p_h = (float*)down_flow.Ptr(0, 1, h);
  78. float temp_h = p_h[w];
  79. temp_h = temp_h * scale_h;
  80. p_h[w] = temp_h;
  81. }
  82. }
  83. feature.Release();
  84. feature = new Mat();
  85. grid_sample.warp_with_flow(feature_oris[0], down_flow, ref feature);
  86. }
  87. }
  88. Mat correction_final = new Mat();
  89. grid_sample.warp_with_flow(input_tensor, flow_list[iter_num - 1], ref correction_final);
  90. Mat correction_img = grid_sample.convert4dtoimage(correction_final);
  91. return correction_img;
  92. }
  93. }
  94. }

下载

源码下载

天天代码码天天
微信公众号
.NET 人工智能实践
注:本文转载自blog.csdn.net的天天代码码天天的文章"https://lw112190.blog.csdn.net/article/details/140230451"。版权归原作者所有,此博客不拥有其著作权,亦不承担相应法律责任。如有侵权,请联系我们删除。
复制链接
复制链接
相关推荐
发表评论
登录后才能发表评论和回复 注册

/ 登录

评论记录:

未查询到任何数据!
回复评论:

分类栏目

后端 (14832) 前端 (14280) 移动开发 (3760) 编程语言 (3851) Java (3904) Python (3298) 人工智能 (10119) AIGC (2810) 大数据 (3499) 数据库 (3945) 数据结构与算法 (3757) 音视频 (2669) 云原生 (3145) 云平台 (2965) 前沿技术 (2993) 开源 (2160) 小程序 (2860) 运维 (2533) 服务器 (2698) 操作系统 (2325) 硬件开发 (2492) 嵌入式 (2955) 微软技术 (2769) 软件工程 (2056) 测试 (2865) 网络空间安全 (2948) 网络与通信 (2797) 用户体验设计 (2592) 学习和成长 (2593) 搜索 (2744) 开发工具 (7108) 游戏 (2829) HarmonyOS (2935) 区块链 (2782) 数学 (3112) 3C硬件 (2759) 资讯 (2909) Android (4709) iOS (1850) 代码人生 (3043) 阅读 (2841)

热门文章

101
推荐
关于我们 隐私政策 免责声明 联系我们
Copyright © 2020-2025 蚁人论坛 (iYenn.com) All Rights Reserved.
Scroll to Top