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);
相关推荐
Liekkas Kono9 分钟前
RapidOCR Python 贡献指南
开发语言·python·rapidocr
张张努力变强16 分钟前
C++ STL string 类:常用接口 + auto + 范围 for全攻略,字符串操作效率拉满
开发语言·数据结构·c++·算法·stl
xyq202418 分钟前
Matplotlib 绘图线
开发语言
小镇敲码人20 分钟前
探索CANN框架中TBE仓库:张量加速引擎的优化之道
c++·华为·acl·cann·ops-nn
m0_6948455722 分钟前
tinylisp 是什么?超轻量 Lisp 解释器编译与运行教程
服务器·开发语言·云计算·github·lisp
平安的平安24 分钟前
面向大模型算子开发的高效编程范式PyPTO深度解析
c++·mfc
June`25 分钟前
muduo项目排查错误+测试
linux·c++·github·muduo网络库
春日见27 分钟前
如何创建一个PR
运维·开发语言·windows·git·docker·容器
C++ 老炮儿的技术栈30 分钟前
VS2015 + Qt 实现图形化Hello World(详细步骤)
c语言·开发语言·c++·windows·qt
心疼你的一切35 分钟前
Unity异步编程神器:Unitask库深度解析(功能+实战案例+API全指南)
深度学习·unity·c#·游戏引擎·unitask