Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conversion between OpenCV matrix and int matrix

I am trying to read video frames with openCV, save data in cv::Mat form. then I want to use my another code which accepts "int type matrix" to this video frame. I know the "cvMat" and "int type matrix" are not compatible. Maybe I am not familiar with opencv, but can I do any kind of conversion between "cvMat" and "int mat", so that I can use my code for the captured video frames? I say "int mat", maybe it is not correct, but what I mean is this:

cv::Mat videoFrame;
int inputData[600][400];

I want to do something like:

inputData = videoFrame;

Thank you.

like image 786
E_learner Avatar asked Sep 26 '12 13:09

E_learner


1 Answers

The fast way is to share the memory: no copy needed. Though, if you modify one matrix, the other will be modified (obvious!)

int inputData[600][400];
cv::Mat videoFrame(600, 400, CV_32UC1, inputData);

Please note that any further OpenCV call on videoFrame that changes its type/size will allocate a new piece of memory where the result will be stored.

If you want to have separate data matrices, you may use memcpy -it's the fastest way to duplicate data:

memcpy((char*)inputData, (char*)videoFrame.data, 600*400*sizeof(int) );

The problem with this approach is that it's not safe at all. What if videoFrame is not of type int? (Most probably it is char). What if it's not continuously stored in memory (may happen)? ...

like image 147
Sam Avatar answered Oct 24 '22 06:10

Sam