代码改变世界

三维高斯模型 opencv实现

2016-04-10 13:56  GarfieldEr007  阅读(606)  评论(0编辑  收藏  举报
  1. OnProbabilityModel()  
  2. {  
  3.     int i;  
  4.     for(int x=0;x<workImg->height;x++)  
  5.     {  
  6.         for(int y=0;y<workImg->width;y++)  
  7.         {  
  8.             //double cur[3];  
  9.             CvMat* cur=cvCreateMat(3,1,CV_32F);  
  10.             for(i=0;i<3;i++){  
  11.                 double tt=((uchar*)(workImg->imageData+x*workImg->widthStep))[y*3+i];  
  12.                 cvmSet(cur,i,0,tt);  
  13.             }  
  14.             CvMat dst=cvRGB2YCbCr(cur);  
  15.             if(CalProbability(WHITE,&dst)<0.1&&CalProbability(YELLOW,&dst)<0.1)  
  16.                 for (i=0;i<3;i++)  
  17.                 ((uchar*)(workImg->imageData+x*workImg->widthStep))[y*3+i]=0;  
  18.         }  
  19.     }  
  20.     Invalidate();  
  21. }  

 

 

[cpp] view plain copy
 
  1. double CalProbability(int classid,CvMat* cur)  
  2. {  
  3.     /************************************************************************/  
  4.     /* function: 
  5.     一个像素点cur[3]={r,g,b}; 它属于classid色类的概率 
  6.     */  
  7.     /************************************************************************/  
  8.     double temp,t1;  
  9.   
  10.      CvMat inv_w,inv_y;  
  11.      cvInitMatHeader(&inv_w,3,3,CV_32F,Inv_white);  
  12.      cvInitMatHeader(&inv_y,3,3,CV_32F,Inv_yellow);  
  13.      CvMat* tmp=cvCreateMat(1,3,CV_32F);  
  14.      CvMat* tmp1=cvCreateMat(1,3,CV_32F);  
  15.      CvMat* res=cvCreateMat(1,1,CV_32F);  
  16.   
  17.     //double tmp[3][3],tmp1[3][3];  
  18.     //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
  19.     temp=1/pow(2*PI,3/2)/sqrt(norm[classid]);  
  20.     //cvmGetMat()  
  21.   
  22.     for (i=0;i<3;i++)    {  
  23.         double x=cvmGet(cur,i,0);  
  24.         x-=mean_ycbcr[classid][i];  
  25.         if(x<0)  
  26.             x=0;  
  27.         cvmSet(cur,i,0,x);  
  28.     }  
  29.   
  30.     double c1=cvmGet(cur,0,0);  
  31.     double c2=cvmGet(cur,1,0);  
  32.     double c3=cvmGet(cur,2,0);  
  33.   
  34.     cvTranspose(cur,tmp);//转置  
  35.   
  36.     if(classid==WHITE)  
  37.         cvmMul(tmp,&inv_w,tmp1);  
  38.     else if(classid==YELLOW)  
  39.         cvmMul(tmp,&inv_y,tmp1);  
  40.   
  41.     cvmMul(tmp1,cur,res);  
  42.     //t1=cvNorm(tmp,0,CV_L1,0);  
  43.     t1=cvmGet(res,0,0);  
  44.     t1*=(-0.5);  
  45.     temp*=pow(Ezhishu,t1);  
  46.   
  47.     return temp;  
  48. }  

from: http://blog.csdn.net/abcjennifer/article/details/7392373