1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
|
C++ +OpenCV
//===================
//at this point, mean and deviation frames are computed and stored in E & S respectively
//brighteness & chromaticity deviation estimation
std::vector<float>
Alpha,CD;
for(int i=0;i<h;i++)
{
for(int j=0;j<w;j++)
{
int
Mu_r=((int *)(E->imageData + i*E->widthStep))[j*E->nChannels + 0],
Mu_g=((int *)(E->imageData + i*E->widthStep))[j*E->nChannels + 1],
Mu_b=((int *)(E->imageData + i*E->widthStep))[j*E->nChannels + 2];
float
Var_r=((float *)(S->imageData + i*S->widthStep))[j*S->nChannels + 0],
Var_g=((float *)(S->imageData + i*S->widthStep))[j*S->nChannels + 1],
Var_b=((float *)(S->imageData + i*S->widthStep))[j*S->nChannels + 2];
double
a=1/(Mu_r*Mu_r*Var_r+Mu_g*Mu_g*Var_g+Mu_b*Mu_b*Var_b),
b=Mu_r*a*Var_r,
c=Mu_r*a*Var_g,
d=Mu_r*a*Var_b,
Rms_Alpha=0,
Rms_CD=0;
for(int k=0;k<s;k++)
{
CvScalar r=cvScalar(0);
GetVoxel(j,i,k,r);
double
al=b*r.val[0]+c*r.val[1]+d*r.val[2],
cdi=(r.val[0]-al*Mu_r)*(r.val[0]-al*Mu_r)*Var_r
*(r.val[1]-al*Mu_g)*(r.val[1]-al*Mu_g)*Var_g
*(r.val[2]-al*Mu_b)*(r.val[2]-al*Mu_b)*Var_b;
Alpha.push_back(al);
CD.push_back(sqrt(cdi));
Rms_Alpha+=(al-1)*(al-1);
Rms_CD+=cdi;
}
Rms_Alpha=1/sqrt(Rms_Alpha/s);
Rms_CD=1/sqrt(Rms_CD/s);
if(Rms_CD>1.81)
{
Rms_CD=1.818181;
}
int
n=Alpha.size()-s;
for(int i=0;i<s;i++)
{
Alpha[n]=(Alpha[n]-1)*Rms_Alpha;
CD[n]=CD[n]*Rms_CD;
n++;
}
}
}
cvReleaseImage(&E);
cvReleaseImage(&S);
//= ===== Thresholds computing
std::vector<float> X=CD;
int seuil=(int)(rate*X.size()-1);
nth_element ( X.begin(), X.begin()+seuil,X.end());
float
Tcd=X[seuil];
X.clear();
//= ===== Foreground extraction
std::vector<float>::iterator
P1=CD.begin();
for(int i=0;i<h;i++)
for(int j=0;j<w;j++)
for(int k=0;k<s;k++)
{
CvScalar r=cvScalar(0,0,0);
if( *P1>Tcd )
r.val[0]=(r.val[1]=(r.val[2]=255));
else
r.val[0]=(r.val[1]=(r.val[2]=0));
Vout.SetVoxel(j,i,k,r);
P1++;
} |
Partager