# Increase the maximum amount of channels in cv::Mat

Hi,

In my current work, I need to represent an input image (e.g. 200x200 color) as a matrix of feature vectors extracted from fixed sub-regions of the image. So the final feature representation forms a matrix with Col * Row * Depth dimensional size, where Col and Row are always smaller than the original size(i.e. 200* 200), and the Depth (channel) needs to be 512, 1024, ..., up to 8192. And the feature is already extracted and stored in a text file offline, so all I have to do is just declare a cv::Mat variable and initialize it with values loaded from the text file. But one problem was that maximum amount of channels of cv::Mat is currently defined and limited to 512 by constant CV_CN_MAX, as mentioned in http://answers.opencv.org/question/13601/cv2split-doesnt-work-beyond-512/. So I tried to modify the constant and recompiled the opencv and my programs, and it seems to be able to accommodate higher channel such as 1024, 2048, and so on. But after I increase the constant, I have matrix computation results different from before the constant modification, even though I've used same feature text file and same numerical operation. For example, if the dot product of two feature vectors (row==1, col==1) and (row==2, col==2) was "1.1", then I expected the result must be same for the same feature vectors and dot product operation after the constant modification, but it wasn't. Also the computation time and memory usage have been drastically increased. I guess CV_CN_MAX not only influence on the maximum amount of channels, but also something else in opencv.

So my question is that is there another way to represent a high-depth (e.g. 4096 and 8192) matrix other than modifying CV_CN_MAX? If no, then what else would I need to look at to solve this issue? Sorry for the non-straightforward question :(

edit retag close merge delete

Sort by » oldest newest most voted

The maximum channel count wasn't meant to support high numbers of channels. In fact, it wasn't meant to be changeable. If you change it, that makes your code incompatible with other users of OpenCV. Code that is compiled with one value of CV_CN_MAX cannot be linked to other code compiled with a different value.

For one thing, OpenCV assigns a constructed enum value to each matrix, consisting of a bitfield concatenation of the channel count and numeric type. Code that is compiled with a non-standard value of CV_CN_MAX will trash that enum value and will probably causes crashes and headaches for future maintainers of your code.

I think you will have to create higher-dimensional matrices instead. If you find that such higher-dimensional matrices did not provide all of the conveniences that were available to two-dimensional matrices, you have my sympathy. You might consider integrating with alternative libraries such as Eigen or NT2.

Code:

void Mat::create(int ndims, const int* sizes, int type)


more

you meant int sz[] = {100, 100, 100}; Mat bigCube(3, sz, CV_8U, Scalar::all(0));

( 2014-11-03 18:36:28 -0500 )edit

Official site

GitHub

Wiki

Documentation