OpenCV中Mat数据的访问报错
最近再写一段程序的时候,要访问Mat中的元素。在定义Mat型数据的时候,用
Mat ObjectPoints(48,3,CV_32FC1,0)
对其进行初始化后,用at进行访问时报内存错误。
1 Mat ObjectPoints(48,3,CV_32FC1); 2 Mat ImgPoints(48,2,CV_32FC1); 3 Mat CameraMatrix(3,3,CV_32FC1); 4 CvMat *intrinsic_matrix; 5 CvMat *ObjPoint; 6 CvMat *ImgPoint; 7 CvMat rotation_vector; 8 CvMat translation_vector; 9 10 int j,k,n; 11 n = 0; 12 for( j = 0; j < 8; j++ ) 13 { 14 for( k = 0; k < 6; k++ ) 15 {int m=6*j+k; 16 objectPoints[n++] = cv::Point3f(j*30, k*30, 0); 17 ObjectPoints.at<float>(m,0)=objectPoints[n++].x; 18 ObjectPoints.at<float>(m,1)=objectPoints[n++].y; 19 ObjectPoints.at<float>(m,2)=objectPoints[n++].z; 20 } 21 }
在网上搜了下CV_32FC1,表示32位即float型数据,但是我却对其赋了初值0(int 数据)。改为
Mat ObjectPoints(48,3,CV_32FC1,0.0);
Mat ObjectPoints(48,3,CV_32FC1,0.0f);
即可消除错误,错误虽然小,可也耽误了不少时间,以后编程时要更加注意程序的规范性。