📄 fdprep.c
字号:
#include "mex.h"
#define LOW_BOUND 30
#define HIGH_BOUND 220
#define IN_BOUND_PERCENTAGE 170
/* mexFunction is the gateway routine for the MEX-file. */
void
mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
int i,m,n;
unsigned char *in,*out,*searchpos;
unsigned short *x,*y;
// [sample,y,x] = fdprep(image,searchpos)
if (nlhs!=3)
mexErrMsgTxt("Must have exactly 3 outputs [sample,y,x] = fdprep (...)\n");
if (nrhs!=2)
mexErrMsgTxt("Must have exactly two input2: image and searchpos .\n");
if ((!mxIsUint8(prhs[0]))||(!mxIsUint8(prhs[1])))
mexErrMsgTxt("Both inputs must be of type UINT8.\n");
in= (unsigned char*)mxGetData(prhs[0]);
searchpos = (unsigned char*)mxGetData(prhs[1]);
m=mxGetM(prhs[0]);
n=mxGetN(prhs[0]);
if ((mxGetM(prhs[1])!=m)||(mxGetN(prhs[1])!=n))
mexErrMsgTxt("Both input matrices must be of the same size");
if ((m<20)||(n<20))
return;
out = mxCalloc(360*(m-20+1)*(n-20+1),sizeof(char));
y = mxCalloc((m-20+1)*(n-20+1),sizeof(short));
x = mxCalloc((m-20+1)*(n-20+1),sizeof(short));
{
int size;
unsigned char* pchar;
unsigned short* pshort;
size=DoCalc(in,searchpos,out,x,y,n,m);
plhs[0] = mxCreateNumericMatrix(360,size,mxUINT8_CLASS,mxREAL);
pchar = (unsigned char*)mxGetData(plhs[0]);
for (i=0; i<size*360; i++)
pchar[i]=out[i];
mxFree(out);
plhs[1] = mxCreateNumericMatrix(1,size,mxUINT16_CLASS,mxREAL);
pshort = (unsigned short*)mxGetData(plhs[1]);
for (i=0; i<size; i++)
pshort[i]=y[i];
mxFree(y);
plhs[2] = mxCreateNumericMatrix(1,size,mxUINT16_CLASS,mxREAL);
pshort = (unsigned short*)mxGetData(plhs[2]);
for (i=0; i<size; i++)
pshort[i]=x[i];
mxFree(x);
}
}
int DoCalc(unsigned char *in,
unsigned char *searchpos,
unsigned char *out,
unsigned short *x_array,
unsigned short* y_array,
int w,
int h)
{
unsigned int index360[360];
int i,j,curr,x,y;
double sum,sqsum,var;
int hist[256];
char str[100];
unsigned char* pout=out;
int sample_index=0;
// initializations
curr=0;
for (i=0; i<20; i++)
{
int startj, endj;
if (i<4)
{
startj=4-i;
endj = 19-(4-i);
}
else
if (i>15)
{
startj=4-(19-i);
endj = 19-(4-(19-i));
}
else
{
startj=0;
endj=19;
}
for (j=startj; j<=endj; j++)
{
index360[curr++]=j+i*h;
}
}
// build histogram
for (y=0; y<=h-20;y++)
{
for (x=0; x<=w-20; x++)
{
int offset = y+x*h;
if (searchpos[offset]==0)
continue;
for(i=0; i<256; i++)
hist[i]=0;
sum=0;
sqsum=0;
for (i=0; i<360;i++)
{
pout[i]=in[offset+index360[i]];
hist[pout[i]]++;
sum+= pout[i];
sqsum += pout[i]*pout[i];
}
// If variance is too low, ignore current sample
var = (sqsum - sum*sum/360)/360;
if (var <300)
continue;
//else
// mexPrintf("var = %f\t y=%d, x=%d\n",var,y,x);
// build probability density function
for (i=1; i<256;i++)
hist[i]= hist[i]+hist[i-1];
for (i=0; i<256;i++)
hist[i]= hist[i]*255/360;
// If histogram is too bright or too dark, ignore it
if ((hist[HIGH_BOUND]-hist[LOW_BOUND])<IN_BOUND_PERCENTAGE)
continue;
// apply histogram equalization
for (i=0; i<360; i++)
pout[i]=hist[pout[i]];
x_array[sample_index]=x+1;
y_array[sample_index]=y+1;
sample_index++;
pout+=360;
}
}
return sample_index;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -