📄 comphisteq.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, comph, compw;
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!=4)
mexErrMsgTxt("Must have exactly 4 inputs: image,searchpos, component height, comp width .\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");
comph = (int)(*mxGetPr(prhs[2]));
compw = (int)(*mxGetPr(prhs[3]));
mexPrintf("w = %d, h = %d\n", compw, comph);
if ((m<comph)||(n<compw))
return;
out = mxCalloc(comph*compw*(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,compw,comph);
plhs[0] = mxCreateNumericMatrix(comph*compw,size,mxUINT8_CLASS,mxREAL);
pchar = (unsigned char*)mxGetData(plhs[0]);
for (i=0; i<size*comph*compw; 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,
int compw,
int comph)
{
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;
// build histogram
for (y=0; y<=h-comph;y++)
{
for (x=0; x<=w-compw; x++)
{
int index =0;
int offset = y+x*h;
if (searchpos[offset]==0)
continue;
for(i=0; i<256; i++)
hist[i]=0;
sum=0;
sqsum=0;
for (j=0; j<compw;j++)
{
for (i=0; i<comph;i++)
{
pout[index]=in[offset+i+j*h];
hist[pout[index]]++;
index++;
}
}
// 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/comph/compw;
// apply histogram equalization
for (i=0; i<comph*compw; i++)
pout[i]=hist[pout[i]];
x_array[sample_index]=x+1;
y_array[sample_index]=y+1;
sample_index++;
pout+=comph*compw;
}
}
return sample_index;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -