OpenCV Python (NumPy) to C++ CV::Mat conversion guide for NDArrays and DNN

submited by
Style Pass
2023-09-18 00:00:07

W hen working with OpenCV in Python and C++ there are some key differences. For starters OpenCV Python is seemlessly integrated with NumPy. This makes working with images much easier if you are familliar with common NumPy operations. However when using the C++ API things can get more complicated. However for common 2D operations, the C++ class CV::Mat seems to have lots of common operations to manipulate 2D color images. In that case CV::Mat represents can represent a 2D, 3D, or 4D array which can represent Gray, RGB (BGR in OpenCV), or BGR + Alpha. However many people are not aware that CV::Mat can act as an ND Array where dimensions > 4, in the same way as NumPy. This is very usefull when using the Deep Neural Network module in OpenCV (DNN). Where the inputs and outputs can be ND Arrays, while it is easy to manipulate ND Arrays in NumPy and there is plenty of documentation. Unfortunately that’s not the case of cv::Mat ND Arrays capabilities. So I decided to create this guide to make it easier for other developers to work with cv::Mat. This took me lots of time parsing documentation, reading StackOverFlow threads, and looking in the OpenCV C++ sourcecode. So I hope this can help others to save time.

I hope that this will be able to help someone, there is plenty of documentation for the cv::Mat object in the 2D and 3D form. But ther isn’t much when it is used as an NDArray which is often the type of arrays you deel with when working with Deep Neural Networks and other AI models.

Leave a Comment
Related Posts