马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
下面是C的程序,在matlab里面调用的时候,也不报错,就是直接闪退,我查了网上说可能是数组溢出了,断点调试了,但是也没找到是哪有问题
#include "mex.h"
#include "math.h"
void Recon(double *dims, double *m, double *Pro, double *P)
{
int i, j, k;
int u, v ;
double pu, pv, lamda, x, y, z, dx, dy, dz;
double inter_x, inter_y ,sin_w,cos_w ,s;
double Nx, Ny, Nz, nu , nv ,light_to_center;
Nx = dims[0];
Ny = dims[1];
Nz = dims[2];
dx = dims[3];
dy = dims[4];
dz = dims[5];
nu = dims[6];
nv = dims[7];
sin_w = dims[8];
cos_w = dims[9];
light_to_center = dims[10];
for (k = 1; k < Nz+1; k++)
{
for (i = 1; i < Nx+1; i++)
{
for (j = 1; j < Ny+1; j++)
{
x = (i-0.5)*dx - dx*(Nx / 2 + 0.5);
y = (j-0.5)*dy - dy*(Ny / 2 + 0.5);
z = (k-0.5)*dz - dz*(Nz / 2 + 0.5);
//加权参数
//s = - x * sin(deg2rad(beta)) + y * cos(deg2rad(beta));
s = -x * sin_w + y * cos_w;
pu = m[0] * x + m[1] * y + m[2] * z + m[3];
pv = m[4] * x + m[5] * y + m[6] * z + m[7];
lamda = m[8] * x + m[9] * y + m[10] * z +1;
inter_x = pu /lamda ;
inter_y = pv /lamda ;
u = int(inter_x); // exchange u,v
v = int(inter_y);
if ((inter_x >= 1 && inter_x <= (nu-1 )) && (inter_y >= 1 && inter_y <= (nv-1)))
{
//在投影图中插值获得当前点的像素
P[int(j-1 + Ny*((i-1) + Nx*(k-1)))] = Pro[int( inter_x - 1 + nv* (inter_y - 1))] * (light_to_center * light_to_center)/ ((light_to_center - s )* (light_to_center - s ));
}
else
{
P[int(j-1 + Ny*((i-1) + Nx*(k-1)))] = 0;
}
// if ((u >= 1 && u <= (Ni-1 )) && (v >= 1 && v <= (Nj-1)))
// {
// P[u - 1 + Nj* (v - 1)] = P[u - 1 + Nj* (v - 1)] + invoxel[j-1 + Ny*((i-1) + Nx*(k-1))] * (u - inter_x + 1)*(v - inter_y + 1);
// P[u - 1 + Nj* v] = P[u - 1 + Nj* v] + invoxel[j-1 + Ny*((i-1) + Nx*(k-1))] * (u - inter_x + 1)*(inter_y - v);
// P[u + Nj* (v - 1)] = P[u + Nj* (v - 1)] + invoxel[j-1 + Ny*((i-1) + Nx*(k-1))] * (inter_x - u)*(v - inter_y + 1);
// P[u + Nj* v] = P[u + Nj* v] + invoxel[j-1 + Ny*((i-1) + Nx*(k-1))] * (inter_x - u)*(inter_y - v);
// }
}
}
}
}
//接口
//nlhs 为输出参数的数目,
//plhs是输出参数数组,指针数组
//nrhs是为输入参数的数目
//prhs是输入参数数组吗,是一个指针数组
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
double *dims;
double *m, *Pro;
double *P;
if (nrhs != 3)
mexErrMsgTxt("3 inputs required.");
if (nlhs != 1)
mexErrMsgTxt("One output required.");
dims = mxGetPr(prhs[0]);
m = mxGetPr(prhs[1]);
Pro = mxGetPr(prhs[2]);
/* set the output pointer to the output matrix */
// plhs[0] = mxCreateDoubleMatrix(dims[3],dims[4], mxREAL);
const mwSize ds[3] = {*dims,*(dims +1),*(dims +2)};
plhs[0] = mxCreateNumericArray(3,ds,mxSINGLE_CLASS,mxREAL);
/* create a C pointer to a copy of the output matrix */
P = mxGetPr(plhs[0]); //获得矩阵第一个元素的指针
/* call the C subroutine */
Recon(dims, m, Pro, P);
}
//plhs:指向输出参数的指针
//nrhs:输入参数数目
|