In OpenCV I have cv::Mat object.
It is one channel with the format CV_8UC1 = unsigned char.
It is continuous, which means the data are stored in one place and rows by rows.
I know the numbers of rows and columns of the cv::Mat object.
I have 2D std::vector of size vector< vector > vec1(rows, vector(img.cols));
How could I assign the data of cv::Mat object to the std::vector without copying.
For copying it is easy to create a for-loop or maybe use vector::assign with an iterator.
The storage of the data is the same between cv::Mat and 2D-std::vector. Both have an internal pointer to the data (vector::data and Mat::ptr) but vector::data can't be set to the value of Mat::ptr.
Currently, I have the code with copying the data:
cv::Mat img = cv::imread("test.tif");
cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
vector< vector<double> > vec1(img.rows, vector<double>(img.cols));
for(int i=0; i < img.rows; ++i)
for(int j=0; j < img.cols; ++j)
vec1.at(i).at(j) = img.at<double>(i, j);
Thanx.
vector
. Maybespan
? – Mikicv::Mat
class ispublic
, which means you can access it directly throughimg.data
. However, you can't manipulate the pointer to the data instd::vector
, so I'm afraid it's not possible to do what you want. – Eron