获取某一坐标的值
cvGetReal1D, cvGetReal2D, cvGetReal3D, cvGetRealND; 对单通道进行访问
CVAPI(double) cvGetReal3D( const CvArr* arr, int idx0, int idx1, int idx2 );
cvGet1D, cvGet2D, cvGet3D, cvGetND; 对多通道进行访问
CVAPI(CvScalar) cvGet3D( const CvArr* arr, int idx0, int idx1, int idx2 );
多通道:某一坐标的值都由多个数值组成,如颜色图像RGB;如下:
int main()
{
float data[18] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
CvMat mat;
cvInitMatHeader(&mat, 3, 3, CV_32FC2, data);
for(int y = 0; y < mat.rows; y++)
{
for(int x = 0; x < mat.cols; x++)
{
CvScalar value = cvGet2D(&mat, y, x);
printf("(%5.2f, %5.2f) ", value.val[0], value.val[1]);
}
printf("\n");
}
getchar();
return 0;
}
多维数据
CvMatND,维度可以为多维
int main()
{
float data[18] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
int size[] = {2,3,3};
CvMatND mat;
cvInitMatNDHeader(&mat, 3, size, CV_32FC1, data);
for(int z = 0; z < mat.dim[0].size; z++)
{
for(int y = 0; y < mat.dim[1].size; y++)
{
for(int x = 0; x < mat.dim[2].size; x++)
{
int pos[] = {z, y, x};
double value = cvGetRealND(&mat, pos); //double value = cvGetReal3D(&mat, z, y, x);
printf("%5.2f ", value);
}
printf("\n");
}
printf("=========\n");
}
getchar();
return 0;
}
可以用cvGet1D可以访问数据,但是这种方式太慢,因此可以观察数据在内存中的位置而直接访问内存即可;
int main()
{
float data[18] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
CvMat mat;
cvInitMatHeader(&mat, 3, 3, CV_32FC2, data);
for(int y = 0; y < mat.rows; y++)
{
for(int x = 0; x < mat.cols; x++)
{
float * pData = (float*)(mat.data.ptr + mat.step * x + y * sizeof(CV_32FC2));
printf("(%5.2f, %5.2f) ", pData[0], pData[1]);
}
printf("\n");
}
getchar();
return 0;
}
int main()
{
float data[18] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18};
int size[] = {2,3,3};
CvMatND mat;
cvInitMatNDHeader(&mat, 3, size, CV_32FC1, data);
for(int z = 0; z < mat.dim[0].size; z++)
{
for(int y = 0; y < mat.dim[1].size; y++)
{
for(int x = 0; x < mat.dim[2].size; x++)
{
int pos[] = {z, y, x};
float * pValue = (float *)(mat.data.ptr + z * mat.dim[0].step + y * mat.dim[1].step + x * sizeof(CV_32FC1));
printf("%5.2f ", pValue[0]);
}
printf("\n");
}
printf("=========\n");
}
getchar();
return 0;
}