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

[C#]将opencvsharp的Mat对象转成onnxruntime的inputtensor的3种方法

第一种方法:在创建tensor时候直接赋值改变每个tensor的值,以下是伪代码:

var image = new Mat(image_path);inpWidth = image.Width;inpHeight = image.Height;//将图片转为RGB通道Mat image_rgb = new Mat();Cv2.CvtColor(image, image_rgb, ColorConversionCodes.BGR2RGB);//输入Tensorinput_tensor = new DenseTensor<float>(new[] { 1, 3, inpHeight, inpWidth });for (int y = 0; y < image_rgb.Height; y++){for (int x = 0; x < image_rgb.Width; x++){input_tensor[0, 0, y, x] = image_rgb.At<Vec3b>(y, x)[0] / 255f;input_tensor[0, 1, y, x] = image_rgb.At<Vec3b>(y, x)[1] / 255f;input_tensor[0, 2, y, x] = image_rgb.At<Vec3b>(y, x)[2] / 255f;}}//将 input_tensor 放入一个输入参数的容器,并指定名称input_container.Add(NamedOnnxValue.CreateFromTensor("input", input_tensor));//运行 Inference 并获取结果result_infer = onnx_session.Run(input_container);

第2种通过不安全指针进行赋值以下是伪代码:

            var image = new Mat(image_path);Mat dstimg = new Mat();Cv2.Resize(image, dstimg, new OpenCvSharp.Size(inpWidth, inpHeight));dstimg.ConvertTo(dstimg, MatType.CV_32FC3, 1 / 127.5, -1.0);float* pdata = (float*)dstimg.Data;float[] input_tensor_data = new float[1 * 3 * inpWidth * inpHeight];for (int i = 0; i < 1 * 3 * inpWidth * inpHeight; i++){input_tensor_data[i] = pdata[i];}//输入Tensorinput_tensor = new DenseTensor<float>(input_tensor_data, new[] { 1, inpHeight, inpWidth, 3 });//将 input_tensor 放入一个输入参数的容器,并指定名称input_container.Add(NamedOnnxValue.CreateFromTensor("input_1", input_tensor));//运行 Inference 并获取结果result_infer = onnx_session.Run(input_container);

第3种通过不安全指针进行赋值,本质和第2种方法一样,但是这个是通过图像指针直接赋值以下是伪代码:

示范代码1:

            var image = new Mat(image_path);Mat resize_image = new Mat();Cv2.Resize(image, resize_image, new OpenCvSharp.Size(512, 512));float h_ratio = (float)image.Rows / 512;float w_ratio = (float)image.Cols / 512;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++;}}input_tensor = new DenseTensor<float>(input_tensor_data, new[] { 1, 512, 512, 4 });//将 input_tensor 放入一个输入参数的容器,并指定名称input_container.Add(NamedOnnxValue.CreateFromTensor("input_image_with_alpha:0", input_tensor));//运行 Inference 并获取结果result_infer = onnx_session.Run(input_container);

示范代码2:

            var image = new Mat(image_path);int img_height = image.Rows;int img_width = image.Cols;Mat resize_image = new Mat();Cv2.Resize(image, resize_image, new OpenCvSharp.Size(inpWidth, inpHeight));int row = resize_image.Rows;int col = resize_image.Cols;float[] input_tensor_data = new float[1 * 3 * inpHeight * inpWidth];for (int c = 0; c < 3; c++){for (int i = 0; i < row; i++){for (int j = 0; j < col; j++){float pix = ((byte*)(resize_image.Ptr(i).ToPointer()))[j * 3 + c];input_tensor_data[c * row * col + i * col + j] = (float)((pix / 255.0 - mean[c]) / std[c]);}}}input_tensor = new DenseTensor<float>(input_tensor_data, new[] { 1, 3, inpHeight, inpWidth });//将 input_tensor 放入一个输入参数的容器,并指定名称input_ontainer.Add(NamedOnnxValue.CreateFromTensor("input", input_tensor));//运行 Inference 并获取结果result_infer = onnx_session.Run(input_ontainer);

http://www.lryc.cn/news/429281.html

相关文章:

  • CTF入门教程(非常详细)从零基础入门到竞赛,看这一篇就够了!
  • 数据链路层 I(组帧、差错控制)【★★★★★】
  • 悟空降世 撼动全球
  • Swoole 和 Java 哪个更有优势呢
  • Salesforce 发布开源大模型 xGen-MM
  • 冒 泡 排 序
  • 采用先进的人工智能视觉分析技术,能够精确识别和分析,提供科学、精准的数据支持的智慧物流开源了。
  • IAA游戏APP如何让合理地让用户观看更多广告,提高广告渗透率
  • 环网交换机的特殊作用是什么?
  • mac电脑安装Zsh并启用
  • 【后续更新】python搜集上海二手房数据
  • 创建GPTs,打造你的专属AI聊天机器人
  • 深度学习 vector 之模拟实现 vector (C++)
  • 关于LLC知识10
  • 最长的严格递增或递减子数组
  • 【JavaEE】SpringBoot 统一功能处理:拦截器、统一数据返回与异常处理的综合应用与源码解析
  • I2C学习:上拉电阻选取
  • AC自动机-1
  • 注解@Service@Component@Slf4j@Data
  • 【Nodejs】六、express框架
  • 进阶 pro max
  • Agentic Security:一款针对LLM模型的模糊测试与安全检测工具
  • Spring Cloud Config 与 Spring Cloud Bus 来实现动态配置文件
  • Qt:Qt背景
  • 【数据结构】选择排序
  • 国产GD32单片机开发入门(二)GD32单片机详解
  • 8个我平时每天都会看的网站,涵盖办公、娱乐、学习等
  • Vue2——父子之间间的调用
  • xfs Vs ext4?
  • 数据结构stack (笔记)