Convert char array to Opencv Mat

Electra picture Electra · May 9, 2016 · Viewed 9.8k times · Source

I have defined an array char buf[], and need to convert it into Opencv Mat. Is there a way?

char buf[Max_size];

Answer

Humam Helfawi picture Humam Helfawi · May 9, 2016

There is nothing that called: "Converting char array to cv::Mat". cv::Mat is a container that represents an image in the memory. It may own the data and may not. If you want to create a cv::Mat to represent an image that its bitmap data in some char array, you may use the following code. This is assuming that you know the rows and the columns of the image.

cv::Mat my_mat(rows,cols,CV_8UC1,&buf[0]); //in case of BGR image use CV_8UC3

Keep in mind that cv::Mat in this case does not hold the ownership of the data. You have delete your data manually. However, since it is a stack array, you do not need to anything.