简体   繁体   English

为什么在尝试将opencv图像数据指针转换为char *时出现错误?

[英]Why I am getting error when I try to cast an opencv image data pointer to a char*

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? 为什么我收到此错误?

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. 从技术上讲,问题是const_cast只能更改CV限定词,而从uchar *constchar *作用还不止uchar *const :它将还将unsigned char (由OpenCV别名为uchar )转换为char ,指针-明智的。

I would advise you not to remove the const qualifier. 我建议您不要删除const限定词。 But if you cannot avoid it, converting the pointer to uchar* instead should work for you. 但是,如果您无法避免,则将指针转换为uchar*应该适合您。

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* ,也可以这样做。

char* dp = reinterpret_cast<char*>(dataPointer);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我在尝试编译时遇到此编译错误? - Why am i getting this compile error when i try to compile? 为什么我按照预期会出现图像大小的opencv错误? - Why am I getting an opencv error with size of images when they are as expected? 为什么在将指针分配给双变量时出现错误 - why am i getting an error when assigning pointer to double variable 当我尝试在启用推理引擎的情况下编译OpenCv时,出现错误 - When I try to compile OpenCv with inference engine enabled, I am getting an error 指针+ LinkedList:为什么我收到此错误? - Pointer + LinkedList: Why am I getting this error? 当我尝试使用这个动态数组时,为什么会出现EXC BAD ACCESS错误? - Why am I getting a EXC BAD ACCESS error when I try to impliment this dynamic array? 尝试使用结构时为什么会出现错误? - Why am I getting an error when I try to use my structure? 为什么当我尝试在 c++ 中的 Visual Studio 中将字符串分配给 istringstream 对象时出现错误? - Why am i getting error when i try to assign string to istringstream object in visual studio in c++? 当我尝试重载运算符时,为什么会出现链接错误? - Why am I getting a Link error when I try to overload an operator? 当我尝试通过构造函数分配 char 数组时出现错误 - Getting an error when I try to assign a char array via the constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM