我如何使用 QT 在 C++ 中删除浮点二维数组
How i delete float 2D array in c++, with QT
您好,我在工作中使用 QT,但我无法从内存中删除二维浮点数组。
我正在处理图像,所以我需要删除数组以免占用太多内存。
我试过这种方法但不起作用:
int n = test.cols; // number of colums image.
int m = test.rows; // number of lines image
float soma[test.cols][test.rows]; // create a array 2D for operations...
for(int i = 0 ; i < n + 2 ; ++i)
{
for(int j = 0 ; j < m + 2 ; ++j) delete[] soma[i][j] ;
delete[] soma[i];
}
delete[] soma;
在这种特殊情况下,数组位于堆栈或内存的数据部分,而不是堆。 delete[] 运算符只能删除 HEAP 中用 new[] 分配的内存。
不是这个例子在整个内存中创建了一系列不连续行。
如果你这样分配内存。
float ** soma = new float* [test.rows];
// WARNING UNITIALIZED CONTENT
for(int i = 0 ; i < test.rows; ++i) soma[i] = new float[test.cols];
你可以这样删除记忆
for(int i = 0 ; i < test.rows; ++i) delete [] soma[i];
delete [] soma;
然而,分配单个连续图像通常更好(如果尺寸不是太大)。然后使用第二个数组将行偏移记录为指针。
// WARNING UNITIALIZED CONTENT
float * buffer = new float [ test.rows * test.cols ]
float ** soma = new float* [ test.rows ];
for(int i = 0 ; i < m; ++i) soma[i] = soma + i * test.cols;
然后像这样删除
delete [] soma;
delete [] buffer;
或者直接使用std::vector.
谢谢,我使用 std::vector,我正在处理图像,静态分配不是一个好方法,所以我使用 std::vector,对于这项工作,感谢您的关注,,立即关注我的代码:
vector <float> lines(test.rows);
vector<vector<float> > colums(test.cols,lines);
for(int i=0;i<colums.size(); i++) {
for (int j=0;j<colums[i].size(); j++){
colums[i][j] = ((float)imagem.at<Vec3b>(j,i)[0]/(float)(imagem.at<Vec3b>(j,i)[0] + (float)imagem.at<Vec3b>(j,i) [1] + (float)imagem.at<Vec3b>(j,i) [2]))*255;
aux = (int) floor(colums[i][j] + 0.5);
colums[i][j] = aux;
test.at<Vec3b>(j, i)[0] = aux;
aux = 0;
colums[i][j] = ((float)imagem.at<Vec3b>(j,i)[1]/
(float)(imagem.at<Vec3b>(j,i)[0] +
(float)imagem.at<Vec3b>(j,i) [1] +
(float)imagem.at<Vec3b>(j,i) [2]))*255;
aux = (int) floor(colums[i][j] + 0.5);
colums[i][j] = aux;
test.at<Vec3b>(j, i)[1] = aux;
aux = 0;
colums[i][j] = ((float)imagem.at<Vec3b>(j,i)[2]/
(float)(imagem.at<Vec3b>(j,i)[0] +
(float)imagem.at<Vec3b>(j,i) [1] +
(float)imagem.at<Vec3b>(j,i) [2]))*255;
aux = (int) floor(colums[i][j] + 0.5);
colums[i][j] = aux;
test.at<Vec3b>(j, i)[2] = aux;
aux = 0;
}
}
您好,我在工作中使用 QT,但我无法从内存中删除二维浮点数组。
我正在处理图像,所以我需要删除数组以免占用太多内存。
我试过这种方法但不起作用:
int n = test.cols; // number of colums image.
int m = test.rows; // number of lines image
float soma[test.cols][test.rows]; // create a array 2D for operations...
for(int i = 0 ; i < n + 2 ; ++i)
{
for(int j = 0 ; j < m + 2 ; ++j) delete[] soma[i][j] ;
delete[] soma[i];
}
delete[] soma;
在这种特殊情况下,数组位于堆栈或内存的数据部分,而不是堆。 delete[] 运算符只能删除 HEAP 中用 new[] 分配的内存。
不是这个例子在整个内存中创建了一系列不连续行。
如果你这样分配内存。
float ** soma = new float* [test.rows];
// WARNING UNITIALIZED CONTENT
for(int i = 0 ; i < test.rows; ++i) soma[i] = new float[test.cols];
你可以这样删除记忆
for(int i = 0 ; i < test.rows; ++i) delete [] soma[i];
delete [] soma;
然而,分配单个连续图像通常更好(如果尺寸不是太大)。然后使用第二个数组将行偏移记录为指针。
// WARNING UNITIALIZED CONTENT
float * buffer = new float [ test.rows * test.cols ]
float ** soma = new float* [ test.rows ];
for(int i = 0 ; i < m; ++i) soma[i] = soma + i * test.cols;
然后像这样删除
delete [] soma;
delete [] buffer;
或者直接使用std::vector.
谢谢,我使用 std::vector,我正在处理图像,静态分配不是一个好方法,所以我使用 std::vector,对于这项工作,感谢您的关注,,立即关注我的代码:
vector <float> lines(test.rows);
vector<vector<float> > colums(test.cols,lines);
for(int i=0;i<colums.size(); i++) {
for (int j=0;j<colums[i].size(); j++){
colums[i][j] = ((float)imagem.at<Vec3b>(j,i)[0]/(float)(imagem.at<Vec3b>(j,i)[0] + (float)imagem.at<Vec3b>(j,i) [1] + (float)imagem.at<Vec3b>(j,i) [2]))*255;
aux = (int) floor(colums[i][j] + 0.5);
colums[i][j] = aux;
test.at<Vec3b>(j, i)[0] = aux;
aux = 0;
colums[i][j] = ((float)imagem.at<Vec3b>(j,i)[1]/
(float)(imagem.at<Vec3b>(j,i)[0] +
(float)imagem.at<Vec3b>(j,i) [1] +
(float)imagem.at<Vec3b>(j,i) [2]))*255;
aux = (int) floor(colums[i][j] + 0.5);
colums[i][j] = aux;
test.at<Vec3b>(j, i)[1] = aux;
aux = 0;
colums[i][j] = ((float)imagem.at<Vec3b>(j,i)[2]/
(float)(imagem.at<Vec3b>(j,i)[0] +
(float)imagem.at<Vec3b>(j,i) [1] +
(float)imagem.at<Vec3b>(j,i) [2]))*255;
aux = (int) floor(colums[i][j] + 0.5);
colums[i][j] = aux;
test.at<Vec3b>(j, i)[2] = aux;
aux = 0;
}
}