Search
 
SCRIPT & CODE EXAMPLE
 

CPP

C++ with SVD

   int main(int argc, char* argv[])
    {
        // Image matrix
        Mat img;
        Mat result;
        //---------------------------------------------
        //
        //---------------------------------------------
        namedWindow("Source Image");

        namedWindow("Result");
        // Load image in grayscale mode
        img=imread("D:ImagesForTestcat.bmp",0);
        img.convertTo(img,CV_32FC1,1.0/255.0);
        cout << "Source size:" << img.rows*img.cols <<" elements "<< endl;
        // create SVD 
        cv::SVD s;
        // svd result
        Mat w,u,vt;
        // computations ...
        s.compute(img,w,u,vt);

        // collect Sigma matrix (diagonal - is eigen values, other - zeros)
        // we got it in as vector, transform it to diagonal matrix
        Mat W=Mat::zeros(w.rows,w.rows,CV_32FC1);       
        for(int i=0;i<w.rows;i++)
        {
            W.at<float>(i,i)=w.at<float>(i);
        }

        // reduce rank to k
        int k=25;
        W=W(Range(0,k),Range(0,k));
        u=u(Range::all(),Range(0,k));
        vt=vt(Range(0,k),Range::all());

        // Get compressed image
        result=u*W*vt;
        cout << "Result size:" << u.rows*u.cols+k+vt.rows*vt.cols <<" elements "<< endl;
        //---------------------------------------------
        //
        //--------------------------------------------- 
        imshow("Source Image", img);
        imshow("Result", result);
        cvWaitKey(0);
        return 0;
    }
Comment

PREVIOUS NEXT
Code Example
Cpp :: c++ insert vector into vector 
Cpp :: bash script add another user 
Cpp :: glm multiply vector by scalar 
Cpp :: Character convert c++ 
Cpp :: How to clear keyboard buffer c++ 
Cpp :: Studying Alphabet codechef solution in c++ 
Cpp :: std::random_device 
Cpp :: how the theam are store in database 
Cpp :: c + + to c converter 
Cpp :: cuda allocate memory 
Cpp :: C++ Vector Initialization method 02 
Cpp :: for in c++ example 
Cpp :: 123213 
Cpp :: // A C++ program to show that we can use reference to 
Cpp :: calculate number of edges of graph in data structure c++ 
Cpp :: font family slick 
Cpp :: object as a function argument and returning object 
Cpp :: 496. Next Greater Element I.cpp 
Cpp :: inversed priority queue 
Cpp :: bullet physics directx 11 
Cpp :: c++ install 
Cpp :: c++ map key exists 
Cpp :: add for input output file in c/c++ 
Cpp :: C++ Counting 
Cpp :: how to get part from the vector cpp 
C :: remix icon cdn 
C :: conio.h linux 
C :: save numpy array to text file 
C :: close file in c 
C :: arduino client disconnect 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =