how do I draw a gradient image with internal data structure(step) of opencv
I was trying to draw a gradient image with opencv's step But the result was wrong. Could someone give me some hints ? thanks!
#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;
}
thanks for the reply. But there is some mis-understanding, the gradient image I mentioned is some kind of gimp style gradient image. Please kindly check the code above. thanks!
What do you mean by wrong result? I've run your code and have image with horizontal gradient. BTW, instead of "img.data + i*img.step[0]" you can use "img.row(i) .data".