void dynamicArrToMat( int rows, int cols){
/* Dynamic Array declaration and Initialization */
float **fData = NULL;
fData = new float*[4];
for (int i = 0 ; i < 4 ; i++) {
fData[i] = new float[8];
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 8; j++) {
fData[i][j] = 2.0;
/* Mat matrix declaration and Initialization by the element copy from dynamic array */
Mat mat_fData(4,8,CV_32FC1);
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 8; j++) {
mat_fData.at<float>(i,j) = fData[i][j];
下面的这个例子中,静态数组s_data,s_data1,n_data初始化mat_data都没问题.
但动态数组data初始化mat_data后,输出打印Mat数据,数据出现-42000000000000异常。
#include "stdafx.h"
#include "iostream"
#include "stdlib.h"
#include "stdio.h"
#include <cv.h>
#include <highgui.h>
using namespace std;
float s_data[4][8]={{1006,932,1006,1006,859,1006,932,1006},
{1333,1333,1247,1333,1247,1247,1247,1247},
{1840,1840,1840,1778,1778,1778,1716,1778},
{3374,3328,3465,3419,3419,3419,3374,3419}};
float s_data1[32]={1006,932,1006,1006,859,1006,932,1006,
1333,1333,1247,1333,1247,1247,1247,1247,
1840,1840,1840,1778,1778,1778,1716,1778,
3374,3328,3465,3419,3419,3419,3374,3419};
int _tmain(int argc, _TCHAR* argv[])
{
/* dynamic allocate the dynamic array ,and initialed it by NULL */
float **data=NULL;
data=new float*[4];
for (int i=0;i<4;i++) // Initalized the dynamic array;
data[i]=new float[8];
for (int i=0;i<4;i++)
for (int j=0;j<8;j++)
data[i][j]=s_data[i][j];
for (int i=0;i<4;i++)
for (int j=0;j<8;j++)
cout<<data[i][j]<<" ";
cout<<endl;
float n_data[4][8];
for (int i=0;i<4;i++)
for (int j=0;j<8;j++)
n_data[i][j]=data[i][j];
// CvMat *mat_data=cvCreateMat(5,7,CV_32SC1);
// cvInitMatHeader(mat_data,5,7,CV_32SC1,data);
/* The above sentence changed as follow .*/
Mat mat_data = Mat(4,8,CV_32FC1);
for (int i = 0; i < 4; i++) {
for (int j = 0 ; j < 8 ; j++) {
mat_data.at<float>(i,j) = data[i][j];
}
}
CvMat mat_data=cvMat(4,8,CV_32FC1,s_data1);
CvMat mat_data;
cvInitMatHeader(&mat_data,5,7,CV_32FC1,data);
for (int i=0;i<4;i++)
for (int j=0;j<8;j++)
cout<<cvmGet(&mat_data,i,j)<<" ";
cout<<endl;
getchar();
return 0;
跟踪调试 原来mat中*data是一维指针,如果用二维动态数组初始化的话,读取的是地址,而不是其中的数据:
int cols = 300, rows = 200;
float* myarr = new float[rows*cols];
/* 第一步,初始化 CvMat 头*/
/* 这里更改为逐元素赋值复制,Mat声明时其大小可以通过参数传递,动态指定 */
CvMat mat = cvMat( rows, cols,
CV_32FC1, // 32 位浮点单通道类型
myarr // 用户数据指针(数据没有被复制)
);
/* 第二步,使用 cv 函数, 例如计算 l2 (Frobenius) 模 */
double norm = cvNorm( &mat, 0, CV_L2 );
...
delete myarr;