C + + version of the LLC code __c++

Source: Internet
Author: User
Tags mul

Image Sparse Coding Summary: LLC and SCSPM , the article gives a very detailed explanation of sparse coding.

The author of "Locality-constrained Linear coding for Image Classification" provides MATLAB code implementation, see HTTP://WWW.IFP.ILLINOIS.EDU/~JYANG29 /llc.htm.

The following is based on the author's code, implemented by OPENCV, the C + + version of the LLC:

Matlab code:www.ifp.illinois.edu/~jyang29/llc.htm

<span style= "FONT-SIZE:12PX;"  
      > Cv::mat bowmodel::findknn (Cv::mat &codebook, Cv::mat &input, int k) {int nbase = codebook.rows;  
      int nquery = input.rows;  
      Mat II = INPUT.MUL (input);  
        
      Mat cc = Codebook.mul (codebook);  
      Mat SII (NQUERY,1,CV_32FC1);  
      Sii.setto (0);  
      Mat SCC (NBASE,1,CV_32FC1);  
      Scc.setto (0);  for (int i = 0; i<ii.rows; i++) {for (int j = 0; j<ii.cols; j + +) {sii.at<float> (i,0)  
        + + ii.at<float> (i,j); (int i = 0; i<cc.rows; i++) {for (int j = 0; j<cc.cols; + +) {SCC.  
        At<float> (i,0) + = cc.at<float> (i,j);  
      } Mat D (NQUERY,NBASE,CV_32FC1); for (int i = 0; i<nquery; i++) {for (int j = 0; j<nbase; j + +) {d.at<float> (i,j) = Si  
        I.at<float> (i,0);  
  } Mat CT;    Transpose (codebook, CT);  
        
      Mat D1 = 2*input*ct;  
      Mat Scct;  
      Transpose (SCC, SCCT);  
      Mat D2 (Nquery, Nbase, CV_32FC1); for (int i = 0; i<nquery; i++) {for (int j = 0; j<nbase; j + +) {d2.at<float> (i,j) = s  
        Cct.at<float> (0,J);  
      } D = D-d1 + D2;  
      Mat SD;  
      Sortidx (D, SD, cv_sort_every_row+cv_sort_ascending);  
      Mat IDX (NQUERY,K,CV_8UC1); for (int i = 0; i<nquery; i++) {for (int j = 0; j<k; j + +) {idx.at<uchar> (i,j) = SD.R  
        ow (i). Col (j) .at<uchar> (0,0);  
      } ii.release ();  
      Cc.release ();  
      Sii.release ();  
      Scc.release ();  
      D.release ();  
      Ct.release ();  
      D1.release ();  
      Scct.release ();  
      D2.release ();  
      Sd.release ();  
    return IDX; } Cv::mat Bowmodel::llccode (Cv::mat &codebook, Cv::mat &amP;input, Cv::mat IDX, int k) {int nquery = input.rows;  
        int nbase = codebook.rows;  
      
        int dim = Codebook.cols;  
        Mat II = Mat::eye (k, K, CV_32FC1);  
        Mat Coeff (NQUERY,NBASE,CV_32FC1);  
        Coeff.setto (0);  
        Mat Z;  
        Mat Z1 (K,DIM,CV_32FC1);  
        Mat Z2 (K,DIM,CV_32FC1);  
        Mat C;  
        Mat un (K,1,CV_32FC1);  
        Un.setto (1);  
        Mat temp;  
        Mat Temp2;  
        Mat W;  
      
        Mat wt; for (int n = 0; n<nquery; n++) {for (int i = 0; i<k; i++) {for (int j = 0; J<di M  
                    J + +) {z1.at<float> (i,j) = Codebook.at<float> (idx.at<uchar> (N,i), j);  
                Z2.at<float> (i,j) = input.at<float> (N,J);  
            }} z = z1-z2;  
            Transpose (z, temp);  
            C = z*temp;  
    c = C + ii* (1e-4) *trace (c) [0];        Invert (C,TEMP2);  
            W = Temp2*un;  
            float sum_w=0;  
            for (int i = 0; i<k; i++) {sum_w + = w.at<float> (i,0);  
            } w = w/sum_w;  
            Transpose (w, WT); for (int i = 0; i<k; i++) {coeff.at<float> (n,idx.at<uchar> (n,i)) = wt.at<float> (0,  
            i);  
        } ii.release ();  
        Z.release ();  
        Z1.release ();  
        Z2.release ();  
        C.release ();  
        Un.release ();  
        Temp.release ();  
        Temp2.release ();  
        W.release ();  
      
        Wt.release ();  
    return Coeff; } </span>


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.