Convert a byte arry to OpenCV image in C++ Convert a byte arry to OpenCV image in C++ arrays arrays

Convert a byte arry to OpenCV image in C++


the previously mentioned method will work fine, if it's PIXEL data.

if instead, you have a whole jpg FILE in memory, headers, compression, and all, it won't work.

in that case you want:

Mat img = imdecode(data);

which will do the same as imread(), only from memory, not from a filename


as @bob mention, this can be done using the Mat constructor.

byte *data;cv::Mat imageWithData = cv::Mat(sizeOfData, 1, CV_8U, data).clone();

After you have created this matrix, call the reshape function with the number of rows in the image.

cv::Mat reshapedImage = imageWithData.reshape(0, numberOfRows);


cv::Mat GetImageFromMemory(uchar* image, int length, int flag)    {        std::vector<uchar> data = std::vector<uchar>(image, image + length);        cv::Mat ImMat = imdecode(data, flag);        return ImMat;    }

the parameter "flag" refers to the type of image, grayscale, color, any depht .. that has opencv defined