C++mat传入C#OpencvCSharp的mat

C++使用opencv的mat图像,将mat传入OpencvCSharp的几种方法。

mat直接传入方法:

C++部分程序

cpp 复制代码
extern "C" __declspec(dllexport) void MatToMat(cv::Mat& img)
{
    img = cv::imread("E:\\OnnxTransEngine\\bus.jpg");
   
}

C#部分程序

cs 复制代码
[DllImport("Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
extern static public void MatToMat(IntPtr mat);
cs 复制代码
Mat img = new Mat();
Class1.MatToMat(img.CvPtr);

Cv2.ImShow("img", img);

接下里介绍rgb图片和单通道图片传入方法,主要是将图片数据放入数组内,再传出。

RGB图片传入方法:

C++部分程序

cpp 复制代码
extern "C" __declspec(dllexport) void MatToMatRgb(int& width, int& height, uchar* array)
{
    cv::Mat matTemp = cv::imread("E:\\OnnxTransEngine\\bus.jpg");

    //依次将rgb数据放入uchar数组中	
    for (int i = 0; i < matTemp.rows; i++) {
        for (int j = 0; j < matTemp.cols; j++) {
            for (int k = 0; k < 3; k++) {
                array[i * matTemp.cols * 3 + j * 3 + k] = matTemp.at<cv::Vec3b>(i, j)[k];
            }
        }
    }

    //将图片的宽高传出
    width = matTemp.cols;
    height = matTemp.rows;

}

C#部分程序

cs 复制代码
[DllImport("Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
extern static public void MatToMatRgb(ref int width, ref int height, ref byte array);
cs 复制代码
int width = 0;
int height = 0;
byte[] datas = new byte[810 * 1080 * 3];    //根据图片实际尺寸来确定


Class1.MatToMatMonon(ref width, ref height, ref datas[0]);

Mat img = Mat.FromPixelData(height, width, MatType.CV_8UC3, datas);

Cv2.ImShow("img", img);

单图片传入方法:

C++部分程序

cpp 复制代码
extern "C" __declspec(dllexport) void MatToMatMonon(int& width, int& height, uchar* array)
{
    cv::Mat matTemp = cv::imread("E:\\OnnxTransEngine\\bus.jpg");
    cv::cvtColor(matTemp, matTemp, cv::COLOR_BGR2GRAY);

    //依次将rgb数据放入uchar数组中	
    for (int i = 0; i < matTemp.rows; i++) {
        for (int j = 0; j < matTemp.cols; j++) {
            array[i * matTemp.cols + j] = matTemp.at<uchar>(i, j);
        }
    }

    //将图片的宽高传出
    width = matTemp.cols;
    height = matTemp.rows;

}

C#部分程序

cs 复制代码
[DllImport("Dll1.dll", CallingConvention = CallingConvention.Cdecl)]
extern static public void MatToMatMonon(ref int width, ref int height, ref byte array);
cs 复制代码
int width = 0;
int height = 0;
byte[] datas = new byte[810 * 1080 * 1];    //根据图片实际尺寸来确定


Class1.MatToMatMonon(ref width, ref height, ref datas[0]);

Mat img = Mat.FromPixelData(height, width, MatType.CV_8UC1, datas);

Cv2.ImShow("img", img);
相关推荐
晴天的雨.9922 小时前
【C++算法】和为s的两个数
开发语言·数据结构·c++·算法
IvanCodes7 小时前
Python 数据处理(十三):JSON、CSV 与数据序列化
开发语言·python
aramae9 小时前
MySQL复合查询(8)
java·c语言·开发语言·后端·算法
淡海水10 小时前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
asdzx6711 小时前
如何使用 C# 提取 PPT 文本与图片
c#·ppt·提取文本
qq_25183645711 小时前
springboot vue3 开发实现 拼豆管理系统
java·开发语言·ai编程
郑州光合科技余经理12 小时前
同城外卖小程序开发:下单成功后,后台导出能不能对上用户端状态
开发语言·前端·git·后端·uni-app·php·ai编程
知识分享小能手13 小时前
C++ 学习教程,从入门到精通,C++ 入门知识 — 完整知识点(1)
开发语言·c++·学习
qq_1998868713 小时前
第8板块·第2节:统一内存的高级特性与性能调优
c++·人工智能·gpu算力·cuda
+VX:Fegn089513 小时前
计算机毕业设计|基于springboot + vue旅游管理系统(源码+数据库+文档)
java·开发语言·vue.js·spring boot·课程设计