📄 imagepr.cpp
字号:
return number;
}
void ImagePr::YCbCr()
{
//algorithm by Rein-Lien Hsu. 2003.5.22
double r,g,b;
int y,cb,cr;
int i,j,t;
for(i=0; i<myPHeight; i++) //initialize
{
for (j=0; j<myPWidth; j++)
{
if(*(RegionLabel+i*myPWidth+j)>0)
{
r=(double)GetRValue(RGBImage[i*myPWidth+j])/(double)255;//RGBImage
g=(double)GetGValue(RGBImage[i*myPWidth+j])/(double)255;
b=(double)GetBValue(RGBImage[i*myPWidth+j])/(double)255;
y=int(16+65.481*r+128.553*g+24.966*b);
y=(int)((y-16)*255/(235-16));
cb=int(128-37.797*r-74.203*g+112*b);
cb=(int)((cb-16)*255/(240-16));
cr=int(128+112*r-93.786*g-18.214*b);
cr=(int)((cr-16)*255/(240-16));
t=2*(cb*cb/255+(255-cr)*(255-cr)/255+(cb/cr));// /3,originally.
//t=cr*cr*(cr*cr-0.8*cr/cb)*(cr*cr-0.8*cr/cb); ///(255*255)unsucsessful
if(t>255)
t=255;
*(myPArray+i*myPWidth+j)=RGB(t,t,t);
}
else
*(myPArray+i*myPWidth+j)=RGB(0,0,0);
}
}
}
void ImagePr::binarize(char c)
{
//binarize(char c, int n),if n=256,then for intensity,else if n=360,for hue.
//and numi,pi need to be dynamicly allocated.
/**
** function name:binarize()
** purpose:for every segmented region,get its own threshhold.
** global varible-- myPArray,gFlag,RegionLabel
** call block: none,but must be excuated after RegionLabeling() or MomentDescription()
** founder: Li Xuewei
** date: 5,21, 2003
** mender: Li Xuewei
** date:
*/
//MomentDescription();
Thresh=new short[NumOfLabel];
int i,j;
int label;
for(label=1;label<=NumOfLabel;label++)
{
Thresh[label-1]=-1;
}
short ii;// one point's intensity
int numi[256];//store the total pixels of every intensity's .
double pi[256];//every intensity's probability.
int th; //temp threshhold
double bmax=0; // maximium varience
int maxth=0; // maximium threshhold
double mu,muk,mu0,mu1;
double varience;
double w1,w0;
int counter;
LONGLONG sum;
int maxint;
//get the maxmium intensity level of every region.
for(label=1;label<=NumOfLabel;label++)
{
if(IntCentdx[label-1]>0)//this labeled region remainded.
{
for(i=0;i<256;i++)
{
numi[i]=0;
pi[i]=0;
}
sum=0;
maxint=0;
switch(c)
{
case 'f'://high_boost filtering
{
High_Boost();
for(i=SkinYmin;i<=SkinYmax;i++)
for(j=SkinXmin;j<=SkinXmax;j++)
{
if(*(RegionLabel+i*myPWidth+j)==label)
{
ii=*(gBoost+i*myPWidth+j);
numi[ii]++;
sum++;
if(maxint<ii)
maxint=ii;// this is necessary.
}
}
}
break;
case 'n'://normal binarize
{
for(i=SkinYmin;i<=SkinYmax;i++)
for(j=SkinXmin;j<=SkinXmax;j++)
{
if(*(RegionLabel+i*myPWidth+j)==label)
{
ii=myHSIArray[i*myPWidth+j].I;
numi[ii]++;
sum++;
if(maxint<ii)
maxint=ii;// this is necessary.
}
}
}
}// end switch
mu=0;
for(i=0;i<256;i++)
{
pi[i]=double(numi[i])/(double)sum;//Rsum[label-1];
mu+=i*pi[i];
}
for(th=30;th<maxint;th++)
{
w0=0;mu0=0;
muk=0;mu1=0;
counter=0;
for(i=0;i<=th;i++)
{
w0+=pi[i];
muk+=pi[i]*i;
counter+=numi[i];
}
w1=1-w0;
mu0=muk/w0;
mu1=(mu-muk)/(1-w0);
varience=w0*w1*(mu1-mu0)*(mu1-mu0);
if(bmax<varience)
{
bmax=varience;
maxth=th;
}
/* //p_tile
if(double(counter)/double(Rsum[label-1])>0.1) //p-tile
{//because there is background's distrubs ,so the result is not ideal.
Thresh[label-1]=th;
break;
}*/
}
Thresh[label-1]=maxth;
}
}
//delete []Thresh;to be continued.
}
int ImagePr::binarizeForImage(char c)//short *tp
{
//the algorithm is consulted book <computer image recognition>:page 76
/*
** function name:binarizeForImage()
** purpose:for an input intensity image or gradient image,get its threshhold.
** global varible-- myPArray,myHSIArray
** founder: Li Xuewei
** date: 5,21, 2003
** mender: Li Xuewei
** date:
*/
int threshhold;
int i,j;
threshhold=-1;
double counter1,counter2;//the number of every class.
short ii;// one point's intensity
//get the maxmium intensity level of the image.
int *numi;
numi=new int[256];
for(i=0;i<256;i++)
numi[i]=0;
int maxint=0;
switch(c)
{
case 'f'://high_boost filtering
{
High_Boost();//
for(i=0;i<myPHeight;i++)
{
for(j=0;j<myPWidth;j++)
{
ii=*(gBoost+i*myPWidth+j);
numi[ii]++;
if(maxint<ii)
maxint=ii;
}
}
}
break;
case 'n'://normal
{
for(i=0;i<myPHeight;i++)
{
for(j=0;j<myPWidth;j++)
{
ii=myHSIArray[i*myPWidth+j].I;//*(tp+i*myPWidth+j);
numi[ii]++;
if(maxint<ii)
maxint=ii;
}
}
}
break;
}
int th; //temp threshhold
double w0,w1; // the average intensity of every class.
double bmax=-1; // maximium varience
int maxth=0; // maximium threshhold
double varience;
double mu,muk,mu1,mu0;
double *pi;
pi=new double[256];
mu=0;
for(i=0;i<256;i++)
{
pi[i]=(double)numi[i]/(double)(myPHeight*myPWidth);
mu+=pi[i]*i;
}
for(th=20;th<maxint;th++)
{
counter1=counter2=0;
for(i=0;i<=th;i++)
{
counter1+=pi[i];
counter2+=i*pi[i];
}
w0=counter1;
w1=1-w0;
mu0=counter2/w0;
muk=counter2;
mu1=(mu-muk)/(1-w0);
varience=w0*w1*(mu1-mu0)*(mu1-mu0);
if(bmax<varience)
{
bmax=varience;
maxth=th;
}
}
delete []numi;
delete []pi;
threshhold=maxth;//
return threshhold;
}
void ImagePr::EyeExtraction()
{
/*
** function name:EyeExtraction()
** purpose: extract the eye pixels from every remained region and
dilation,erosion,remove single pixel etc. finally,get
the eye candidates region.
** global varible-- myPArray,myHSIArray,
** call block: MomentDescription(),eyeRegion
** founder: Li Xuewei
** date: 5,19, 2003
** mender: Li Xuewei
** date:
*/
MomentDescription();
int i,j;
int thresh=binarizeForImage('n');//
//extract the eyes.
visionSobel();///*
//ImageEnhance();
short *tEyeP;
eyePixel=new short[myPHeight*myPWidth]; //if this pixel is eye pixel,the it's value is 1.
tEyeP=new short[myPHeight*myPWidth]; //temp for eyePixel.
//get every region's edge
short *EdgePixel;
EdgePixel=new short[myPHeight*myPWidth];
//the following code remove the eye pixels in the region's edge .
int count;
for(i=1; i<myPHeight-1; i++)
{
for (j=1; j<myPWidth-1; j++)
{
*(EdgePixel+i*myPWidth+j)=0;
if(*(gFlag+i*myPWidth+j)==true)
{
count=0;
count+=*(gFlag+(i-1)*myPWidth+j-1)+*(gFlag+(i-1)*myPWidth+j)
+*(gFlag+(i-1)*myPWidth+j+1)
+*(gFlag+i*myPWidth+j-1)+*(gFlag+i*myPWidth+j+1)
+*(gFlag+(i+1)*myPWidth+j-1)+*(gFlag+(i+1)*myPWidth+j)
+*(gFlag+(i+1)*myPWidth+j+1);
if(count<8)
{
*(EdgePixel+i*myPWidth+j)=*(RegionLabel+i*myPWidth+j);
}
}
}
}
//extract the eye pixels.
int R,G,B;
int ii;
int t;
binarize('n');//
short *temp_Thresh;
temp_Thresh=new short[NumOfLabel];
int label;
for(label=1;label<=NumOfLabel;label++)
{
temp_Thresh[label-1]=Thresh[label-1];//the threshhold of every region.
}
//t=*(gradient+221*myPWidth+400); test for 411all
//ii=myHSIArray[221*myPWidth+400].I;
//bool flag;
short enii;//enhanced image intensity.
short maxii=220,minii;//when maxii is more small,the minii will be more great.
int subi,subj;
// FilterFeature();
postprocess();
//int thresh1=binarizeForImage('f');
// int t2;
for(i=1; i<myPHeight-1; i++)
{
for (j=1; j<myPWidth-1; j++)
{
*(eyePixel+i*myPWidth+j)=0;//initialize
*(tEyeP+i*myPWidth+j)=0;
//t2=*(RegionL2+i*myPWidth+j);
if(*(gFlag+i*myPWidth+j)==true && *(EdgePixel+i*myPWidth+j)==0)
{//t2 &&
ii=myHSIArray[i*myPWidth+j].I;
R=GetRValue(RGBImage[i*myPWidth+j]);
//try to extract the eyes
t=*(RegionLabel+i*myPWidth+j);//
count=0;
for(subi=i-1;subi<=i+1;subi++)//if size=1,for a 3*3 region
for(subj=j-1;subj<=j+1;subj++)
{
if(ii<=myHSIArray[subi*myPWidth+subj].I)
count++;
}
minii=(256-maxii)*Thresh[t-1]/(256-Thresh[t-1]);
if(ii<=minii)
enii=0;
if(ii>=maxii)
enii=255;
if(ii>minii && ii<maxii)
enii=(ii-minii)*256/(maxii-minii);
if(temp_Thresh[t-1]>95)//100
temp_Thresh[t-1]=95;//100
//*(DilateFactor+i*myPWidth+j) &&
//if(*(FilterFlag+i*myPWidth+j) )
if( *(gradient+i*myPWidth+j)>=50 && enii<temp_Thresh[t-1] && ii>=10 )//&&R<100*/
//if(*(gBoost+i*myPWidth+j)<thresh1 && enii<temp_Thresh[t-1] && ii>=10)
{//area[t2-1] &&
*(eyePixel+i*myPWidth+j)=*(RegionLabel+i*myPWidth+j);
*(tEyeP+i*myPWidth+j)=*(eyePixel+i*myPWidth+j);
}
}
}
}
//closing operation
int size=2;//1
EyeDilation(size);
EyeErosion(size);/**/
//
//remove the invalid link between eyes and brows or eyes and hair.
/* for(i=0; i<myPHeight; i++)
{
for (j=0; j<myPWidth; j++)
{
*(tEyeP+i*myPWidth+j)=*(eyePixel+i*myPWidth+j);
}
}
for(i=1; i<myPHeight-1; i++)
{
for (j=1; j<myPWidth-1; j++)
{
if(*(tEyeP+i*myPWidth+j)>0)
{
count=0;
for(subi=i-1;subi<=i+1;subi++)//if size=1,for a 3*3 region
for(subj=j-1;subj<=j+1;subj++)
{
count+=*(tEyeP+subi*myPWidth+subj);
}
if(count<=3*(*(tEyeP+i*myPWidth+j)))
{
*(eyePixel+i*myPWidth+j)=0;
}
}
}
}
//*/
//remove the single pixel under certain conditions:
//if this pixel's intensity less than the region's threshhlod,
///*then remain it,else remove it.
for(i=0; i<myPHeight; i++)
{
for (j=0; j<myPWidth; j++)
{
*(tEyeP+i*myPWidth+j)=*(eyePixel+i*myPWidth+j);
}
}
int count1;
//for(i=0; i<myPHeight; i++)
for(i=SkinYmin;i<=SkinYmax;i++)
{
//for (j=0; j<myPWidth; j++)
for(j=SkinXmin;j<=SkinXmax;j++)
{
if(*(tEyeP+i*myPWidth+j)>0)
{
ii=myHSIArray[i*myPWidth+j].I;
t=*(RegionLabel+i*myPWidth+j);
count=0;
count1=0;
for(subi=i-1;subi<=i+1;subi++)//if size=1,for a 3*3 region
for(subj=j-1;subj<=j+1;subj++)
{
count+=*(tEyeP+subi*myPWidth+subj);
if(ii<myHSIArray[i*myPWidth+j].I)
count1++;
}
if(count==*(tEyeP+i*myPWidth+j)&& ii>temp_Thresh[t-1])
//if(count<=2)
{
*(eyePixel+i*myPWidth+j)=0;
}
}
}
}//*/
///*display the final result image.
for(i=0; i<myPHeight; i++)
{
for (j=0; j<myPWidth; j++)
{
if(*(RegionLabel+i*myPWidth+j)>0)
{
R=GetRValue(RGBImage[i*myPWidth+j]);//RGBImage
G=GetGValue(RGBImage[i*myPWidth+j]);
B=GetBValue(RGBImage[i*myPWidth+j]);
ii=(R+G+B)/3;
t=*(RegionLabel+i*myPWidth+j);
//adjust the image contrast.
minii=(256-maxii)*Thresh[t-1]/(256-Thresh[t-1]);
if(ii<=minii)
enii=0;
if(ii>=maxii)
enii=255;
if(ii>minii && ii<maxii)
enii=(ii-minii)*256/(maxii-minii);
if(*(eyePixel+i*myPWidth+j)>0 && enii<temp_Thresh[t-1])//
//*(myPArray+i*myPWidth+j)=RGB(255,0,0);
*(myPArray+i*myPWidth+j)=RGB(0,0,0);// for hejing 2005.4.13
else
{
//*(myPArray+i*myPWidth+j)=RGB(R,G,B);
*(myPArray+i*myPWidth+j)=RGB(255,255,255);// for hejing
*(eyePixel+i*myPWidth+j)=0;
}
}
else
*(myPArray+i*myPWidth+j)=RGB(255,255,255);
}
}
delete []tEyeP;
delete []EdgePixel;
delete []temp_Thresh;
delete []Thresh;// global array
delete []gradient; //global array pointer
delete []gFlag; //finish its commission for face candidates regions.
delete []DilateFactor;
/*delete []RegionL2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -