Reputation: 73
i need to read dynamic two dimensional array in opencv mat
int main()
{
Mat matrix;
double **theArray;
int numOfRows,numOfCols;
cin >> numOfRows ;
cin >> numOfCols ;
theArray = AllocateDynamicArray<double>(numOfRows,numOfCols);
matrix = Mat(numOfRows,numOfCols,CV_64FC1,&theArray);
string filename = "IN.xml";
FileStorage fs1(filename, FileStorage::WRITE);
fs1.open(filename, FileStorage::WRITE);
fs1 << "locsINMat" << matrix;
fs1 << "descriptorsINMat" << matrix;
fs1.release();
cout << "---------------------" << endl;
FreeDynamicArray(theArray);
}
template <typename T>
T **AllocateDynamicArray( int nRows, int nCols)
{
T **dynamicArray;
dynamicArray = new T*[nRows];
for( int i = 0 ; i < nRows ; i++ )
dynamicArray[i] = new T [nCols];
return dynamicArray;
}
template <typename T>
void FreeDynamicArray(T** dArray)
{
delete [] *dArray;
delete [] dArray;
}
i get this exception: Unhandled exception at 0x5d08f1aa in GP.exe: 0xC0000005: Access violation reading location 0x003f4000.
Upvotes: 1
Views: 3878
Reputation: 17035
float data[2][5] = {{1,2,3,4,5},{7,8,9,10,11}};
A = Mat(2, 5, CV_32FC1, &data);
doesn't throw any error. are your rows and columns correct?
For dynamic memory allocation: I think the memory allocation should be done like this:
double **theArray;
theArray= (double**)malloc(numOfRows* sizeof(double*));
for(i = 0; i < numOfRows; i++)
theArray[i] = (double*)malloc(numOfCols* sizeof(double));
Upvotes: 2