Technology sharing

C# CvDnn disponit CoupledTPS ad effectum deducendi imaginem corrigendi

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

C# CvDnn disponit CoupledTPS ad efficiendum rotationemimago correctionis

Tabula contentorum

illustrare

Effectus

Exemplar informationes

project

code

download


illustrare

TPAMI2024 - Semi-Supervised tenues-Plate Coupled Spline Model ad gyrationis correctione et ultra

githubinscriptio:https://github.com/nie-lang/CoupledTPS

Codicis exsecutionem referentia:https://github.com/hpc203/CoupledTPS-opencv-dnn

Effectus

Exemplar informationes

feature_extractor.onnx

Exemplar Properties
-------------------------
---------------------------------------------------------------

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

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

regressnet.onnx

Exemplar Properties
-------------------------
---------------------------------------------------------------

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

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

project

code

Form1.cs

usus OpenCvSharp;
Ratio utens;
using System.Drawing;
using System.Drawing.Imaging;
usura System.Windows.Forms;

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

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

CoupledTPS_RotationNet rotationNet;
int iter_num = 3;

privatum inanis button1_Click (mittentes obiectum, EventArgs e)
        {
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory =Application.StartupPath+"\test_img\";
ofd.Filter = fileFilter;
            
si (ofd.ShowDialog() != DialogResult.OK) reditus;
pictureBox1.Image = null;
image_path = ofd.FileName;
pictureBox1.Image = new Bitmap(image_path);
textBox1.Text = "";
image = new Mat(image_path);
pictureBox2.Image = null;
        }

privatum vacui button2_Click (mittentes obiectum, EventArgs e)
        {
si (image_path == "")
            {
remittere;
            }
button2.Enabled = falsum;
pictureBox2.Image = null;
textBox1.Text = "";
Application.DoEvents();
// Read imaginibus
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 = "Consequens temporis consumptio:" + (dt2 - dt1).TotalMilliseconds + "ms";
button2.Enabled = verum;
        }

privatum vacui Form1_Load (mittens obiectum, 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);
        }

privatum inane pictureBox1_DoubleClick (mittente object, EventArgs e)
        {
Common.ShowNormalImg(pictureBox1.Image);
        }

privatum inane pictureBox2_DoubleClick (mittente object, EventArgs e)
        {
Common.ShowNormalImg(pictureBox2.Image);
        }

SaveFileDialog sdf = new SaveFileDialog();
privatum inanis button3_Click (mittentes obiectum, EventArgs e)
        {
si (pictureBox2.Image == null)
            {
remittere;
            }
Bitmap output = new Bitmap(pictureBox2.Image);
sdf.Title = "Save";
sdf.Filter = "Imagines (*.jpg)|*.jpg|Imagines (*.png)|*.png|Imagines (*.bmp)|*.bmp|Imagines (*.emf)|*.emf|Imagines (*.exif)|*.exif|Imagines (*.gif)|*.gif|Imagines (*.ico)|*.ico|Imagines (*.tiff)|*.tiff|Imagines (*.wmf)| *.wmf";
si (sdf.ShowDialog() == DialogResult.OK)
            {
switch (sdf.FilterIndex)
                {
casus I;
                        {
output.Save(sdf.FileName, ImageFormat.Jpeg);
frangere;
                        }
casus II;
                        {
output.Save(sdf.FileName, ImageFormat.Png);
frangere;
                        }
casus III;
                        {
output.Save(sdf.FileName, ImageFormat.Bmp);
frangere;
                        }
casus 4:
                        {
output.Save(sdf.FileName, ImageFormat.Emf);
frangere;
                        }
causam V;
                        {
output.Save(sdf.FileName, ImageFormat.Exif);
frangere;
                        }
casus VI;
                        {
output.Save(sdf.FileName, ImageFormat.Gif);
frangere;
                        }
casus VII;
                        {
output.Save(sdf.FileName, ImageFormat.Icon);
frangere;
                        }

casus VIII;
                        {
output.Save(sdf.FileName, ImageFormat.Tiff);
frangere;
                        }
casus, 9;
                        {
output.Save(sdf.FileName, ImageFormat.Wmf);
frangere;
                        }
                }
MessageBox.Show("Save feliciter, location:" + 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. }

download

Source codice download