Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

access violation while filling a Mat with custom uchar* data

I want to test the base64 encoding/decoding about to make an Android application communicate with native Opencv c++ code.

I try to show the result image about to verify the result data are correct. The code I used looks like this:

cv::Mat img = imread(imgPath);
string imgData = reinterpret_cast<char*>(img.data);
string str = base64_encode(reinterpret_cast<const unsigned char*>(imgData.c_str()), imgData.length()).c_str();
string strOutput = base64_decode(str);
uchar* data = (uchar*)reinterpret_cast<const unsigned char*>(strOutput.c_str());

I have compared the result data and the input image's data using:

int cpr = strcmp((char*)img.data, (char*)data);

and the result (0) confirms that the data uchar* is the same as the input image's data

When I try to copy the input image's data into the output image, everything works:

memcpy(imgOutput.data, img.data, img.rows * img.cols * img.channels() * sizeof(uchar));

But when I try to copy the result data into the output image, I get an access violation error

memcpy(imgOutput.data, data, img.rows * img.cols * img.channels() * sizeof(uchar));

I wonder why the i cannot fill the output image with the "data" uchar* whereas it is strictly identic with the "img.data" uchar*.

Thanks in advance.

access violation while filling a Mat with custom uchar* data

I want to test the base64 encoding/decoding about to make an Android application communicate with native Opencv c++ code.

I try to show the result image about to verify the result data are check weather the result string is correct. The code I used looks like this:

cv::Mat img = imread(imgPath);
string imgData = reinterpret_cast<char*>(img.data);
string str = base64_encode(reinterpret_cast<const unsigned char*>(imgData.c_str()), imgData.length()).c_str();
string strOutput = base64_decode(str);
uchar* data = (uchar*)reinterpret_cast<const unsigned char*>(strOutput.c_str());

I have compared the result data and the input image's data using:

int cpr = strcmp((char*)img.data, (char*)data);

and the result (0) confirms that the data uchar* is the same as the input image's data

When I try to copy the input image's data into the output image, everything works:

memcpy(imgOutput.data, img.data, img.rows * img.cols * img.channels() * sizeof(uchar));

But when I try to copy the result data into the output image, I get an access violation error

memcpy(imgOutput.data, data, img.rows * img.cols * img.channels() * sizeof(uchar));

I wonder why the i cannot fill the output image with the "data" uchar* whereas it is strictly identic with the "img.data" uchar*.

Thanks in advance.