0

I have this code:

cv::Mat myImage = imread("Image.png");
char * dataPointer = const_cast<char*>(myImage.data);

but I am getting error:

'const_cast' : cannot convert from 'uchar *const ' to 'char *'  

Why I am getting this error?

Codor
  • 17,447
  • 9
  • 29
  • 56
mans
  • 17,104
  • 45
  • 172
  • 321

1 Answers1

1

Technically, the issue is that a const_cast can only change the CV-qualifiers, and a cast from uchar *const to char * does a bit more than that: it will also convert unsigned char (aliased by OpenCV to uchar) to char, pointer-wise.

I would advise you not to remove the const qualifier. But if you cannot avoid it, converting the pointer to uchar* instead should work for you.

cv::Mat myImage = imread("Image.png");
uchar * dataPointer = const_cast<uchar*>(myImage.data);

If you really want a char*, that can be done too.

char* dp = reinterpret_cast<char*>(dataPointer);
E_net4
  • 27,810
  • 13
  • 101
  • 139