# convert Mat image into 2d Matrix double

Is there any way ,in which I can convert a Mat object into an double[][] array?I have some image proccesing algoritms that I don't know how to use them on Mat objects, but I can use them if I can find a way of transforming , on double[][]'s ! I'm a novice in Opencv! If the Mat's are converted into double[][]'s , and the Mat's are on 3 channels, do I lose some information, do I have to convert the Mat image in gray? What approach can I use?

edit retag close merge delete

You can access to pixel data using method at or Ptr. you can find some examples here.

I think you should think in the other way transform your algorithm to be compatible with opencv Mat.

About double and 3 channels : a real number is not vector.

It's dfficult to answer to your question without knowing your problem

( 2016-01-16 14:46:04 -0500 )edit

Sort by ยป oldest newest most voted

//code for passing 1 channel image pixel values to a 2D array

//declare ,read,convert image to 32float

    Mat img,dst;
img.convertTo(dst, CV_32F);
float arrOut[dst.rows][dst.cols];


//pass data to 2D array

    for(int i = 0 ;i < dst.rows; ++i)
for(int j = 0; j < dst.cols; ++j)
arrOut[i][j]=dst.at<float>(i,j);


//show the array

    for(int i = 0 ;i < dst.rows; ++i)
for(int j = 0; j < dst.cols; ++j)
{  cout<<arrOut[i][j]<<" ";
if(j==(dst.cols-1))
cout<<endl;
}

more

When Mat is continous (bool isContinous = src.isContinous()) you can use a simple pointer. If you have a submatrix of a Mat (src(Rect(x, y, w, h)) normaly a row is continous, so you can set a pointer to the begining of a row.

cv::Mat src; // some input image
cv::Mat dst;

// Convert to double (much faster than a simple for loop)
src.convertTo(dst, CV_64F, 1, 0);

if(dst.isContinous()) {
double *ptrDst = dst.ptr<double>();
for(int i = 0; i < dst.total(); ++i) {
double value = ptrDst[i]; // Or do some other stuff
}
} else
for(int i = 0 i < dst.rows; ++i) {
double *ptrDst = dst.ptr<double>(i);

for(int j = 0; j < dst.cols; ++j) {
double value = ptrDst[j];
}
}
}


Or for 2D matrix access:

cv::Mat src; // some input image
cv::Mat dst;

// Convert to double (much faster than a simple for loop)
src.convertTo(dst, CV_64F, 1, 0);

double *ptrDst[dst.rows];
for(int i = 0 i < dst.rows; ++i) {
ptrDst[i] = dst.ptr<double>(i);

for(int j = 0; j < dst.cols; ++j) {
double value = ptrDst[i][j];
}
}


Edit: You use a 3 channel image, above example is for 1 channel image. So storage order is explained here. The data access is similar to example above with respect to the 3 channel storage.

Edit2: code corrected. Thanks to LBerger.

more

Although this is almost a 4-year old question,it is worth to note that for a 2D matrix access,there is something strange: the ptrDst is declared for a 1D array ,and then it is later used as a referenced 2D array: ptrDst[i][j].

( 2019-09-06 16:12:53 -0500 )edit

Official site

GitHub

Wiki

Documentation

## Stats

Asked: 2016-01-16 14:05:07 -0500

Seen: 8,740 times

Last updated: Sep 06 '19