图像滤镜的一些加速和改进--笔记

原文:http://blog.youkuaiyun.com/zwlq1314521/article/details/50455782

滤镜有很多开源代码包,imageshop,tinyimage,还有安卓的源码包,看过安卓的源码包,感觉还有很大的提升空间.

http://blog.csdn.NET/jingwen3699/article/details/7770287  这个博客有介绍,看看你就知道,至少针对图像遍历像素有很多加速的方法可用.

列出我整理出的速度最快的几个,基于OpenCV

[cpp]  view plain  copy
  1. /*adjust brightness and contract 
  2. 查找表 
  3.  */  
  4. void cast_LUT(const cv::Mat& src, cv::Mat& dst,unsigned char(&r_lookup)[256],unsigned char(&g_lookup)[256],  
  5.                  unsigned char(&b_lookup)[256],IMAGE_CHANNELMODE mode)  
  6. {  
  7.     uchar* srcData = src.data;  
  8.     if(dst.empty()){  
  9.     dst.create(src.size(),src.type());  
  10.     }  
  11.     uchar* dstData = dst.data;  
  12.     int size = src.rows * src.cols * 3;  
  13.     switch (mode){  
  14.     case IMAGE_CHANEL_RGB:  
  15.     {  
  16. //经测试,这种遍历像素的方法时最快的,而且比较安全  
  17.  for (int i = 0; i < size-3; i+=3)  
  18.             {  
  19.   
  20.                 dstData[i] = b_lookup[srcData[i]];  
  21.                 dstData[i+1] = g_lookup[srcData[i+1]];  
  22.                 dstData[i+2] = r_lookup[srcData[i+2]];  
  23.   
  24.         }  
  25.          break;  
  26.     }  
  27.     case IMAGE_CHANEL_B:  
  28.     {  
  29.         for (int i = 0; i < size-3; i+=3)  
  30.             {  
  31.                 dstData[i] = b_lookup[srcData[i]];  
  32.         }  
  33.         break;  
  34.     }  
  35.     case IMAGE_CHANEL_G:  
  36.     {  
  37.         for (int i = 0; i < size-3; i+=3)  
  38.             {  
  39.                 dstData[i] = g_lookup[srcData[i+1]];  
  40.         }  
  41.         break;  
  42.     }  
  43.     case IMAGE_CHANEL_R:  
  44.     {  
  45.         for (int i = 0; i < size-3; i+=3)  
  46.             {  
  47.                 dstData[i] = r_lookup[srcData[i+2]];  
  48.         }  
  49.         break;  
  50.     }  
  51.   
  52.     }  
  53.   
  54. }  

[cpp]  view plain  copy
  1. //这个是利用opencv里的LUT查找表来实现的亮度和对比度调节  
  2. void adjustBrightContract(const cv::Mat &src, cv::Mat &dst, float brightFactor, float contractFactor)  
  3. {  
  4.     IMAGE_ASSERT_VOID(brightFactor >= -1.0 && brightFactor <= 1.0);  
  5.     IMAGE_ASSERT_VOID(contractFactor >= -1.0 && contractFactor <= 1.0);  
  6.     float delta = 1 + brightFactor;  
  7.   
  8.     float beta = 1 + contractFactor;  
  9.     cv::Mat lookup(1,256,CV_8UC1);  
  10.     uchar* pData = lookup.data;  
  11. //生成查找表  
  12.  for(int i = 0;i < 256;i++)  
  13.     {  
  14.         pData[i] = cv::saturate_cast<uchar>(i * delta);//bright  
  15.         pData[i] = cv::saturate_cast<uchar>(thresholdc + (i - thresholdc) * beta);//contract  
  16.     }  
  17.     cv::LUT(src,lookup,dst);  
  18. }  
  19. //接下来时自动色阶的  
  20. void adjustcolorLevel(cv::Mat& src,cv::Mat& dst,int blackThreshold,int whiteThreshold,float gamma,IMAGE_CHANNELMODE mode)  
  21. {  
  22.     IMAGE_ASSERT_VOID(blackThreshold>= 0 && whiteThreshold>blackThreshold && whiteThreshold<=255);  
  23.     IMAGE_ASSERT_VOID(gamma >= 0.0 && gamma <= 10.0);  
  24.      unsigned char lookup[256];  
  25.    // cv::Mat lookup(1,256,CV_8UC1);  
  26.     uchar*pData = lookup;  
  27.     //smaller than blackThreshold will be set to 0  
  28.     for (int i = 0; i < blackThreshold; i ++)  
  29.     {  
  30.         pData[i] = 0;  
  31.     }  
  32.    //between using gamma correct  
  33.     float ig = (gamma == 0.0) ? 0.0 : 1 / gamma;  
  34.     float threshold = (float)(whiteThreshold - blackThreshold);  
  35.     for (int i = blackThreshold; i < whiteThreshold; i++)  
  36.     {  
  37.         pData[i] = cv::saturate_cast<uchar>( pow((i-blackThreshold)/threshold,ig)*255);  
  38.     }  
  39.    //big than whiteThreshold will be set to 255  
  40.     for (int i = whiteThreshold; i < 256; i++)  
  41.     {  
  42.         pData[i] = 255;  
  43.     }  
  44. //自己写的LUT  
  45.  cast_LUT(src,dst,lookup,lookup,lookup,mode);  
  46.     //cv::LUT(src,lookup,dst);  
  47. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值