openCV中的saturate_cast()方法

在OpenCV学习中经常看见saturate_cast的使用,下面的代码会展示它的作用,详细的代码可以参见文章http://blog.csdn.net/mjlsuccess/article/details/12400787


   
   
   
   
  1. //使用图像混合例子中的C语言版本演示
  2. for ( int i= 0; i
  3. {
  4. const uchar* src1_ptr = src1.ptr(i);
  5. const uchar* src2_ptr = src2.ptr(i);
  6. uchar* dst_ptr = dst.ptr(i);
  7. for ( int j= 0; j
  8. {
  9. dst_ptr[j] = saturate_cast(src1_ptr[j]*alpha + src2_ptr[j]*beta + gama); //gama = -100, alpha = beta = 0.5
  10. // dst_ptr[j] = (src1_ptr[j]*alpha + src2_ptr[j]*beta + gama);
  11. }
  12. }
  13. imshow( "output2",dst);

   
   
   
   

这里加入了溢出保护,结果如下


    
    
    
    
  1. //没加入溢出保护
  2. for ( int i= 0; i
  3. {
  4. const uchar* src1_ptr = src1.ptr(i);
  5. const uchar* src2_ptr = src2.ptr(i);
  6. uchar* dst_ptr = dst.ptr(i);
  7. for ( int j= 0; j
  8. {
  9. // dst_ptr[j] = saturate_cast(src1_ptr[j]*alpha + src2_ptr[j]*beta + gama);//gama = -100, alpha = beta = 0.5
  10. dst_ptr[j] = (src1_ptr[j]*alpha + src2_ptr[j]*beta + gama);
  11. }
  12. }
  13. imshow( "output2",dst);


大致的原理应该如下


   
   
   
   
  1. if(data< 0)
  2. data= 0;
  3. else if(data> 255)
  4. data= 255;




你可能感兴趣的:(OpenCV)