📄 imageprocessing.cpp
字号:
}
}
}
memcpy(lpDIBBits,lpNewDIBBits,lWidth*lHeight);
PutPoints(lWidth,lHeight,lpOutput,lpDIBBits);
free(lpNewDIBBits);
free(lpTransArea);
return true;
}
//find the maximize number
int maxnumber(int d1,int d2,int d3,int d4)
{
int temp1=(d1>d2?d1:d2);
int temp2=(d3>d4?d3:d4);
return (temp1>temp2?temp1:temp2);
}
int maxnumber2(int d1,int d2,int d3,int d4,int d5,int d6)
{
int t1=d1>d2?d1:d2;
int t2=d3>d4?d3:d4;
t1=t1>t2?t1:t2;
t2=d5>d6?d5:d6;
return t1>t2?t1:t2;
}
//////////////////////////////////////////////////////////////////////////
bool Convolution(int lWidth,int lHeight,BYTE *lpOutput)
{
BYTE Mask[3][3]={{1,1,1},{1,1,1},{1,1,1}};
int scale=9;
//int len=12;
BYTE *lpDib;
lpDib=(BYTE *)malloc(lWidth*lHeight);
memset(lpDib,(BYTE)0,lWidth*lHeight);
int thred=180;
int total;
for(int i=1;i<lHeight-1;i++)
for(int j=1;j<lWidth-1;j++)
{
if(*(lpOutput+i*lWidth+j)==255)
{
total=0;
for(int y=-1;y<=1;y++)
{
for(int x=-1;x<=1;x++)
total +=*(lpOutput+(i+y)*lWidth+j)*Mask[y+1][x+1];
}
if((total/scale) >thred)
*(lpDib+i*lWidth+j)=*(lpOutput+i*lWidth+j);
}
}
memcpy(lpOutput,lpDib,lWidth*lHeight);
free(lpDib);
return true;
}
////////////////////////////////////////////////////////////////////
void Sobel2(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput)
{
int x,y,x1,y1,i;
BYTE *lpPoints=new BYTE[nWidth*nHeight];
BYTE *lpPoints1=new BYTE[nWidth*nHeight];
memset(lpPoints1,0,nWidth*nHeight);
//GetPoints(nWidth,nHeight,lpInput,lpPoints);
memcpy(lpPoints,lpInput,nWidth*nHeight);
int d,max;
static s[8][9]={
{-1,-2,-1,0,0,0,1,2,1},
{0,-1,-2,1,0,-1,2,1,0},
{1,0,-1,2,0,-2,1,0,-1},
{2,1,0,1,0,-1,0,-1,-2},
{1,2,1,0,0,0,-1,-2,-1},
{0,1,2,-1,0,1,-2,-1,0},
{-1,0,1,-2,0,2,-1,0,1},
{-2,-1,0,-1,0,1,0,1,2}
};
for(y=1;y<nHeight-1;y++)
{
for(x=1;x<nWidth-1;x++)
{
max=0;
for(i=0;i<8;i++)
{
d=0;
for(y1=0;y1<3;y1++)
for(x1=0;x1<3;x1++)
{
d+=s[i][x1+y1*3]*Point(x+x1-1,y+y1-1);
}
if (d>max) max=d;
}
if (max>255) max=255;
Point1(x,y)=(BYTE)max;
}
//Progress(y);
}
//PutPoints(nWidth,nHeight,lpOutput,lpPoints1);
memcpy(lpOutput,lpPoints1,nWidth*nHeight);
delete lpPoints;
delete lpPoints1;
}
///////////////////////////////////////////////////////////////////////////////
void SobelThin(int lWidth,int lHeight,BYTE *lpInput,BYTE *lpOutput)
{
int g;
Sobel2(lWidth,lHeight,lpInput,lpOutput);
for(int i=1;i<lHeight-1;i++)
{
for(int j=1;j<lWidth-1;j++)
{
g=*(lpInput+i*lWidth+j)-*(lpOutput+i*lWidth+j);
if(*(lpInput+i*lWidth+j-1)+*(lpInput+i*lWidth+j+1)==0\
||*(lpInput+(i-1)*lWidth+j)+*(lpInput+(i+1)*lWidth+j)==0)
{
*(lpOutput+i*lWidth+j)=*(lpInput+i*lWidth+j);
}
else
{
if(g<0)
*(lpOutput+i*lWidth+j)=0;
else
*(lpOutput+i*lWidth+j)=g;
}
}
}
}
////////////////////////////////////////////////////////////////////
void BiValue2(int nWidth,int nHeight,BYTE *lpOutput)
{
BYTE *lpPoints=new BYTE[nWidth*nHeight];
//GetPoints(nWidth,nHeight,lpInput,lpPoints);
memcpy(lpPoints,lpOutput,nWidth*nHeight);
double t=GetNumber("请输入阈值",128,255);
BYTE threshold=(BYTE)t;
int x,y;
for(y=0;y<nHeight;y++)
{
for(x=0;x<nWidth;x++)
{
if (Point(x,y)>threshold) Point(x,y)=255;
else Point(x,y)=0;
}
}
//PutPoints(nWidth,nHeight,lpOutput,lpPoints);
memcpy(lpOutput,lpPoints,nWidth*nHeight);
delete lpPoints;
}
////////////////////////////////////////////
void noiseRemove(int lWidth,int lHeight,BYTE *lpOutput)
{
BYTE *MM;
BYTE *NN;
BYTE *lpDib=lpOutput;
for(int i=0;i<lHeight;i++)
{
for(int j=0;j<lWidth;j++)
{
if(*(lpDib+i*lWidth+j)==255)
*(lpDib+i*lWidth+j)=1;
}
}
MM=(BYTE *)malloc(lWidth*lHeight);
memset(MM,(BYTE)0,lWidth*lHeight);
NN=(BYTE *)malloc(lWidth*lHeight);
memset(NN,(BYTE)0,lWidth*lHeight);
for(int i=2;i<lHeight-2;i++)
for(int j=2;j<lWidth-2;j++)
{
if(*(lpDib+i*lWidth+j)==1)
{
if(*(lpDib+(i-1)*lWidth+j-1)+*(lpDib+(i-1)*lWidth+j)\
+*(lpDib+(i-1)*lWidth+j+1)+*(lpDib+i*lWidth+j-1) > 0)
*(MM+i*lWidth+j)=maxnumber(*(MM+(i-1)*lWidth+j-1),*(MM+(i-1)*lWidth+j),\
*(MM+(i-1)*lWidth+j+1),*(MM+i*lWidth+j-1))+1;
else
*(MM+i*lWidth+j)=maxnumber2(*(MM+(i-2)*lWidth+j-1),*(MM+(i-2)*lWidth+j),\
*(MM+(i-2)*lWidth+j+1),*(MM+(i-1)*lWidth+j-2),*(MM+(i-1)*lWidth+j+2),\
*(MM+i*lWidth+j-2))+1;
}
}
for(int i=lHeight-2;i>=2;i--)
for(int j=lWidth-2;j>=2;j--)
{
if(*(lpDib+i*lWidth+j)==1)
{
if(*(lpDib+(i+1)*lWidth+j-1)+*(lpDib+(i+1)*lWidth+j)\
+*(lpDib+(i+1)*lWidth+j+1)+*(lpDib+i*lWidth+j+1) > 0)
*(NN+i*lWidth+j)=maxnumber(*(NN+(i+1)*lWidth+j-1),*(NN+(i+1)*lWidth+j),\
*(NN+(i+1)*lWidth+j+1),*(NN+i*lWidth+j+1))+1;
else
*(NN+i*lWidth+j)=maxnumber2(*(NN+(i+2)*lWidth+j-1),*(NN+(i+2)*lWidth+j),\
*(NN+(i+2)*lWidth+j+1),*(NN+(i+1)*lWidth+j-2),*(NN+(i+1)*lWidth+j+2),\
*(NN+i*lWidth+j+2))+1;
}
}
int TL=200;
int TS=5;
for(int i=0;i<lHeight;i++)
for(int j=0;j<lWidth;j++)
{
if(*(lpDib+i*lWidth+j)==1)
{
if(*(MM+i*lWidth+j)+*(NN+i*lWidth+j)>TL || *(MM+i*lWidth+j)+*(NN+i*lWidth+j)<TS)
*(lpDib+i*lWidth+j)=0;
}
}
for(int i=0;i<lHeight;i++)
{
for(int j=0;j<lWidth;j++)
{
if(*(lpDib+i*lWidth+j)==1)
*(lpDib+i*lWidth+j)=255;
else
*(lpDib+i*lWidth+j)=0;
}
}
}
//////////////////////////////////////////
//clear the voice
bool NoiseRm(int lWidth,int lHeight,BYTE *lpOutput)
{
BYTE *lpDIBBits;
lpDIBBits=(BYTE *)malloc(lWidth*lHeight);
GetPoints(lWidth,lHeight,lpOutput,lpDIBBits);
//Sobel2(lWidth,lHeight,lpDIBBits);
BiValue2(lWidth,lHeight,lpDIBBits);
//BYTE *lpDib;
//lpDib=(BYTE *)malloc(lWidth*lHeight);
//memset(lpDib,(BYTE)0,lWidth*lHeight);
noiseRemove(lWidth,lHeight,lpDIBBits);
//Convolation(lWidth,lHeight,lpDIBBits);
PutPoints(lWidth,lHeight,lpOutput,lpDIBBits);
free(lpDIBBits);
return true;
}
//////////////////////////////////////////////////////////////////////////
//texture
BYTE Texture(int lWidth,int lHeight,BYTE *lpOutput)
{
BYTE model[3][3]={{1,2,4},{8,0,16},{32,64,128}};
//BYTE temp[3][3];
BYTE LBP;
BYTE flag;
BYTE Gmax=0;
BYTE Gmin=0;
for(int i=1;i<lHeight-1;i++)
{
for(int j=1;j<lWidth-1;j++)
{
LBP=0;
//flag=0;
for(int m=-1;m<=1;m++)
{
for(int n=-1;n<=1;n++)
{
//flag=(*(lpOutput+i*lWidth+j)-*(lpOutput+(i+m)*lWidth+j+n)>=0)?1:0;
if(*(lpOutput+i*lWidth+j)-*(lpOutput+(i+m)*lWidth+j+n)>0)
flag=1;
else
flag=0;
LBP +=flag*model[m+1][n+1];
}
}
*(lpOutput+i*lWidth+j)=LBP;
if(LBP>Gmax)Gmax=LBP;
if(LBP<Gmin)Gmin=LBP;
}
}
return Gmax-2*(Gmax-Gmin)/3;
}
/////////////////////////////////////////////////////////////////
typedef struct REC{
int x;
int y;
int hei;
int wid;
}REC;
void PutPoints1(int nWidth,int nHeight,BYTE *lpBits,BYTE *lpPoints,REC *rectangle)
{
//int h=80;
//int w=270;
int nByteWidth=nWidth*3;
if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4);
int x,y,p,p1;
for(y=0;y<nHeight;y++)
{
for(x=0;x<nWidth;x++)
{
p=x*3+y*nByteWidth;
p1=x+y*nWidth;
lpBits[p]=lpPoints[p1];
lpBits[p+1]=lpPoints[p1];
lpBits[p+2]=lpPoints[p1];
}
}
for(y=0;y<rectangle->hei;y++)
{
p=(rectangle->y+y) * nByteWidth + rectangle->x * 3;
p1=(rectangle->y+y+2) * nByteWidth + (rectangle->x+rectangle->wid)*3;
lpBits[p]=0;
lpBits[p+1]=0;
lpBits[p+2]=255;
lpBits[p1]=0;
lpBits[p1+1]=0;
lpBits[p1+2]=255;
}
for(x=0;x<rectangle->wid;x++)
{
p=rectangle->y * nByteWidth + (rectangle->x+x) * 3;
p1=(rectangle->y+rectangle->hei+2) * nByteWidth + (rectangle->x+x) * 3;
lpBits[p]=0;
lpBits[p+1]=0;
lpBits[p+2]=255;
lpBits[p1]=0;
lpBits[p1+1]=0;
lpBits[p1+2]=255;
}
}
///////////////////////////////////////////////////////////////////
#define pixel(x,y) lpOutput[(x)+(y)*lWidth]
bool Detection(int lWidth,int lHeight,BYTE *lpOutput,REC *rectangle)
{
int h=55;
int w=175;
BYTE *hori=new BYTE[w];
BYTE *vert=new BYTE[h];
int x;
int y;
BYTE thredh=20;
int hcount;
int vcount;
int n;
int m;
for(int i=0;i<lHeight-h;i +=2)
{
for(int j=0;j<lWidth-w;j +=2)
{
memset(hori,(BYTE)0,w);
memset(vert,(BYTE)0,h);
for(m=0;m<h;m++)
{
for(n=0;n<w;n++)
{
y=i+m;
x=j+n;
if(pixel(x,y)==255)
{
vert[m]++;
hori[n]++;
}
}
}
hcount=0;
m=0;
while(vert[m++]==0 && m<h-1);
rectangle->y=i+m-1;
while(m<h-1)
{
if(abs(vert[m]-vert[m-1])>thredh||vert[m]<5)
hcount++;
m +=1;
}
if(hcount>15)
continue;
else
{
vcount=0;
n=0;
while(hori[n]==0&&n<w-1)
{
n++;
}
rectangle->x=j+n;
while(n<w-1)
{
if(hori[n]>13 && hori[n+1]<5)
vcount++;
n +=1;
}
if(vcount>3&&vcount<=15)//
{
rectangle->hei=h-(rectangle->y-i);
rectangle->wid=w-(rectangle->x-j);//
return true;
}
}
}
}
return false;
}
//////////////////////////////////////////////////////////////////////
void LoG2(int nWidth,int nHeight,BYTE *lpIn)
{
BYTE *lpOut=lpIn;
BYTE *lpInput=new BYTE[nWidth*nHeight];
BYTE *lpOutput=new BYTE[nWidth*nHeight];
//GetPoints(nWidth,nHeight,lpIn,lpInput);
memcpy(lpInput,lpIn,nWidth*nHeight);
double thresh=0,sigma=1;
int x,y,xx,yy;
int radius,window; //window of the converlutin kernel(window=2*radius+1)
double dev_inv=0.5/(sigma*sigma); //1/(2*sigma^2)
radius = (int)ceil(3*sigma);
window = radius*2+1;
//generate mask of convolution (matlab code)
//std2 = std*std;
//h1 = exp(-(x.*x + y.*y)/(2*std2));
double* mask = new double [window*window];
double sum=0;
double std2=2*sigma*sigma;
for(y=0;y<radius;y++)
{
for(x=0;x<radius;x++)
{
yy=(y-radius)*(y-radius);
xx=(x-radius)*(x-radius);
mask[x+y*window]=exp(-(xx+yy)*dev_inv);
// mask[window-1-x+y*window]=mask[x+y*window];
// mask[window-1-x+(window-1-y)*window]=mask[x+y*window];
// mask[x+(window-1-y)*window]=mask[x+y*window];
sum=sum+4*mask[x+y*window];
}
}
x=radius;
for(y=0;y<radius;y++)
{
yy=(y-radius)*(y-radius);
mask[x+y*window]=exp(-yy*dev_inv);
// mask[x+(window-1-y)*window]=mask[x+y*window];
sum=sum+2*mask[x+y*window];
}
y=radius;
for(x=0;x<radius;x++)
{
xx=(x-radius)*(x-radius);
mask[x+y*window]=exp(-xx*dev_inv);
// mask[window-1-x+y*window]=mask[x+y*window];
sum=sum+2*mask[x+y*window];
}
mask[radius+radius*window]=1;
sum=sum+mask[radius+radius*window];
//h = h1.*(x.*x + y.*y - 2*std2)/(2*pi*(std^6));
double denominator=2*3.14159265*pow(sigma,6)*sum;
sum=0;
for(x=0;x<radius;x++)
{
for(y=0;y<radius;y++)
{
yy=(y-radius)*(y-radius);
xx=(x-radius)*(x-radius);
mask[x+y*window]=mask[x+y*window]*(xx+yy-2*std2)/denominator;
// mask[window-1-x+y*window]=mask[x+y*window];
// mask[window-1-x+(window-1-y)*window]=mask[x+y*window];
// mask[x+(window-1-y)*window]=mask[x+y*window];
sum=sum+4*mask[x+y*window];
}
}
x=radius;
for(y=0;y<radius;y++)
{
yy=(y-radius)*(y-radius);
mask[x+y*window]=mask[x+y*window]*(yy-2*std2)/denominator;
// mask[x+(window-1-y)*window]=mask[x+y*window];
sum=sum+2*mask[x+y*window];
}
y=radius;
for(x=0;x<radius;x++)
{
xx=(x-radius)*(x-radius);
mask[x+y*window]=mask[x+y*window]*(xx-2*std2)/denominator;
// mask[window-1-x+y*window]=mask[x+y*window];
sum=sum+2*mask[x+y*window];
}
mask[radius+radius*window]=mask[radius+radius*window]*(-2*std2)/denominator;
sum=sum+mask[radius+radius*window];
//h = h - sum(h(:))/prod(size(h)); % make the filter sum to zero
double average=sum/(window*window);
for(x=0;x<radius;x++)
{
for(y=0;y<radius;y++)
{
mask[x+y*window]=mask[x+y*window]-average;
mask[window-1-x+y*window]=mask[x+y*window];
mask[window-1-x+(window-1-y)*window]=mask[x+y*window];
mask[x+(window-1-y)*window]=mask[x+y*window];
}
}
x=radius;
for(y=0;y<radius;y++)
{
mask[x+y*window]=mask[x+y*window]-average;
mask[x+(window-1-y)*window]=mask[x+y*window];
}
y=radius;
for(x=0;x<radius;x++)
{
mask[x+y*window]=mask[x+y*window]-average;
mask[window-1-x+y*window]=mask[x+y*window];
}
mask[radius+radius*window]=mask[radius+radius*window]-average;
double* lpMediate = new double[nWidth*nHeight];
//2D convolution
if(Conv2(nWidth,nHeight,lpMediate,lpInput,mask,window,window))
{
TRACE("error in Conv2");
return;
}
//calcaulate the thresh for default
// thresh = .75*mean2(abs(b(rr,cc)));
sum = 0;
for(y=radius;y<nHeight-radius;y++)
{
for(x=radius;x<nWidth-radius;x++)
{
sum = sum + fabs(Mediate(x,y));
}
}
thresh=0.5*sum/((nWidth-2*radius)*(nHeight-2*radius));
//zerocross cal
if(Zerocross(nWidth,nHeight,lpOutput,lpMediate,thresh))
{
TRACE("error in Zerocross");
return;
}
delete mask;
//PutPoints(nWidth,nHeight,lpOut,lpOutput);
memcpy(lpOut,lpOutput,nWidth*nHeight);
delete lpInput;
delete lpOutput;
}
///////////////////////////////////////////////////////////////////////
void BiValue3(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput)
{
BYTE *lpPoints=new BYTE[nWidth*nHeight];
BYTE *lpDib=new BYTE[nWidth*nHeight];
BYTE threshold=GetPoints(nWidth,nHeight,lpInput,lpPoints);
int x,y;
for(y=0;y<nHeight;y++)
{
for(x=0;x<nWidth;x++)
{
if (Point(x,y)>threshold) Point(x,y)=255;
else Point(x,y)=0;
}
}
threshold=Texture(nWidth,nHeight,lpPoints);
for(y=0;y<nHeight;y++)
{
for(x=0;x<nWidth;x++)
{
if (Point(x,y)>threshold) Point(x,y)=255;
else Point(x,y)=0;
}
}
//Sobel2(nWidth,nHeight,lpPoints,lpDib);
//Convolution(nWidth,nHeight,lpPoints);
noiseRemove(nWidth,nHeight,lpPoints);
memcpy(lpDib,lpPoints,nWidth*nHeight);
//SobelThin(nWidth,nHeight,lpPoints,lpDib);
/*threshold=Texture(nWidth,nHeight,lpDib);
for(y=0;y<nHeight;y++)
{
for(x=0;x<nWidth;x++)
{
if (Point(x,y)>threshold) Point(x,y)=255;
else Point(x,y)=0;
}
}*/
//LoG2(nWidth,nHeight,lpPoints);
//
REC *rectangle=new REC;
bool flag=false;
flag=Detection(nWidth,nHeight,lpPoints,rectangle);
if(flag)
PutPoints1(nWidth,nHeight,lpOutput,lpDib,rectangle);
else
PutPoints(nWidth,nHeight,lpOutput,lpDib);
delete lpPoints;
}
///////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -