initialize an OpenCV Mat with an 2D array

前端 未结 4 1819
悲哀的现实
悲哀的现实 2020-12-03 03:09

In my application, I want to create a OpenCV Mat A (2-Dimensions) having some values and then pass it to another OpenCV function using A as input.

Currently, I\'m t

4条回答
  •  醉酒成梦
    2020-12-03 03:42

    Since data is 2D array - all of data, &data, *data, data[0], &data[0], and &data[0][0] point to base of array. Any of the above representation could be chosen to correctly construct the Mat in place of X in

    A = Mat(2, 5, CV_32FC1, X );
    

    Have peace of mind since data is accepted by OpenCV as void*and data access by OpenCV is as in line. I prefer same syntax for constructing Mat from single or multi dimensional arrays.

    A = Mat(1, 10, CV_32FC1, data ); //for 1D array
    A = Mat(2, 5, CV_32FC1, data ); //for 2D array
    

    Back to the query - Note that, your construction of Mat even from 1D array is incorrect. The step parameter is mentioned as 2. It just happened to work, since OpenCV overrides the step parameter provided, if the number of rows is 1. For higher dimensional arrays, OpenCV throws debug assertion for incorrect step parameter, which you got.

提交回复
热议问题