阴影补偿(shading compnsation)--去除图像中的光影

[入库:2005年8月19日] [更新:2007年3月24日]

本文简介:选择自 mc43 的 blog

         图1                                            图2 光影
 
通过运算将光影从图1中去掉

实现步骤:
 1.对图像2作平滑去噪声处理 。方法:周围25个像素去平均,得到sp2;
 2.得到sp2中r、g、b各个分量的最大值得到maxr,maxg,maxb;
 3.对图1(p1)的每一个像素:
      p1.r=p1.r/sp2.r*maxr
      p1.g=p1.g/sp2.g*maxg
      p1.b=p1.b/sp2.b*maxb 

代码片断:

class classlenscomp{
private:
 dataprocess * dprgb;// = new dataprocess();
 dataprocess * dpsmsk;

 pixel smooth(pixel *, size_t, size_t);
public:
 //pixel getv1(int, int);
 classlenscomp(dataprocess *, dataprocess *);
 
 void lenscomp(dataprocess* );
};
/////////////////////////////////////////////////////////////
其中dataprocess 是pixel结构的数组
struct pixel
  {
   double r;
   double g; 
   double b;
}

/////////////////////////////////////////////////////////////
classlenscomp::classlenscomp(dataprocess* rgb_in, dataprocess* smsk_in)
{
 dprgb=rgb_in;
 dpsmsk=smsk_in;

}

void classlenscomp::lenscomp(dataprocess* output)
{
 size_t i,j;
 
 pixel *rgbbuf=dprgb->get_rgbdata();
 pixel *smskbuf=dpsmsk->get_rgbdata();
 pixel *outputbuf=output->get_rgbdata();
 
 size_t w=dprgb->get_rgb_width(); //the smsk width is the same as the grb's
 size_t h=dprgb->get_rgb_height(); //the smsk height is the same as the grb's
 
 pixel maxsmsk(0,0,0);    //save the max r,g,b
 pixel* smskbuffer=new pixel[h*w];

 //for(k=0;k<3;k++)
 {
 
  //smoothed smsk;
  for(i=0;i<h;i++)  
   for (j=0;j<w;j++) 
   { 
    //ignore the two circles pixels of the picture.
    //if pixel is in this bound, copy from original
    if ((i<=1) || (i>=h-2) || (j<=1) || (i>=w-2) )
     smskbuffer[i*w+j]=smskbuf[i*w+j];
    else
     smskbuffer[i*w+j]=smooth(smskbuf,i,j);
   
   }// the smsk has been smoothed first
  
  //get maxrgb
  for(j=0;j<w*h;j++)
  {
   if  (maxsmsk.r<smskbuffer[j].r)
    maxsmsk.r =smskbuffer[j].r;
   if  (maxsmsk.g<smskbuffer[j].g)
    maxsmsk.g =smskbuffer[j].g;
   if  (maxsmsk.b<smskbuffer[j].b)
    maxsmsk.b =smskbuffer[j].b;
  }
  
  //
  for(i=0;i<h;i++)
   for (j=0;j<w;j++)
   {
    
    outputbuf[i*w+j].r =rgbbuf[i*w+j].r/smskbuffer[i*w+j].r * maxsmsk.r ;
    outputbuf[i*w+j].g =rgbbuf[i*w+j].g/smskbuffer[i*w+j].g * maxsmsk.g ;
    outputbuf[i*w+j].b =rgbbuf[i*w+j].b/smskbuffer[i*w+j].b * maxsmsk.b ;
    
   }
 

 }
 
 delete[] smskbuffer;
  
}
///////////////////////////////////////////////////////////////////////////////


pixel classlenscomp::smooth(pixel* rgbbuf, size_t x, size_t y)
{
 size_t i,j;
 size_t w=dprgb->get_rgb_width(); //the smsk width is the same as the grb's
 pixel sum=(0,0,0);
 for(i=x-2;i<=x+2;i++)
  for (j=y-2;j<=y+2;j++)
  {
   sum.r =sum.r +rgbbuf[i*w+j].r ;
   sum.g =sum.g +rgbbuf[i*w+j].g ;
   sum.b =sum.b +rgbbuf[i*w+j].b ;
  }
  sum.r=sum.r/25;
  sum.g=sum.g/25;
  sum.b=sum.b/25;
 return sum;
}
///////////////////////////////////////////////////////////////////////////////
结果图片

结果图片有点过亮, 再校正一下即可

本文关键:阴影补偿(shading compnsation)--去除图像中的光影
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top