Ask Your Question

Revision history [back]

The 'efficient' way of scanning an image is to iterate over rows, as described here:

void ScanImageAndDoYourStuff(Mat& I, const uchar* const table)
{
    // accept only char type matrices
    CV_Assert(I.depth() == CV_8U);

    int channels = I.channels();

    int nRows = I.rows;
    int nCols = I.cols; // assuming channels==1 in binary image;

    int i,j;
    uchar* p;
    for( i = 0; i < nRows; ++i)
    {
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            // do your stuff
        }
    }
}

If you need to scan columns, just transpose your image (effcient way of rotating) and then your columns will become rows. Does it make sense?

PS. As described in the document mentioned above, using .at<t>(u,v) is not efficient -especially for scanning whole image.

The 'efficient' way of scanning an image is to iterate over rows, as described here:

void ScanImageAndDoYourStuff(Mat& I, const uchar* const table)
{
    // accept only char type matrices
    CV_Assert(I.depth() CV_Assert(I.type() == CV_8U);
CV_8UC1);

    int channels = I.channels();

    int nRows = I.rows;
    int nCols = I.cols; // assuming channels==1 in binary image;

    int i,j;
    uchar* p;
    for( i = 0; i < nRows; ++i)
    {
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            // do your stuff
        }
    }
}

If you need to scan columns, just transpose your image (effcient way of rotating) and then your columns will become rows. Does it make sense?

PS. As described in the document mentioned above, using .at<t>(u,v) is not efficient -especially for scanning whole image.

The 'efficient' way of scanning an image is to iterate over rows, as described here:

void ScanImageAndDoYourStuff(Mat& I, const uchar* const table)
{
    // accept only 1-channel char type matrices
    CV_Assert(I.type() == CV_8UC1);

    int channels = I.channels();

    int nRows = I.rows;
    int nCols = I.cols; // assuming channels==1 in binary image;
I.cols;

    int i,j;
    uchar* p;
    for( i = 0; i < nRows; ++i)
    {
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            // do your stuff
            // p[j] is now same as I.at<uchar>(i,j), but accessing it sequentially is muuuuch faster
        }
    }
}

If you need to scan columns, just transpose your image (effcient way of rotating) and then your columns will become rows. Does it make sense?

PS. As described in the document mentioned above, using .at<t>(u,v) cv::Mat::at<T>(u,v) is not efficient -especially for scanning whole image.

The 'efficient' way of scanning an image is to iterate over rows, as described here:

void ScanImageAndDoYourStuff(Mat& I, const uchar* const table)
I)
{
    // accept only 1-channel char type matrices
    CV_Assert(I.type() == CV_8UC1);

    int channels = I.channels();

    int nRows = I.rows;
    int nCols = I.cols;

    int i,j;
    uchar* p;
    for( i = 0; i < nRows; ++i)
    {
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            // do your stuff
            // p[j] is now same as I.at<uchar>(i,j), but accessing it sequentially is muuuuch faster
        }
    }
}

If you need to scan columns, just transpose your image (effcient way of rotating) and then your columns will become rows. Does it make sense?

PS. As described in the document mentioned above, using cv::Mat::at<T>(u,v) is not efficient -especially for scanning whole image.

The 'efficient' way of scanning an image is to iterate over rows, as described here:

void ScanImageAndDoYourStuff(Mat& I)
{
    // accept only 1-channel char type matrices
    CV_Assert(I.type() == CV_8UC1);

    int channels = I.channels();

    int nRows = I.rows;
    int nCols = I.cols;

    int i,j;
    uchar* p;
    for( i = 0; i < nRows; ++i)
    {
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            // do your stuff
            // p[j] is now same as I.at<uchar>(i,j), but accessing it sequentially is muuuuch faster
        }
    }
}

If you need to scan columns, just transpose your image (effcient way of rotating) and then your columns will become rows. Does it make sense?

PS. As described in the document mentioned above, using cv::Mat::at<T>(u,v) is not efficient -especially for scanning whole image.

The 'efficient' way of scanning an image is to iterate over rows, as described here:

void ScanImageAndDoYourStuff(Mat& I)
{
    // accept only 1-channel char type matrices
    CV_Assert(I.type() == CV_8UC1);

    int nRows = I.rows;
    int nCols = I.cols;

    int i,j;
    uchar* p;
    for( i = 0; i < nRows; ++i)
i+=10)
    {
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            // do your stuff
stuff, eg.
            if (p[j]==0) cout << "0 binary=" << endl;
            else cout << "1 binary=" << endl;
            // p[j] is now same as I.at<uchar>(i,j), but accessing it sequentially is muuuuch faster
        }
    }
}

If you need to scan columns, just transpose your image (effcient way of rotating) and then your columns will become rows. Does it make sense?

PS. As described in the document mentioned above, using cv::Mat::at<T>(u,v) is not efficient -especially for scanning whole image.

The 'efficient' way of scanning an image is to iterate over rows, as described here:

void ScanImageAndDoYourStuff(Mat& I)
{
    // accept only 1-channel char type matrices
    CV_Assert(I.type() == CV_8UC1);

    int nRows = I.rows;
    int nCols = I.cols;

    int i,j;
    uchar* p;
    for( i = 0; i < nRows; i+=10)
    {
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            // do your stuff, eg.
            if (p[j]==0) cout << "0 binary=" << endl;
            else cout << "1 binary=" << endl;
            // p[j] is now same as I.at<uchar>(i,j), but accessing it sequentially is muuuuch faster
        }
    }
}

If you need to scan columns, just transpose ([cv::transpose](http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#transpose)) your image (effcient way of rotating) rotating in your case) and then your columns will become rows. Does it make sense?

PS. As described in the document mentioned above, using cv::Mat::at<T>(u,v) is not efficient -especially for scanning whole image.

The 'efficient' way of scanning an image is to iterate over rows, as described here:

void ScanImageAndDoYourStuff(Mat& I)
{
    // accept only 1-channel char type matrices
    CV_Assert(I.type() == CV_8UC1);

    int nRows = I.rows;
    int nCols = I.cols;

    int i,j;
    uchar* p;
    for( i = 0; i < nRows; i+=10)
    {
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            // do your stuff, eg.
            if (p[j]==0) cout << "0 binary=" << endl;
            else cout << "1 binary=" << endl;
            // p[j] is now same as I.at<uchar>(i,j), but accessing it sequentially is muuuuch faster
        }
    }
}

If you need to scan columns, just transpose ([cv::transpose](http://docs.opencv.org/2.4/modules/core/doc/operations_on_arrays.html#transpose)cv::transpose) your image (effcient way of rotating in your case) and then your columns will become rows. Does it make sense?

PS. As described in the document mentioned above, using cv::Mat::at<T>(u,v) is not efficient -especially for scanning whole image.