Ask Your Question

Amro_'s profile - activity

2020-01-28 09:54:45 -0600 received badge  Notable Question (source)
2018-09-07 22:49:09 -0600 received badge  Popular Question (source)
2017-01-18 05:19:46 -0600 commented answer OpenCV build fails because I cannot download ICV on our build farm

@StevenPuttemans oh I just noticed if(DEFINED ENV{OPENCV_ICV_URL}) so one could simply set an environment variable to change the download URL without modifying the code.

2017-01-18 05:14:58 -0600 commented answer OpenCV build fails because I cannot download ICV on our build farm

@StevenPuttemans: sure. Basically this line would be changed. But since GitHub only serves over HTTPS (HTTP will redirect to HTTPS), I guess you would use a CDN like RawGit or Githack..

2016-10-25 08:56:41 -0600 commented question Missing aruco contrib functionality when building OpenCV 3.1.0

Python wrappers for aruco module were added later in dev version (post 3.1.0).

2016-01-26 09:12:16 -0600 commented answer OpenCV build fails because I cannot download ICV on our build farm

+1 right on, the CMake I was using (installed from a PPA) was built without SSL support. Perhaps downloader.cmake could be changed to download over HTTP rather than HTTPS...

2016-01-25 17:28:33 -0600 received badge  Necromancer (source)
2016-01-25 17:28:33 -0600 received badge  Self-Learner (source)
2016-01-25 16:40:18 -0600 received badge  Scholar (source)
2016-01-25 16:39:53 -0600 answered a question Changing Mat dimensions inplace

Solved; In OpenCV 3.1.0, the Mat::reshape was extended to handle ND-arrays:

2015-06-28 13:26:32 -0600 received badge  Student (source)
2015-06-28 07:38:14 -0600 commented question Changing Mat dimensions inplace

@berak: no problem, thank you for the help :) I noticed you are a regular on the site, do you know any devs on the OpenCV team we can ping on this thread? I'm sure they'll easily have an answer.

2015-06-28 05:27:22 -0600 commented question Changing Mat dimensions inplace


first, there are no real ND arrays: typedef Mat MatND;

I know that MatND is just a typedef for Mat, but Mat itself can be multi-dimensions and/or multi-channels. For example:

int dims[] = {2,3,4,5,6};
cv::Mat mat1(5, dims, CV_64FC(7), Scalar::all(0));

... is a 5-dimensional array with 7-channels.


then, what you do is for sure unsafe and dangerous.

what is the correct way to reshape the ND-array without making a second copy of the data?

2015-06-28 05:25:45 -0600 received badge  Editor (source)
2015-06-28 05:17:40 -0600 received badge  Enthusiast
2015-06-28 01:17:57 -0600 asked a question Changing Mat dimensions inplace

As part of a larger project, I have some code that does something similar to the following:

cv::Mat some_function()
    // get some ND-array
    Mat mat1 = get_some_mat();
    vector<int> dims(mat1.size.p, mat1.size.p + mat1.dims);

    // ... shuffle the dimensions stored in "dims" (the total() remains the same)
    std::swap(dims[0], dims[1]);    // only an example...

    // create new mat header for the same data (no copying)
    Mat mat2(dims.size(),, mat1.type(),;

    // return matrix with the shuffled dimensions
    mat1 = mat2;    // <-- is this part safe??
    return mat1;    //

Is the last assignment safe, or will it mess up the ref count and leak memory?

I know the safer option is to just clone the data as in:

    // return matrix with the shuffled dimensions
    return mat2.clone();

but I'm trying to avoid making unnecessary copies of data...

I'm open to suggestion if there are better ways to reshape the ND-array in-place ...

You can assume the following is true:

CV_Assert(mat1.isContinuous() && mat1.dims > 3 && mat1.channels() == 1);

N.B: I am working with OpenCV 3.0.0


Another thing I tried is to directly overwrite the Mat size and step with those from the shuffled array:

    // return matrix with the shuffled dimensions
    std::copy(mat2.size.p, mat2.size.p + mat2.dims, mat1.size.p);
    std::copy(mat2.step.p, mat2.step.p + mat2.dims, mat1.step.p);
    return mat1;

But it's not quite right.. Apparently something in the mat1.flags field needs to be updated as well! Any help?


I found an overload of the Mat::reshape method for ND-arrays, unfortunately it's not yet fully implemented and currently throws a CV_StsNotImplemented error!

    // return matrix with the shuffled dimensions
    return mat1.reshape(mat2.channels(), mat2.dims, mat2.size.p);

See the source code (either 3.0 or the master branch)...


Let me explain a bit more about my use case. In my actual code, I'm trying to convert the last dimension of any 1-channel ND-array into channels of a ND-array with one less dimension but multi-channels, without copying the data. (Again we can assume that the array is continuous and the destination number of channels is less than CV_CN_MAX).

As an example, If I had the following array:

cv::Mat get_some_mat()
    int dims[] = {2,3,4,5};   // I'm really working with arbitrary number of dimensions
    Mat matnd(4, dims, CV_64F, Scalar::all(0));
    return matnd;

I want to convert it from 2x3x4x5 array with 1-channel into a 2x3x4 array with 5-channels, i.e:

int dims[] = {2,3,4};
Mat mat(3, dims, CV_64FC(5), Scalar::all(0));

And I want to avoid unneeded copies... The above Mat::reshape method would have been great, but it's not implemented at the moment!

The regular Mat::reshape leaves an undesired singleton dimension at the end:

matnd.reshape(5, 0);    // returns 2x3x4x1 with 5-channel (note the "1" at the end)

Thanks and sorry for the long post :)

2015-06-23 05:01:11 -0600 received badge  Supporter (source)