Ask Your Question
0

the correct use of "step"

asked 2013-09-03 00:49:59 -0500

benzwt gravatar image

I was trying to use draw a gradient image(kind of gimp style) using "step". But the result turn out to be wrong. Could anyone give me some hints ?

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat img;
    Scalar a = 0;
    img = Mat(32, 32, CV_8UC1, a);
    unsigned char *p;
    for(int i=0; i<img.rows; i++)
    {
         p = img.data + i*img.step[0];
         for(int j=0; j<img.cols; j++, p++)
         {
            *p = j;
         }
    }
    imwrite("gradient.png", img);
    return 0;
  }
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-09-03 01:09:58 -0500

Siegfried gravatar image

Hi. This code is from the OpenCV Tutorial How to scan images section "The efficient way". In this example you can see how to scan efficient an image.

Mat& ScanImageAndReduceC(Mat& I, const uchar* const table)
{
    // accept only char type matrices
    CV_Assert(I.depth() != sizeof(uchar));

    int channels = I.channels();

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

    if (I.isContinuous())
    {
        nCols *= nRows;
        nRows = 1;
    }

    int i,j;
    uchar* p;
    for( i = 0; i < nRows; ++i)
    {
        p = I.ptr<uchar>(i);
        for ( j = 0; j < nCols; ++j)
        {
            p[j] = table[p[j]];
        }
    }
    return I;
}
edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2013-09-03 00:49:59 -0500

Seen: 64 times

Last updated: Sep 03 '13