c 2b 2b with svd

Solutions on MaxInterview for c 2b 2b with svd by the best coders in the world

showing results for - "c 2b 2b with svd"
Maria
16 Mar 2016
1   int main(int argc, char* argv[])
2    {
3        // Image matrix
4        Mat img;
5        Mat result;
6        //---------------------------------------------
7        //
8        //---------------------------------------------
9        namedWindow("Source Image");
10
11        namedWindow("Result");
12        // Load image in grayscale mode
13        img=imread("D:\\ImagesForTest\\cat.bmp",0);
14        img.convertTo(img,CV_32FC1,1.0/255.0);
15        cout << "Source size:" << img.rows*img.cols <<" elements "<< endl;
16        // create SVD 
17        cv::SVD s;
18        // svd result
19        Mat w,u,vt;
20        // computations ...
21        s.compute(img,w,u,vt);
22
23        // collect Sigma matrix (diagonal - is eigen values, other - zeros)
24        // we got it in as vector, transform it to diagonal matrix
25        Mat W=Mat::zeros(w.rows,w.rows,CV_32FC1);       
26        for(int i=0;i<w.rows;i++)
27        {
28            W.at<float>(i,i)=w.at<float>(i);
29        }
30
31        // reduce rank to k
32        int k=25;
33        W=W(Range(0,k),Range(0,k));
34        u=u(Range::all(),Range(0,k));
35        vt=vt(Range(0,k),Range::all());
36
37        // Get compressed image
38        result=u*W*vt;
39        cout << "Result size:" << u.rows*u.cols+k+vt.rows*vt.cols <<" elements "<< endl;
40        //---------------------------------------------
41        //
42        //--------------------------------------------- 
43        imshow("Source Image", img);
44        imshow("Result", result);
45        cvWaitKey(0);
46        return 0;
47    }
48
similar questions
queries leading to this page
c 2b 2b with svd