Conversion between OpenCV matrix and int matrix

I am trying to read open source video frames, save data in cv :: Mat form. then I want to use my other code that accepts an "int matrix" for this video card. I know that "cvMat" and "int type matrix" are incompatible. Maybe I am not familiar with opencv, but can I do some kind of conversion between "cvMat" and "int mat" so that I can use my code for captured video frames? I say "int mat", maybe this is not true, but I mean the following:

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

I want to do something like:

 inputData = videoFrame; 

Thanks.

+6
source share
1 answer

Quick way to share information: no copying required. Although, if you change one matrix, the other will change (obviously!)

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

Please note that any further call to OpenCV on the video frame that changes its type / size will allocate a new part of the memory in which the result will be saved.

If you want to have separate data matrices, you can use memcpy - the fastest way to duplicate data:

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

The problem with this approach is that it is not safe at all. What if videoFrame does not have int type? (Most likely, this is char). What if it is not permanently stored in memory (can happen)? ...

+6
source

Source: https://habr.com/ru/post/926313/


All Articles