首页 最新 热门 推荐

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

C# Onnx Yolov8-OBB 旋转目标检测 行驶证副页条码+编号 检测,后续裁剪出图片并摆正显示

  • 25-02-19 03:40
  • 2242
  • 10207
blog.csdn.net

C# Onnx Yolov8-OBB 旋转目标检测 行驶证副页条码+编号 检测,后续裁剪出图片并摆正显示

目录

效果

模型信息

项目

代码 

下载


效果

模型信息

Model Properties
-------------------------
date:2024-06-25T10:59:15.206586
description:Ultralytics YOLOv8n-obb model trained on C:\Work\yolov8\config\dl-obb.yaml
author:Ultralytics
version:8.1.29
task:obb
license:AGPL-3.0 License (https://ultralytics.com/license)
docs:https://docs.ultralytics.com
stride:32
batch:1
imgsz:[1024, 1024]
names:{0: 'code'}
---------------------------------------------------------------

Inputs
-------------------------
name:images
tensor:Float[1, 3, 1024, 1024]
---------------------------------------------------------------

Outputs
-------------------------
name:output0
tensor:Float[1, 6, 21504]
---------------------------------------------------------------

项目

代码 

using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using OpenCvSharp;
using OpenCvSharp.Dnn;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Windows.Forms;

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

        string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
        string image_path = "";
        string classer_path;
        DateTime dt1 = DateTime.Now;
        DateTime dt2 = DateTime.Now;
        string model_path;
        Mat image;
        Mat result_image;
        public string[] class_lables;
        SessionOptions options;
        InferenceSession onnx_session;
        Tensor input_tensor;
        List input_container;
        IDisposableReadOnlyCollection result_infer;
        DisposableNamedOnnxValue[] results_onnxvalue;

        Tensor result_tensors;

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            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;
            pictureBox3.Image = null;
            textBox1.Text = "";
            Application.DoEvents();

            //图片缩放
            image = new Mat(image_path);
            int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
            Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
            Rect roi = new Rect(0, 0, image.Cols, image.Rows);
            image.CopyTo(new Mat(max_image, roi));

            float[] result_array;
            float factor = (float)(max_image_length / 1024.0);

            // 将图片转为RGB通道
            Mat image_rgb = new Mat();
            Cv2.CvtColor(max_image, image_rgb, ColorConversionCodes.BGR2RGB);
            Mat resize_image = new Mat();
            Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(1024, 1024));

           // Cv2.ImShow("resize_image",resize_image);

            // 输入Tensor
            for (int y = 0; y < resize_image.Height; y++)
            {
                for (int x = 0; x < resize_image.Width; x++)
                {
                    input_tensor[0, 0, y, x] = resize_image.At(y, x)[0] / 255f;
                    input_tensor[0, 1, y, x] = resize_image.At(y, x)[1] / 255f;
                    input_tensor[0, 2, y, x] = resize_image.At(y, x)[2] / 255f;
                }
            }

            //将 input_tensor 放入一个输入参数的容器,并指定名称
            input_container.Add(NamedOnnxValue.CreateFromTensor("images", input_tensor));

            dt1 = DateTime.Now;
            //运行 Inference 并获取结果
            result_infer = onnx_session.Run(input_container);
            dt2 = DateTime.Now;

            // 将输出结果转为DisposableNamedOnnxValue数组
            results_onnxvalue = result_infer.ToArray();

            // 读取第一个节点输出并转为Tensor数据
            result_tensors = results_onnxvalue[0].AsTensor();

            result_array = result_tensors.ToArray();

            Mat result_data = new Mat(6, 21504, MatType.CV_32F, result_array);
            result_data = result_data.T();
            string s = result_data.Dump();
            List position_boxes = new List();
            List class_ids = new List();
            List confidences = new List();
            List rotations = new List();
            // Preprocessing output results
            for (int i = 0; i < result_data.Rows; i++)
            {
                Mat classes_scores = new Mat(result_data, new Rect(4, i, 1, 1));
                string s2 = classes_scores.Dump();
                OpenCvSharp.Point max_classId_point, min_classId_point;
                double max_score, min_score;
                // Obtain the maximum value and its position in a set of data
                Cv2.MinMaxLoc(classes_scores, out min_score, out max_score,
                    out min_classId_point, out max_classId_point);
                // Confidence level between 0 ~ 1
                // Obtain identification box information
                if (max_score > 0.5)
                {
                    float cx = result_data.At(i, 0);
                    float cy = result_data.At(i, 1);
                    float ow = result_data.At(i, 2);
                    float oh = result_data.At(i, 3);
                    double x = (cx - 0.5 * ow) * factor;
                    double y = (cy - 0.5 * oh) * factor;
                    double width = ow * factor;
                    double height = oh * factor;
                    Rect2d box = new Rect2d();
                    box.X = x;
                    box.Y = y;
                    box.Width = width;
                    box.Height = height;
                    position_boxes.Add(box);
                    class_ids.Add(max_classId_point.X);
                    confidences.Add((float)max_score);
                    rotations.Add(result_data.At(i, 5));
                }
            }

            // NMS 
            int[] indexes = new int[position_boxes.Count];
            CvDnn.NMSBoxes(position_boxes, confidences, 0.5f, 0.5f, out indexes);
            List rotated_rects = new List();
            for (int i = 0; i < indexes.Length; i++)
            {
                int index = indexes[i];
                float w = (float)position_boxes[index].Width;
                float h = (float)position_boxes[index].Height;
                float x = (float)position_boxes[index].X + w / 2;
                float y = (float)position_boxes[index].Y + h / 2;
                float r = rotations[index];
                float w_ = w > h ? w : h;
                float h_ = w > h ? h : w;
                r = (float)((w > h ? r : (float)(r + Math.PI / 2)) % Math.PI);
                RotatedRect rotate = new RotatedRect(new Point2f(x, y), new Size2f(w_, h_), (float)(r * 180.0 / Math.PI));

                if (rotate.Angle>90)
                {
                    rotate.Angle =  rotate.Angle-180;
                }
                rotated_rects.Add(rotate);
            }

            result_image = image.Clone();

            for (int i = 0; i < indexes.Length; i++)
            {
                int index = indexes[i];

                if (confidences[index]<0.7)
                {
                    continue;
                }

                Point2f[] points = rotated_rects[i].Points();

                //裁剪出需要的图片
                Mat codeMat = GetRotateCropImage(image, rotated_rects[i]);
                pictureBox3.Image = new Bitmap(codeMat.ToMemoryStream());

                for (int j = 0; j < 4; j++)
                {
                    Cv2.Line(result_image, (OpenCvSharp.Point)points[j], (OpenCvSharp.Point)points[(j + 1) % 4], new Scalar(0, 255, 0), 2, LineTypes.Link8);
                }

                Cv2.PutText(result_image, class_lables[class_ids[index]] + "-" + confidences[index].ToString("0.00"),
                    (OpenCvSharp.Point)points[0], HersheyFonts.HersheySimplex, 0.8, new Scalar(0, 0, 255), 2);

                pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());

            }

            


            textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";

            button2.Enabled = true;
        }

        private Mat GetRotateCropImage(Mat src, RotatedRect rect)
        {
            bool wider = rect.Size.Width > rect.Size.Height;
            float angle = rect.Angle;
            OpenCvSharp.Size srcSize = src.Size();
            Rect boundingRect = rect.BoundingRect();

            int expTop = Math.Max(0, 0 - boundingRect.Top);
            int expBottom = Math.Max(0, boundingRect.Bottom - srcSize.Height);
            int expLeft = Math.Max(0, 0 - boundingRect.Left);
            int expRight = Math.Max(0, boundingRect.Right - srcSize.Width);

            Rect rectToExp = boundingRect + new OpenCvSharp.Point(expTop, expLeft);
            Rect roiRect = Rect.FromLTRB(
                boundingRect.Left + expLeft,
                boundingRect.Top + expTop,
                boundingRect.Right - expRight,
                boundingRect.Bottom - expBottom);
            Mat boundingMat = src[roiRect];


            Mat expanded = boundingMat.CopyMakeBorder(expTop, expBottom, expLeft, expRight, BorderTypes.Replicate);
            Point2f[] rp = rect.Points()
                .Select(v => new Point2f(v.X - rectToExp.X, v.Y - rectToExp.Y))
                .ToArray();


            Point2f[] srcPoints = new[] { rp[0], rp[3], rp[2], rp[1] };

            if (wider == true && angle >= 0 && angle < 45)
            {
                srcPoints = new[] { rp[1], rp[2], rp[3], rp[0] };
            }

            var ptsDst0 = new Point2f(0, 0);
            var ptsDst1 = new Point2f(rect.Size.Width, 0);
            var ptsDst2 = new Point2f(rect.Size.Width, rect.Size.Height);
            var ptsDst3 = new Point2f(0, rect.Size.Height);

            Mat matrix = Cv2.GetPerspectiveTransform(srcPoints, new[] { ptsDst0, ptsDst1, ptsDst2, ptsDst3 });

            Mat dest = expanded.WarpPerspective(matrix, new OpenCvSharp.Size(rect.Size.Width, rect.Size.Height), InterpolationFlags.Nearest, BorderTypes.Replicate);

            if (rect.Angle<0)
            {
                Cv2.Flip(dest, dest, FlipMode.X);
            }

            boundingMat.Dispose();
            expanded.Dispose();
            matrix.Dispose();

            return dest;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            model_path = "model/best.onnx";
            classer_path = "model/lable.txt";

            // 创建输出会话,用于输出模型读取信息
            options = new SessionOptions();
            options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
            options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行

            // 创建推理模型类,读取本地模型文件
            onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径

            // 输入Tensor
            input_tensor = new DenseTensor(new[] { 1, 3, 1024, 1024 });
            // 创建输入容器
            input_container = new List();

            List str = new List();
            StreamReader sr = new StreamReader(classer_path);
            string line;
            while ((line = sr.ReadLine()) != null)
            {
                str.Add(line);
            }
            class_lables = str.ToArray();

            image_path = "test_img/1.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 (pictureBox3.Image == null)
            {
                return;
            }
            Bitmap output = new Bitmap(pictureBox3.Image);
            sdf.Title = "保存";
            sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp";
            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;
                        }
                }
                MessageBox.Show("保存成功,位置:" + sdf.FileName);
            }
        }
    }
}

  1. using Microsoft.ML.OnnxRuntime;
  2. using Microsoft.ML.OnnxRuntime.Tensors;
  3. using OpenCvSharp;
  4. using OpenCvSharp.Dnn;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Drawing;
  8. using System.Drawing.Imaging;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Windows.Forms;
  12. namespace Onnx_Yolov8_Demo
  13. {
  14. public partial class Form1 : Form
  15. {
  16. public Form1()
  17. {
  18. InitializeComponent();
  19. }
  20. string fileFilter = "*.*|*.bmp;*.jpg;*.jpeg;*.tiff;*.tiff;*.png";
  21. string image_path = "";
  22. string classer_path;
  23. DateTime dt1 = DateTime.Now;
  24. DateTime dt2 = DateTime.Now;
  25. string model_path;
  26. Mat image;
  27. Mat result_image;
  28. public string[] class_lables;
  29. SessionOptions options;
  30. InferenceSession onnx_session;
  31. Tensor<float> input_tensor;
  32. List<NamedOnnxValue> input_container;
  33. IDisposableReadOnlyCollection<DisposableNamedOnnxValue> result_infer;
  34. DisposableNamedOnnxValue[] results_onnxvalue;
  35. Tensor<float> result_tensors;
  36. private void button1_Click(object sender, EventArgs e)
  37. {
  38. OpenFileDialog ofd = new OpenFileDialog();
  39. ofd.Filter = fileFilter;
  40. if (ofd.ShowDialog() != DialogResult.OK) return;
  41. pictureBox1.Image = null;
  42. image_path = ofd.FileName;
  43. pictureBox1.Image = new Bitmap(image_path);
  44. textBox1.Text = "";
  45. image = new Mat(image_path);
  46. pictureBox2.Image = null;
  47. }
  48. private void button2_Click(object sender, EventArgs e)
  49. {
  50. if (image_path == "")
  51. {
  52. return;
  53. }
  54. button2.Enabled = false;
  55. pictureBox2.Image = null;
  56. pictureBox3.Image = null;
  57. textBox1.Text = "";
  58. Application.DoEvents();
  59. //图片缩放
  60. image = new Mat(image_path);
  61. int max_image_length = image.Cols > image.Rows ? image.Cols : image.Rows;
  62. Mat max_image = Mat.Zeros(new OpenCvSharp.Size(max_image_length, max_image_length), MatType.CV_8UC3);
  63. Rect roi = new Rect(0, 0, image.Cols, image.Rows);
  64. image.CopyTo(new Mat(max_image, roi));
  65. float[] result_array;
  66. float factor = (float)(max_image_length / 1024.0);
  67. // 将图片转为RGB通道
  68. Mat image_rgb = new Mat();
  69. Cv2.CvtColor(max_image, image_rgb, ColorConversionCodes.BGR2RGB);
  70. Mat resize_image = new Mat();
  71. Cv2.Resize(image_rgb, resize_image, new OpenCvSharp.Size(1024, 1024));
  72. // Cv2.ImShow("resize_image",resize_image);
  73. // 输入Tensor
  74. for (int y = 0; y < resize_image.Height; y++)
  75. {
  76. for (int x = 0; x < resize_image.Width; x++)
  77. {
  78. input_tensor[0, 0, y, x] = resize_image.At<Vec3b>(y, x)[0] / 255f;
  79. input_tensor[0, 1, y, x] = resize_image.At<Vec3b>(y, x)[1] / 255f;
  80. input_tensor[0, 2, y, x] = resize_image.At<Vec3b>(y, x)[2] / 255f;
  81. }
  82. }
  83. //将 input_tensor 放入一个输入参数的容器,并指定名称
  84. input_container.Add(NamedOnnxValue.CreateFromTensor("images", input_tensor));
  85. dt1 = DateTime.Now;
  86. //运行 Inference 并获取结果
  87. result_infer = onnx_session.Run(input_container);
  88. dt2 = DateTime.Now;
  89. // 将输出结果转为DisposableNamedOnnxValue数组
  90. results_onnxvalue = result_infer.ToArray();
  91. // 读取第一个节点输出并转为Tensor数据
  92. result_tensors = results_onnxvalue[0].AsTensor<float>();
  93. result_array = result_tensors.ToArray();
  94. Mat result_data = new Mat(6, 21504, MatType.CV_32F, result_array);
  95. result_data = result_data.T();
  96. string s = result_data.Dump();
  97. List<Rect2d> position_boxes = new List<Rect2d>();
  98. List<int> class_ids = new List<int>();
  99. List<float> confidences = new List<float>();
  100. List<float> rotations = new List<float>();
  101. // Preprocessing output results
  102. for (int i = 0; i < result_data.Rows; i++)
  103. {
  104. Mat classes_scores = new Mat(result_data, new Rect(4, i, 1, 1));
  105. string s2 = classes_scores.Dump();
  106. OpenCvSharp.Point max_classId_point, min_classId_point;
  107. double max_score, min_score;
  108. // Obtain the maximum value and its position in a set of data
  109. Cv2.MinMaxLoc(classes_scores, out min_score, out max_score,
  110. out min_classId_point, out max_classId_point);
  111. // Confidence level between 0 ~ 1
  112. // Obtain identification box information
  113. if (max_score > 0.5)
  114. {
  115. float cx = result_data.At<float>(i, 0);
  116. float cy = result_data.At<float>(i, 1);
  117. float ow = result_data.At<float>(i, 2);
  118. float oh = result_data.At<float>(i, 3);
  119. double x = (cx - 0.5 * ow) * factor;
  120. double y = (cy - 0.5 * oh) * factor;
  121. double width = ow * factor;
  122. double height = oh * factor;
  123. Rect2d box = new Rect2d();
  124. box.X = x;
  125. box.Y = y;
  126. box.Width = width;
  127. box.Height = height;
  128. position_boxes.Add(box);
  129. class_ids.Add(max_classId_point.X);
  130. confidences.Add((float)max_score);
  131. rotations.Add(result_data.At<float>(i, 5));
  132. }
  133. }
  134. // NMS
  135. int[] indexes = new int[position_boxes.Count];
  136. CvDnn.NMSBoxes(position_boxes, confidences, 0.5f, 0.5f, out indexes);
  137. List<RotatedRect> rotated_rects = new List<RotatedRect>();
  138. for (int i = 0; i < indexes.Length; i++)
  139. {
  140. int index = indexes[i];
  141. float w = (float)position_boxes[index].Width;
  142. float h = (float)position_boxes[index].Height;
  143. float x = (float)position_boxes[index].X + w / 2;
  144. float y = (float)position_boxes[index].Y + h / 2;
  145. float r = rotations[index];
  146. float w_ = w > h ? w : h;
  147. float h_ = w > h ? h : w;
  148. r = (float)((w > h ? r : (float)(r + Math.PI / 2)) % Math.PI);
  149. RotatedRect rotate = new RotatedRect(new Point2f(x, y), new Size2f(w_, h_), (float)(r * 180.0 / Math.PI));
  150. if (rotate.Angle>90)
  151. {
  152. rotate.Angle = rotate.Angle-180;
  153. }
  154. rotated_rects.Add(rotate);
  155. }
  156. result_image = image.Clone();
  157. for (int i = 0; i < indexes.Length; i++)
  158. {
  159. int index = indexes[i];
  160. if (confidences[index]<0.7)
  161. {
  162. continue;
  163. }
  164. Point2f[] points = rotated_rects[i].Points();
  165. //裁剪出需要的图片
  166. Mat codeMat = GetRotateCropImage(image, rotated_rects[i]);
  167. pictureBox3.Image = new Bitmap(codeMat.ToMemoryStream());
  168. for (int j = 0; j < 4; j++)
  169. {
  170. Cv2.Line(result_image, (OpenCvSharp.Point)points[j], (OpenCvSharp.Point)points[(j + 1) % 4], new Scalar(0, 255, 0), 2, LineTypes.Link8);
  171. }
  172. Cv2.PutText(result_image, class_lables[class_ids[index]] + "-" + confidences[index].ToString("0.00"),
  173. (OpenCvSharp.Point)points[0], HersheyFonts.HersheySimplex, 0.8, new Scalar(0, 0, 255), 2);
  174. pictureBox2.Image = new Bitmap(result_image.ToMemoryStream());
  175. }
  176. textBox1.Text = "推理耗时:" + (dt2 - dt1).TotalMilliseconds + "ms";
  177. button2.Enabled = true;
  178. }
  179. private Mat GetRotateCropImage(Mat src, RotatedRect rect)
  180. {
  181. bool wider = rect.Size.Width > rect.Size.Height;
  182. float angle = rect.Angle;
  183. OpenCvSharp.Size srcSize = src.Size();
  184. Rect boundingRect = rect.BoundingRect();
  185. int expTop = Math.Max(0, 0 - boundingRect.Top);
  186. int expBottom = Math.Max(0, boundingRect.Bottom - srcSize.Height);
  187. int expLeft = Math.Max(0, 0 - boundingRect.Left);
  188. int expRight = Math.Max(0, boundingRect.Right - srcSize.Width);
  189. Rect rectToExp = boundingRect + new OpenCvSharp.Point(expTop, expLeft);
  190. Rect roiRect = Rect.FromLTRB(
  191. boundingRect.Left + expLeft,
  192. boundingRect.Top + expTop,
  193. boundingRect.Right - expRight,
  194. boundingRect.Bottom - expBottom);
  195. Mat boundingMat = src[roiRect];
  196. Mat expanded = boundingMat.CopyMakeBorder(expTop, expBottom, expLeft, expRight, BorderTypes.Replicate);
  197. Point2f[] rp = rect.Points()
  198. .Select(v => new Point2f(v.X - rectToExp.X, v.Y - rectToExp.Y))
  199. .ToArray();
  200. Point2f[] srcPoints = new[] { rp[0], rp[3], rp[2], rp[1] };
  201. if (wider == true && angle >= 0 && angle < 45)
  202. {
  203. srcPoints = new[] { rp[1], rp[2], rp[3], rp[0] };
  204. }
  205. var ptsDst0 = new Point2f(0, 0);
  206. var ptsDst1 = new Point2f(rect.Size.Width, 0);
  207. var ptsDst2 = new Point2f(rect.Size.Width, rect.Size.Height);
  208. var ptsDst3 = new Point2f(0, rect.Size.Height);
  209. Mat matrix = Cv2.GetPerspectiveTransform(srcPoints, new[] { ptsDst0, ptsDst1, ptsDst2, ptsDst3 });
  210. Mat dest = expanded.WarpPerspective(matrix, new OpenCvSharp.Size(rect.Size.Width, rect.Size.Height), InterpolationFlags.Nearest, BorderTypes.Replicate);
  211. if (rect.Angle<0)
  212. {
  213. Cv2.Flip(dest, dest, FlipMode.X);
  214. }
  215. boundingMat.Dispose();
  216. expanded.Dispose();
  217. matrix.Dispose();
  218. return dest;
  219. }
  220. private void Form1_Load(object sender, EventArgs e)
  221. {
  222. model_path = "model/best.onnx";
  223. classer_path = "model/lable.txt";
  224. // 创建输出会话,用于输出模型读取信息
  225. options = new SessionOptions();
  226. options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_INFO;
  227. options.AppendExecutionProvider_CPU(0);// 设置为CPU上运行
  228. // 创建推理模型类,读取本地模型文件
  229. onnx_session = new InferenceSession(model_path, options);//model_path 为onnx模型文件的路径
  230. // 输入Tensor
  231. input_tensor = new DenseTensor<float>(new[] { 1, 3, 1024, 1024 });
  232. // 创建输入容器
  233. input_container = new List<NamedOnnxValue>();
  234. List<string> str = new List<string>();
  235. StreamReader sr = new StreamReader(classer_path);
  236. string line;
  237. while ((line = sr.ReadLine()) != null)
  238. {
  239. str.Add(line);
  240. }
  241. class_lables = str.ToArray();
  242. image_path = "test_img/1.jpg";
  243. pictureBox1.Image = new Bitmap(image_path);
  244. image = new Mat(image_path);
  245. }
  246. private void pictureBox1_DoubleClick(object sender, EventArgs e)
  247. {
  248. Common.ShowNormalImg(pictureBox1.Image);
  249. }
  250. private void pictureBox2_DoubleClick(object sender, EventArgs e)
  251. {
  252. Common.ShowNormalImg(pictureBox2.Image);
  253. }
  254. SaveFileDialog sdf = new SaveFileDialog();
  255. private void button3_Click(object sender, EventArgs e)
  256. {
  257. if (pictureBox3.Image == null)
  258. {
  259. return;
  260. }
  261. Bitmap output = new Bitmap(pictureBox3.Image);
  262. sdf.Title = "保存";
  263. sdf.Filter = "Images (*.jpg)|*.jpg|Images (*.png)|*.png|Images (*.bmp)|*.bmp";
  264. if (sdf.ShowDialog() == DialogResult.OK)
  265. {
  266. switch (sdf.FilterIndex)
  267. {
  268. case 1:
  269. {
  270. output.Save(sdf.FileName, ImageFormat.Jpeg);
  271. break;
  272. }
  273. case 2:
  274. {
  275. output.Save(sdf.FileName, ImageFormat.Png);
  276. break;
  277. }
  278. case 3:
  279. {
  280. output.Save(sdf.FileName, ImageFormat.Bmp);
  281. break;
  282. }
  283. }
  284. MessageBox.Show("保存成功,位置:" + sdf.FileName);
  285. }
  286. }
  287. }
  288. }

下载

源码下载

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

/ 登录

评论记录:

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

分类栏目

后端 (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