0

I am trying to copy the data of a openCV mat object (its of type uchar*) into an unsigned char* using copy function of c++ as follows:

cv::Mat m = cv::imread (path, 0);

uchar * ptr;

std::copy (m.data, ptr, mask.size);

However im getting 26 syntax errors about the copy function. Can you help me with this? Help is appreciated. Thank you.

dramaticlook
  • 653
  • 1
  • 12
  • 39
  • Are you trying to copy the entire mat data to a single pointer? And, what do you think `mask.size` would give you? have you checked? –  Mar 14 '16 at 15:14
  • You messed up a lot of things here. 1) uninitialized pointer 2) Wrong arguments in `std::copy`. Did you meant to use `memcpy` instead? 3) What's `mask`? If you're going to work with non continuous images you should really do in another way. So, what are you trying to do? And why do you need a `uchar*`? And why do you need to copy the data into a `uchar`? – Miki Mar 14 '16 at 15:26
  • Derman and Miki mask was supposed to be "m". İ allocated memory of m.total() instead. m.size returns a size object with width and height. So u were right when u ppinted out usage of copy. I used memcpy instead so everything is fine right now. Thank you! – dramaticlook Mar 15 '16 at 08:11

1 Answers1

2

Simply the answer is:

// cv::Mat m; // your opencv Mat

uchar* mPtr = new uchar[m.total()];    
std::memcpy(mPtr, m.data, m.total());

It was a straightforward question, sorry. though hope it may help someone too.

sercanturkmen
  • 127
  • 2
  • 10
dramaticlook
  • 653
  • 1
  • 12
  • 39
  • 1
    Warning: `m.total()` is only correct if your matrix is of type CV_8UC1, i.e. a single 8bit (uchar) channel! Otherwise, you'll need https://stackoverflow.com/questions/26441072/finding-the-size-in-bytes-of-cvmat – iliis Aug 11 '20 at 10:47