当前位置: 首页 > web >正文

使用ONNX模型实现M-LSD算法

使用ONNX模型实现M-LSD算法的代码如下(使用OpenCvSharp读写图像):

static unsafe void Main(string[] args)
{var OnnxSession = new InferenceSession(@"Models\model_512x512_large.onnx");int TrainImageWidth = 512;int TrainImageHeight = 512;int MapSize = 256;Directory.CreateDirectory("Results");DirectoryInfo directoryInfo = new DirectoryInfo("Images");FileInfo[] fileInfos = directoryInfo.GetFiles();foreach (FileInfo fileInfo in fileInfos){Console.WriteLine(fileInfo.Name);Mat image = new Mat(fileInfo.FullName);Mat resize_image = new Mat();Cv2.Resize(image, resize_image, new Size(TrainImageWidth, TrainImageHeight));float h_ratio = (float)image.Rows / TrainImageHeight;float w_ratio = (float)image.Cols / TrainImageWidth;int row = resize_image.Rows;int col = resize_image.Cols;float[] input_tensor_data = new float[1 * 4 * row * col];int k = 0;for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){for (int c = 0; c < 3; c++){float pix = ((byte*)resize_image.Ptr(i).ToPointer())[j * 3 + c];input_tensor_data[k] = pix;k++;}input_tensor_data[k] = 1;k++;}}var inputTensor = new DenseTensor<float>(input_tensor_data, new[] { 1, TrainImageWidth, TrainImageHeight, 4 });var inputContainer = new List<NamedOnnxValue>{NamedOnnxValue.CreateFromTensor("input_image_with_alpha:0", inputTensor)};var resultInfer = OnnxSession.Run(inputContainer);var resultsOnnxvalue = resultInfer.ToArray();int[] pts = resultsOnnxvalue[0].AsTensor<int>().ToArray();float[] pts_score = resultsOnnxvalue[1].AsTensor<float>().ToArray();float[] vmap = resultsOnnxvalue[2].AsTensor<float>().ToArray();var segments = new List<LineSegmentPoint>();for (int i = 0; i < pts_score.Length; i++){int y = pts[i * 2];int x = pts[i * 2 + 1];float disp_x_start = vmap[0 + y * MapSize * 4 + x * 4];float disp_y_start = vmap[1 + y * MapSize * 4 + x * 4];float disp_x_end = vmap[2 + y * MapSize * 4 + x * 4];float disp_y_end = vmap[3 + y * MapSize * 4 + x * 4];double distance = Math.Sqrt(Math.Pow((disp_x_start - disp_x_end) * 2 * w_ratio, 2) + Math.Pow((disp_y_start - disp_y_end) * 2 * h_ratio, 2));if (pts_score[i] > 0.5 && distance > 15){float x_start = (x + disp_x_start) * 2 * w_ratio;float y_start = (y + disp_y_start) * 2 * h_ratio;float x_end = (x + disp_x_end) * 2 * w_ratio;float y_end = (y + disp_y_end) * 2 * h_ratio;segments.Add(new LineSegmentPoint(new Point(x_start, y_start), new Point(x_end, y_end)));}}Mat result = image.Clone();for (int i = 0; i < segments.Count; i++){var line = segments[i];Cv2.Line(result, line.P1.X, line.P1.Y, line.P2.X, line.P2.Y, new Scalar(0, 0, 255), 2);}result.SaveImage(Path.Combine("Results", fileInfo.Name));}Console.WriteLine("完成");
}

完整Demo:

M-LSD直线检测算法C#实现(包含ONNX模型)资源-CSDN文库

实现效果如下:

http://www.xdnf.cn/news/13135.html

相关文章:

  • Windows11 WSL2 Ubuntu编译安装perf工具
  • 蓝桥杯 冶炼金属
  • 网页版便签应用开发:HTML5本地存储与拖拽交互实践
  • 深度学习:小米 MiMo-VL 技术报告学习
  • https相关
  • 鸿蒙Next仓颉语言开发实战教程:店铺详情页
  • Linux 性能利器:详解 `top` 命令的使用与输出信息解析
  • 【Pandas】pandas DataFrame dropna
  • Linux安装字体
  • Python抽象基类注册机制解析:优雅实现接口兼容的艺术
  • 基于 HTTP 的单向流式通信协议SSE详解
  • Android Framework预装traceroute执行文件到system/bin下
  • JS红宝书10.1-10.5 函数
  • PHP7内核剖析 学习笔记 第十章 扩展开发(1)
  • 【蓝桥杯嵌入式】【复盘】第15届国赛真题
  • 7种分类数据编码技术详解:从原理到实战
  • Java基于BS架构的OA流程可视化实战:从工作流引擎到前端交互(附完整源代码+论文框架)
  • 学校时钟系统,标准考场时钟系统,AI亮相2025高考,赛思时钟系统为教育公平筑起“精准防线”
  • ubuntu22.04有线网络无法连接,图标也没了
  • QT 仿网易云项目
  • React Native 开发环境搭建(全平台详解)
  • LINUX 69 FTP 客服管理系统 man 5 /etc/vsftpd/vsftpd.conf
  • 开始新的认识,对worldquant(50alpha)
  • NLP-文本表征(2011-2022)
  • 中国政务数据安全建设细化及市场需求分析
  • API网关Envoy的鉴权与限流:构建安全可靠的微服务网关
  • C++--string的模拟实现
  • JS红宝书笔记 - 3.3 变量
  • Spring Boot面试题精选汇总
  • 记录:外扩GPIOD访问报警告