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.
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