First time here? Check out the FAQ!

Ask Your Question
0

the correct use of "step"

asked Sep 3 '13

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;
  }
Preview: (hide)

1 answer

Sort by » oldest newest most voted
0

answered Sep 3 '13

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;
}
Preview: (hide)

Question Tools

Stats

Asked: Sep 3 '13

Seen: 263 times

Last updated: Sep 03 '13