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

來源:互聯網
上載者:User

            局部二值模式LBP由於其簡單、高效在目標檢測、目標識別、映像檢索等領域得到了廣泛的應用。現如今,LBP的變體不下上十種,如VLBP、SILBP等。要瞭解更多關於LBP的知識可以參考:紋理分類-全域特徵LBP及相關文獻。

          下面介紹的三種LBP運算元的區別見:


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

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

void lbp( Mat& _src, Mat& _dst) {if ( -_dst.empty())_dst.create(_src.rows, _src.cols, CV_8UC1);_dst = cv::Scalar::all(0);// calculate patternsfor(int i=1;i<_src.rows-1;i++){for(int j=1;j<_src.cols-1;j++) {uchar center = _src.at<uchar>(i,j);unsigned char code = 0;code |= (_src.at<uchar>(i-1,j-1) >= center) << 7;code |= (_src.at<uchar>(i-1,j) >= center) << 6;code |= (_src.at<uchar>(i-1,j+1) >= center) << 5;code |= (_src.at<uchar>(i,j+1) >= center) << 4;code |= (_src.at<uchar>(i+1,j+1) >= center) << 3;code |= (_src.at<uchar>(i+1,j) >= center) << 2;code |= (_src.at<uchar>(i+1,j-1) >= center) << 1;code |= (_src.at<uchar>(i,j-1) >= center) << 0;_dst.at<unsigned char>(i,j) = code;}}}

         2)帶有容忍銀子的LBP運算元

void lbp_with_factor( Mat& _src, Mat& _dst, uchar factor) // factor default 3{if ( -_dst.empty())_dst.create(_src.rows, _src.cols, CV_8UC1);_dst = cv::Scalar::all(0);// calculate patternsfor(int i=1;i<_src.rows-1;i++){for(int j=1;j<_src.cols-1;j++) {uchar center = _src.at<uchar>(i,j)+factor;unsigned char code = 0;code |= (_src.at<uchar>(i-1,j-1) >= center) << 7;code |= (_src.at<uchar>(i-1,j) >= center) << 6;code |= (_src.at<uchar>(i-1,j+1) >= center) << 5;code |= (_src.at<uchar>(i,j+1) >= center) << 4;code |= (_src.at<uchar>(i+1,j+1) >= center) << 3;code |= (_src.at<uchar>(i+1,j) >= center) << 2;code |= (_src.at<uchar>(i+1,j-1) >= center) << 1;code |= (_src.at<uchar>(i,j-1) >= center) << 0; _dst.at<unsigned char>(i,j) = code;}}}

         3)帶有乘性因子的LBP運算元

// 這裡需要採用2個字元來編碼,如00、01、10、11void lbp_with_factor( Mat& _src, Mat& _dst, float factor)  // facotr default 0.1{if ( -_dst.empty())_dst.create(_src.rows, _src.cols, CV_8UC1);_dst = cv::Scalar::all(0);// calculate patternsfor(int i=1;i<_src.rows-1;i++){for(int j=1;j<_src.cols-1;j++) {uchar center = _src.at<uchar>(i,j);int up = center*(1.0+factor);int down = center*(1.0+factor);unsigned char code = 0;/*code |= (_src.at<uchar>(i-1,j-1) >= center ) << 7;code |= (_src.at<uchar>(i-1,j) >= center) << 6;code |= (_src.at<uchar>(i-1,j+1) >= center) << 5;code |= (_src.at<uchar>(i,j+1) >= center) << 4;code |= (_src.at<uchar>(i+1,j+1) >= center) << 3;code |= (_src.at<uchar>(i+1,j) >= center) << 2;code |= (_src.at<uchar>(i+1,j-1) >= center) << 1;code |= (_src.at<uchar>(i,j-1) >= center) << 0; _dst.at<unsigned char>(i,j) = code;*/}}}

        欲瞭解更多參考論文:Modeling Pixel Process with Scale Invariant Local Patterns for Background Subtraction in Complex Scenes

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.