Home Create Mat from uint16 pointer
Reply: 0

Create Mat from uint16 pointer

user7459 Published in September 19, 2018, 11:02 am

I am discovering openCV and working on a project in which I have a C# Plugin which call a C++ DLL to do the treatments. C# deals with Mat but my partner send me a pointer to this matrix from C# to C++. So I am trying to use this pointer to create again a matrix because I don't know how to pass a Mat object between two differente langages through a DLL.

For now, my C++ method is :

double CustomData::calculate(LPVOID ptr)
    double sum = 0;
    UINT16* src = (UINT16*)ptr;

    for (int y = 0; y < _height; y++) {
        for (int x = 0; x < _width; x++) {
            sum += src[y*_width + x];
    return sum;

I want to use Mat from openCV now to see if the time of the calculation is faster for a mean, and also because I need to perform more operations on this matrix after. So I need to create a Mat object from the pointer I get. My best try is the next one :

Mat image(_height, _width, CV_16UC1, src);

But I get unsigned char as data of this matrix. I think I am using correctly the constructor so I think the problem is more in the pointer I pass to the function ?

share|improve this question
  • image.data is always unsigned char, but you can use reinterpret_cast<unsigned short>(image.data) if you are using it directly. You may also use the iterators if it is better for you as cv::Mat::begin<unsigned short>() the same for end. Also, you may access the data directly with cv::Mat::at<unsigned short>(row,col) and it will give you the data as unsigned char. – api55 Apr 16 at 9:46
  • Mat image(_height, _wight, CV_16UC1, (uchar*)src); would work. – Quang Hoang Apr 16 at 10:17
  • And if I want to convert my int into double to perform operations after, can I do in other way than using convertTo method ? – Mathieu Gauquelin Apr 17 at 12:30

1 Answer 1

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306526 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO