局部二值模式LBP(Local Binary Pattern)實現代碼

本文不介紹LBP的理論知識,只是簡單的說說LBP及其變體的實現:

1)最原始的LBP運算元的實現代碼

[cpp] view plain copy print?

  1. voidlbp(Mat&_src,Mat&_dst)
  2. {
  3. if(-_dst.empty())
  4. _dst.create(_src.rows,_src.cols,CV_8UC1);
  5. _dst=cv::Scalar::all(0);
  6. //calculatepatterns
  7. for(inti=1;i<_src.rows-1;i++)
  8. {
  9. for(intj=1;j<_src.cols-1;j++)
  10. {
  11. ucharcenter=_src.at<uchar>(i,j);
  12. unsignedcharcode=0;
  13. code|=(_src.at<uchar>(i-1,j-1)>=center)<<7;
  14. code|=(_src.at<uchar>(i-1,j)>=center)<<6;
  15. code|=(_src.at<uchar>(i-1,j+1)>=center)<<5;
  16. code|=(_src.at<uchar>(i,j+1)>=center)<<4;
  17. code|=(_src.at<uchar>(i+1,j+1)>=center)<<3;
  18. code|=(_src.at<uchar>(i+1,j)>=center)<<2;
  19. code|=(_src.at<uchar>(i+1,j-1)>=center)<<1;
  20. code|=(_src.at<uchar>(i,j-1)>=center)<<0;
  21. _dst.at<unsignedchar>(i,j)=code;
  22. }
  23. }
  24. }

2)帶有容忍銀子的LBP運算元[cpp] view plain copy print?

  1. voidlbp_with_factor(Mat&_src,Mat&_dst,ucharfactor)//factordefault3
  2. {
  3. if(-_dst.empty())
  4. _dst.create(_src.rows,_src.cols,CV_8UC1);
  5. _dst=cv::Scalar::all(0);
  6. //calculatepatterns
  7. for(inti=1;i<_src.rows-1;i++)
  8. {
  9. for(intj=1;j<_src.cols-1;j++)
  10. {
  11. ucharcenter=_src.at<uchar>(i,j)+factor;
  12. unsignedcharcode=0;
  13. code|=(_src.at<uchar>(i-1,j-1)>=center)<<7;
  14. code|=(_src.at<uchar>(i-1,j)>=center)<<6;
  15. code|=(_src.at<uchar>(i-1,j+1)>=center)<<5;
  16. code|=(_src.at<uchar>(i,j+1)>=center)<<4;
  17. code|=(_src.at<uchar>(i+1,j+1)>=center)<<3;
  18. code|=(_src.at<uchar>(i+1,j)>=center)<<2;
  19. code|=(_src.at<uchar>(i+1,j-1)>=center)<<1;
  20. code|=(_src.at<uchar>(i,j-1)>=center)<<0;
  21. _dst.at<unsignedchar>(i,j)=code;
  22. }
  23. }
  24. }

3)帶有乘性因子的LBP運算元[cpp] view plain copy print?

  1. //這裡需要採用2個字元來編碼,如00、01、10、11
  2. voidlbp_with_factor(Mat&_src,Mat&_dst,floatfactor)//facotrdefault0.1
  3. {
  4. if(-_dst.empty())
  5. _dst.create(_src.rows,_src.cols,CV_8UC1);
  6. _dst=cv::Scalar::all(0);
  7. //calculatepatterns
  8. for(inti=1;i<_src.rows-1;i++)
  9. {
  10. for(intj=1;j<_src.cols-1;j++)
  11. {
  12. ucharcenter=_src.at<uchar>(i,j);
  13. intup=center*(1.0+factor);
  14. intdown=center*(1.0+factor);
  15. unsignedcharcode=0;
  16. /*code|=(_src.at<uchar>(i-1,j-1)>=center)<<7;
  17. code|=(_src.at<uchar>(i-1,j)>=center)<<6;
  18. code|=(_src.at<uchar>(i-1,j+1)>=center)<<5;
  19. code|=(_src.at<uchar>(i,j+1)>=center)<<4;
  20. code|=(_src.at<uchar>(i+1,j+1)>=center)<<3;
  21. code|=(_src.at<uchar>(i+1,j)>=center)<<2;
  22. code|=(_src.at<uchar>(i+1,j-1)>=center)<<1;
  23. code|=(_src.at<uchar>(i,j-1)>=center)<<0;

推薦閱讀:

【近現代繪畫大師】祝大年
現代簡歐
魚龍變化 中國現代交誼舞文化創新運動
現代紳士備忘錄
化妝師MM的家 90平現代美式設計

TAG:代碼 | 現代 | 模式 | 實現 |