Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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 simple with for loops)
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 * dst.cols);

        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 simple with for loops)
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 * dst.cols);

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

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 loops)
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 * dst.cols);

        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 simple with for loops)
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
    double *ptrDst[dst.rows];
for(int i = 0 i < dst.rows; ++i) {
        double *ptrDst ptrDst[i] = dst.ptr<double>(i * dst.cols);

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

Or for 2D matrix access:

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

// Convert to double (much faster than simple with for loops)
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 * dst.cols);

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

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 loops)
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 * dst.cols);

        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 simple with for loops)
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 * dst.cols);

    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.

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 loops)
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 * dst.cols);

        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 with for loops)
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 * dst.cols);

    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.

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 * dst.cols);
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 * dst.cols);
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.