【发布时间】:2015-08-31 20:48:25
【问题描述】:
我用 C++ 编写了一段代码。我从搜索引擎结果中提取了第一部分。
1) 使用double **filter_2d定义函数是什么意思?可以用指针定义函数吗?
2) 我对以下行感到困惑:
double **filt_out = filter_2d(A, 3, 3, B, 2, 1);
它不能正常工作,我不明白为什么。
#include <iostream>
#include <stddef.h>
#include <cmath>
#include <fftw3.h>
using namespace std;
void filter_2d(double** image, int width_image, int height_image, double** kernel, int width_kernel, int height_kernel, double *** OutImg)
{
double **output = *OutImg;
int i, j, p, q;
//this is the case of 'full' option selected in matlab
//double **output = (double **)malloc(sizeof(double *)*(width_image + width_kernel - 1));
for (i = 0; i<width_image + width_kernel - 1; i++)
{
output[i] = (double *)malloc(sizeof(double)*(height_image + height_kernel - 1));
}
//for each point in the output
for (i = 0; i<width_image + width_kernel - 1; i++)
{
for (j = 0; j<height_image + height_kernel - 1; j++)
{
output[i][j] = 0;
//kernel(p,q)*image(i-p, j-q)
for (p = 0; p<width_kernel; p++)
{
//avoid unnecessary comparisons
if (i - p < 0)
{
break;
}
else if (i - p < width_image)
{
for (q = 0; q<height_kernel; q++)
{
//idem as above
if (j - q < 0)
{
break;
}
else if (j - q < width_image)
{
output[i][j] += kernel[p][q] * image[i - p][j - q];
}
}
}
}
}
}
}
int main()
{
double ** OutImage = 0;
OutImage = (double **)malloc(sizeof(double *)*(3 * 3));
double A[3][3] = { { 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 } };
double *A_ptr[9];
for (int i = 0; i < 10; i++)
{
A_ptr[i] = A[i];
}
double B[1][2] = { 1, 2 };
double *B_ptr[2];
for (int i = 0; i < 2; i++)
{
B_ptr[i] = B[i];
}
//Error in the below line
filter_2d(A_ptr, 3, 3, B_ptr, 2, 1, &OutImage); //unable to understand
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 4; j++)
cout << *OutImage << endl;
}
system("PAUSE");
return 0;
}
【问题讨论】:
-
**是指向指针的指针,或者在您的情况下是指向二维数组的指针。double** filter_2d表示函数filter_2d的返回类型是一个指向二维双精度数组的指针。 -
请提供比第 66 行 更好的方法来找到您的问题行,我绝对不会数行。 :-/
-
欢迎使用 C++。汗桶试图将矩阵建模为
double**。意识到为什么这是一个坏主意(锯齿状边缘,到处分配的内存)。然后将整个批次装箱并使用 BLAS。 www.boost.org. -
@Rohit:第 65 行:双 **filt_out = filter_2d(A, 3, 3, B, 2, 1);
-
详细了解C++ programming,然后使用标准containers,例如std::vector。
标签: c++ pointers multidimensional-array