question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to convert cv::Mat from C++ opencv to Mat for C# opencvsharp?

See original GitHub issue

I want to Convert Mat object from  C++  opencv  to C# opencvsharp.but I don’t how to do .

C++/opencv-------Mat------------>DLL------->C#/opencvsharp---------->Mat.

At first,I Wrote one DLL for C++ as following:

//C++ DLL -----unmanaged

TSO.h

extern "C"
{
	__declspec(dllexport)  Mat WINAPI GetImage(char* filename);//The Mat object is C++ object from opencv
}

TSO.cpp

__declspec(dllexport)  Mat WINAPI GetImage(char* filename)
{
	return imread(filename);
}

then I quote this dll in C# ,As the same time I quote opencvsharp

[DllImport("TSO.dll", EntryPoint = "GetImage")]
 public extern static Mat GetImage(string filename);//Mat is from opencvsharp

finally ,I did like this

Mat img=GetImage(@"D:\1.jpg");//C#object form opencvsharp
            pictureBoxIpl1.ImageIpl = img;//pictureBoxIpl1  is control of opencvsharp

but it doesn’t work,So how to convert?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:2
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
shimatcommented, Sep 9, 2020

🤔 If your Mat’s Type is 8UC1 or 8UC3, encoding it into an image such as BMP or PNG is one way to do it. If it’s any other type, then copying the pixel values might be a good way.

__declspec(dllexport) std::vector<uchar>* __cdecl vector_uchar_new()
{
    return std::vector<uchar>;
}
__declspec(dllexport) void __cdecl vector_uchar_delete(std::vector<uchar>* vec)
{
    delete vec;
}
__declspec(dllexport) void __cdecl vector_uchar_getPointerAndLength(std::vector<uchar>* vec, uchar** ptr, int* length)
{
    *ptr = &vec[0];
    *length = static_cast<int>(vec->size());
}

__declspec(dllexport) void __cdecl SomeFunction(char* filename, std::vector<uchar> *outVec)
{
    cv::Mat m = cv::imread(filename);
    cv::imencode(m, ".png", *outVec);
}
[DllImport("MyNative.dll", CallingConvention = CallingConvention.Cdecl)]
public extern static IntPtr vector_uchar_new();
[DllImport("MyNative.dll", CallingConvention = CallingConvention.Cdecl)]
public extern static void vector_uchar_delete(IntPtr vec);
[DllImport("MyNative.dll", CallingConvention = CallingConvention.Cdecl)]
public extern static void vector_uchar_getPointerAndLength(IntPtr vec, out IntPtr ptr, out int length);

[DllImport("MyNative.dll", CallingConvention = CallingConvention.Cdecl)]
public extern static void SomeFunction(string fileName, IntPtr outVec);
IntPtr vec = vector_uchar_new();
try
{
    SomeFunction("image.jpg", vec);
    vector_uchar_getPointerAndLength(vec, out IntPtr ptr, out int length);

    byte[] pngImageBytes = new byte[length];
    Marshal.Copy(ptr, pngImageBytes , 0, length);

    using var mat = OpenCvSharp.Mat.FromImageData(pngImageBytes);
}
finally
{
    vector_uchar_delete(vec);
}

However, if you are using C++ in the first place, I would suggest that you don’t use OpenCvSharp and write all image processing consistently in C++.

3reactions
tlhcelikcommented, Dec 9, 2019

it doesn’t work 👎

Read more comments on GitHub >

github_iconTop Results From Across the Web

how to convert Mat.at<cv::Vec3b>() from C++ opencv ...
I want to convert C++ OpenCV Code to C# OpenCVSharp. But i don't know how to replace this code cv::Mat mat; mat.at<cv::vec3b>(int ...
Read more >
cv::Mat Class Reference
Converts an array to another data type with optional scaling. More... void, copySize (const Mat &m). internal use function; properly re-allocates _size, ...
Read more >
Mat Class
Performs an inverse Discrete Fourier transform of 1D or 2D floating-point array. Public method Static member, ImDecode. Creates the Mat instance from image...
Read more >
OpenCV: Operations with images
A conversion from Mat to C API data structures (C++ only):. Mat img = imread("image.jpg");. IplImage img1 = cvIplImage(img);. CvMat m = cvMat...
Read more >
From cv::Mat to saving image in a given format(png,jpeg...)
Hi,. I am trying to work out how to save an image in a given format (PGM, PNG, JPEG,... ) once I have...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found