I wrote the following code:
int _tmain(int argc, _TCHAR* argv[])
{
int vals[]={1,2,3,4,5,6,7,8,9};
CvMat mat = cvMat(3,3,CV_8UC1,vals);
for(int i=0;i<mat.rows;i++)
{
int* ptr = (int*) (mat.data.ptr + i* mat.step);
for(int j=0;j<mat.cols;j++)
{
printf("%d\t",*ptr++);
}
printf("\n");
}
return 0;
}
The output I got is:
1 2 3
512 768 1024
196608 262144 327680
The matrix is not initialized properly. The pointer ptr points to the beginning of each row and incrementing it gives the element in corresponding column. Is my assumption correct? Is there any mistake with the cvMat constructor used or the access method of elements?
Why dont you use the
cv::Matclass?It has some handy functions which handle memory better.
for declaring and initializing
cv::Matin your case the code will look like this: