📄 imgprocessing.cpp
字号:
{
for(k=0;k<(xs/2);k++)
{
bufferx[k*2]=oldimg[n*xsize+k]-128.0 ;
bufferx[k*2+1]=0;
}
for(i=0;i<xs;i++)
{
temp1=0;
for(j=-(DD-1)/2;j<=(DD-1)/2;j++)
temp1=temp1+hi[j+(DD-1)/2]*bufferx[a(i+j,xs)];
buffery[i]=temp1;
}
for(k=0;k<(xs/2);k++)
{
bufferx[k*2]=oldimg[(k+xs/2)+xsize*n]-128.0;
bufferx[k*2+1]=0;
}
for(i=0;i<xs;i++)
{
temp1=0;
for(j=-(DD-1)/2-1;j<=(DD-1)/2-1;j++)
temp1=temp1+gi[j+(DD-1)/2+1]*bufferx[a(i+j,xs)];
temp2=temp1+buffery[i];
buffery[i]=temp2;
}
for(k=0;k<xs;k++)
{
buffery[k]=s(buffery[k]);
oldimg[k+xsize*n]=(BYTE)(buffery[k]+128);
}
}
}*/
/*******************************************/
/* function for scale transform */
/* oldimg : the original image */
/* newimg : the result image */
/* newwidth : the width of result image */
/* newheight: the height of result image */
/* oldwidth : the width of original image */
/* oldheight: the height of original image */
/*******************************************/
void Img_ScaleTransform(CByteArray& oldimg,CByteArray& newimg,
int oldheight,int oldwidth,
int newheight,int newwidth)
{
int newx,newy;
double oldx1,oldy1,oldx2,oldy2,s,t;
int lefttop,leftbottom,righttop,rightbottom;
double sx=(double)oldwidth/(double)newwidth,
sy=(double)oldheight/(double)newheight;
double value1,value2;
DWORD value;
BYTE buf;
newimg.RemoveAll();
newimg.SetSize(newheight*newwidth);
for(newy=0;newy<newheight;newy++)
{
oldy1=newy*sy ;
for(newx=0;newx<newwidth;newx++)
{
value=0;
oldx1=newx*sx ;
oldx2=(int)oldx1+1;
oldy2=(int)oldy1+1;
//edge case
if(oldx1>=(oldwidth-1))
{
oldx1=oldwidth-1 ;
oldx2=oldwidth-1;
}
else
if(oldx1<1)
{
oldx1=0;
oldx2=0;
}
if(oldy1>=(oldheight-1))
{
oldy1=oldheight-1 ;
oldy2=oldheight-1;
}
else
if(oldy1<1)
{
oldy1=0;
oldy2=0;
}
lefttop=oldimg[(int)oldy1*oldwidth+(int)oldx1];
leftbottom=oldimg[(int)oldy2*oldwidth+(int)oldx1];
righttop=oldimg[(int)oldy1*oldwidth+((int)oldx2)];
rightbottom=oldimg[((int)oldy2)*oldwidth+((int)oldx2)];
s=oldx1-(int)oldx1;
value1=lefttop*(1-s)+righttop*s;
value2=leftbottom*(1-s)+righttop*s;
t=oldy1-(int)oldy1;
value=(DWORD)(value1*(1-t)+value2*t);
buf=(BYTE)value;
newimg[newy*newwidth+newx]=buf;
}
}
return;
}
/******************************************/
/* function for rotate transform */
/* imga : the original image */
/* imgb : the result image */
/* point: the point around which rotate */
/* theta: the angle rotate */
/******************************************/
void Img_RotateTransform(CByteArray& imga,CByteArray& imgb,
int height,int width,
CPoint point,double theta,int nCount)
{
int newx,newy;
double oldx,oldy,s,t;
int x0=point.x;
int y0=point.y;
int x1,y1,x2,y2;
BYTE value=0;
BYTE value24[3];
double cs=cos(theta),sn=sin(theta);
imgb.RemoveAll();
imgb.SetSize(height*width*nCount);
//旋转角度小于0.7度即弧度小于0.01
if((theta<=0.01)&&(theta>=-0.01))
{
Img_Copy(imga,imgb);
return;
}
for(newy=0;newy<height;newy++)
for(newx=0;newx<width;newx++)
{
oldx=x0+(newx-x0)*cs+(newy-y0)*sn;
oldy=y0-(newx-x0)*sn+(newy-y0)*cs;
x1=(int)oldx;
y1=(int)oldy;
x2=x1+1;
y2=y1+1;
//edge case
if(oldx>=(width-1))
{
oldx=width-1;
x1=width-1 ;
x2=width-1;
}
else
if(oldx<1)
{
oldx=0;
x1=0;
x2=0;
}
if(oldy>=(height-1))
{
oldy=height-1 ;
y1=height-1;
y2=height-1;
}
else
if(oldy<1)
{
oldy=0;
y1=0;
y2=0;
}
s=oldx-x1;t=oldy-y1;
if(nCount==1)
{
value=(BYTE)((1-s)*(1-t)*imga[y1*width+x1]
+(1-s)*t*imga[y2*width+x1]
+s*(1-t)*imga[y1*width+x2]
+s*t*imga[y2*width+x2]);
imgb[newy*width+newx]=value;
}
if(nCount==3)
{
value24[0]=(BYTE)((1-s)*(1-t)*imga[y1*width*nCount+x1*nCount]
+(1-s)*t*imga[y2*width*nCount+x1*nCount]
+s*(1-t)*imga[y1*width*nCount+x2*nCount]
+s*t*imga[y2*width*nCount+x2*nCount]);
value24[1]=(BYTE)((1-s)*(1-t)*imga[y1*width*nCount+x1*nCount+1]
+(1-s)*t*imga[y2*width*nCount+x1*nCount]
+s*(1-t)*imga[y1*width*nCount+x2*nCount]
+s*t*imga[y2*width*nCount+x2*nCount]);
value24[2]=(BYTE)((1-s)*(1-t)*imga[y1*width*nCount+x1*nCount+2]
+(1-s)*t*imga[y2*width*nCount+x1*nCount]
+s*(1-t)*imga[y1*width*nCount+x2*nCount]
+s*t*imga[y2*width*nCount+x2*nCount]);
imgb[newy*width*nCount+newx*nCount]=value24[0];
imgb[newy*width*nCount+newx*nCount+1]=value24[1];
imgb[newy*width*nCount+newx*nCount+2]=value24[2];
}
}
return;
}
/******************************************/
/* function for get mean of image region */
/* img : the image */
/* imgheight: height of image */
/* imgwidth : width of image */
/* rect : the rect */
/* return value: mean */
/******************************************/
double Img_GetMean(CByteArray& img,int imgheight,int imgwidth)
{
int i,j;
double mean=0.0;
for(i=0;i<imgheight;i++)
for(j=0;j<imgwidth;j++)
mean+=((double)img[i*imgwidth+j]);///(double)(height*width));
return mean/(imgwidth*imgheight);
}
/*********************************************/
/* function for get varience of image region*/
/* img : the image */
/* imgheight: height of image */
/* imgwidth : width of image */
/* rect : the rect */
/* mean : the mean of image region */
/* return value: varience */
/*********************************************/
double Img_GetVar(CByteArray& img,int imgheight,int imgwidth,double mean)
{
int i,j;
double var=0.0,temp;
for(i=0;i<imgheight;i++)
for(j=0;j<imgwidth;j++)
{
temp=(img[i*imgwidth+j]-mean);
var+=(temp*temp);///(height*width);
}
return sqrt(var/(imgheight*imgwidth));
}
/*********************************************/
/* function for get varient of image region */
/* img : the image */
/* imgheight: height of image */
/* imgwidth : width of image */
/* rect : the rect */
/* return value : varient */
/*********************************************/
double Img_GetVar(CByteArray& img,int imgheight,int imgwidth)
{
double mean=Img_GetMean(img,imgheight,imgwidth);
int i,j;
double var=0.0,temp;
for(i=0;i<imgheight;i++)
for(j=0;j<imgwidth;j++)
{
temp=img[i*imgwidth+j]-mean;
var+=(temp*temp);
}
return sqrt(var/(imgheight*imgwidth));
}
/**********************************************/
/* function for get varience of image region */
/* img : the image */
/* height : height of image */
/* width : width of image */
/* mean : mean of image */
/* var : varience of image */
/**********************************************/
void Img_MeanAndVar(CByteArray& imgbody,int height,int width,double& mean,double& var)
{
mean=var=0.0;
int i,j;
for(i=0;i<height;i++)
for(j=0;j<width;j++)
mean+=((double)imgbody[i*width+j]/(height*width));
for(i=0;i<height;i++)
for(j=0;j<width;j++)
var+=(((double)imgbody[i*width+j]-mean)*((double)imgbody[i*width+j]-mean))/(height*width);
var=sqrt(var);
}
/*********************************************/
/* function to get relate of image region */
/* img : the image */
/* th : height of image */
/* tw : width of image */
/* tmean : mean of tempal */
/* tvar : var of tempal */
/* teml : the point of tempal */
/* Return value: the relate degree to templ */
/*********************************************/
double Img_Relate
(CByteArray& img,CByteArray& templ,int imgheight,int imgwidth,double tmean,double tvar,CRect& rect)
{
int k,l;
double imean=0.0,relate=0.0,ivar=0.0;
int left=rect.left;
int top=rect.top;
int height=rect.Height();
int width=rect.Width();
int w=height*width;
for(k=0;k<height;k++)
for(l=0;l<width;l++)
imean+=(double)img[(k+top)*imgwidth+left+l];
imean=imean/(double)w;
for(k=0;k<height;k++)
for(l=0;l<width;l++)
{
relate+=(double)(templ[k*width+l]-tmean)*((double)img[(k+top)*imgwidth+left+l]-imean);
ivar+=(((double)(img[(k+top)*imgwidth+left+l]-imean)*((double)img[(k+top)*imgwidth+left+l]-imean)));
}
ivar=sqrt(ivar/w);
return(relate/(tvar*ivar*w));
}
/******************************************/
/* Function to compute the match degree */
/* img :image for match */
/* templ :tempal for match */
/* th :height of image */
/* tw :width of image */
/* tmean :mean of tempal */
/* tvar :varience of tempal */
/* weight : weight value */
/******************************************/
double Img_GetDegree
(CByteArray& img,CByteArray& templ,int th,int tw,CRect& rect,double tmean,double tvar,double weight)
{
double dis=Img_GetDist(img,templ,th,tw,rect);
double rel=0;//=Img_Relate(img,templ,th,tw,tmean,tvar);
return (rel+weight/(1+dis));
}
/************************************************/
/* Function for geting distance of image region*/
/* from given tempal */
/* img : the image */
/* imgheight: height of image */
/* imgwidth : width of image */
/* rect : the rect */
/************************************************/
double Img_GetDist(DoubleArray& imga, DoubleArray& imgb)
{
int i,num=imgb.GetSize();
double dis=0.0;
for(i=0;i<num;i++)
dis+=((imga[i]-imgb[i])*(imga[i]-imgb[i]));
return sqrt(dis/(double)(num-3)) ;
/* int i,num=imgb.GetSize();
double dis=0.0;
for(i=0;i<num;i++)
{
double x=imga[i]-imgb[i];
if(x<0.0)
x=-x;
dis+=x;
}
return dis;*/
}
double Img_GetDist(CByteArray& imga, DoubleArray& imgb)
{
int i,num=imgb.GetSize();
//num=50;
double dis=0.0;
for(i=0;i<num;i++)
dis+=((imga[i]-imgb[i])*(imga[i]-imgb[i]));
return sqrt(dis/(double)(num-3)) ;
}
double Img_MDist(CByteArray& imga,CByteArray& imgb,int height,int width,
int Nsize)
{
int i,j,k,l;
double dis,disAB;
BYTE value;
disAB=0.0;
int min,x;
for(i=0;i<height;i++)
for(j=0;j<width;j++)
{
value=imga[i*width+j];
min=255;
for(k=0;k<=Nsize;k++)
{
for(l=-k;l<=+k;l++)
{
if((j+l>=0)&&(j+l<width)&&(i+k>=0)&&(i+k<height))
{
if((x=abs(value-imgb[(i+k)*width+j+l]))<min)
{
min=x;
}
}
if((i+l>=0)&&(i+l<height)&&(j+k>=0)&&(j+k<width))
{
if((x=abs(value-imgb[(i+l)*width+j+k]))<min)
{
min=x;
}
}
if((i-k>=0)&&(i-k<height)&&(j+l>=0)&&(j+l<width))
if((x=abs(value-imgb[(i-k)*width+j+l]))<min)
{
min=x;
}
if((i+l>=0)&&(i+l<height)&&(j-k>=0)&&(j-k<width))
if((x=abs(value-imgb[(i+l)*width+j-k]))<min)
{
min=x;
}
}//for l
}//for k
disAB+=(min*min);
}//for outer
dis=sqrt(disAB/(width*height));
return dis;
}
double Img_GetDist(CByteArray& img, CByteArray& templ,
int imgheight,int imgwidth,CRect &rect )
{
double dis=0.0;
int k,l;
int top=rect.top;
int left=rect.left;
int height=rect.Height();
int width=rect.Width();
for(k=0;k<height;k++)
for(l=0;l<width;l++)
{
int x=templ[k*width+l];
int y=img[(top+k)*imgwidth+l+left];
dis+=(abs(templ[k*width+l]-img[(top+k)*imgwidth+l+left])*
abs(templ[k*width+l]-img[(top+k)*imgwidth+l+left]));
}
return sqrt(dis/(height*width)) ;
}
double Img_GetDist(CByteArray& imga,CByteArray& imgb)
{
int i;
double dis=0.0;
int size=imga.GetSize();
for(i=0;i<size;i++)
{
int x=imga[i];
int y=imgb[i];
dis+=(abs(imga[i]-imgb[i])*abs(imga[i]-imgb[i]));
}
return sqrt(dis/(double)size);
}
/*********************************************/
/* function for get Nomalized image of */
/* original image region */
/* img : the image */
/* imgheight: height of image */
/* imgwidth : width of image */
/* rect : the rect */
/* nomimg : the nomalized image */
/*********************************************/
// Note : old image and new image must be different
void Img_Nomalize(CByteArray& img,int imgheight,int imgwidth,double svar,double smean)
{
double mean=Img_GetMean(img,imgheight,imgwidth);
double var=Img_GetVar(img,imgheight,imgwidth,mean);
double temp;
int i,j;
double w=((double)svar)/var;
BYTE buf;
for(i=0;i<imgheight;i++)
for(j=0;j<imgwidth;j++)
{
temp=w*(img[i*imgwidth+j]-mean)+smean;
if(temp<0) temp=0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -