Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
matlab图像svd方法到opencv的转换_Opencv_Svd - Fatal编程技术网

matlab图像svd方法到opencv的转换

matlab图像svd方法到opencv的转换,opencv,svd,Opencv,Svd,我想用VisualStudio中的C++编写OpenCV程序。 我的代码是以下matlab代码: close all clear all clc %reading and converting the image inImage=imread('pic.jpg'); inImageD=double(inImage); [U,S,V]=svd(inImageD); % Using different number of singular values (diagonal of S) to c


我想用VisualStudio中的C++编写OpenCV程序。 我的代码是以下matlab代码:

close all
clear all
clc

%reading and converting the image
inImage=imread('pic.jpg');
inImageD=double(inImage);

[U,S,V]=svd(inImageD);

% Using different number of singular values (diagonal of S) to compress and
% reconstruct the image
dispEr = [];
numSVals = [];
for N=5:25:300
  % store the singular values in a temporary var
  C = S;

  % discard the diagonal values not required for compression
  C(N+1:end,:)=0;
  C(:,N+1:end)=0;

  % Construct an Image using the selected singular values
   D=U*C*V';


  % display and compute error
  figure;
  buffer = sprintf('Image output using %d singular values', N)
  imshow(uint8(D));
  title(buffer);
  error=sum(sum((inImageD-D).^2));

  % store vals for display
  dispEr = [dispEr; error];
  numSVals = [numSVals; N];
end
你对此有什么看法?我想将图像保存在文本文件中,并将其从文件检索到Mat数组中。我将这一部分写如下:

Mat image;
FileStorage read_file("pic_file.txt", FileStorage::READ);
read_file["pic"] >> image;
read_file.release(); 
Mat P;
image.convertTo(P, CV_32FC3,1.0/255);
SVD svda(P); //or SVD::compute(P,W,U,V);
但我有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:\\ImagesForTest\\cat.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;
    }
intmain(intargc,char*argv[])
{
//图像矩阵
Mat-img;
Mat结果;
//---------------------------------------------
//
//---------------------------------------------
namedWindow(“源图像”);
姓名(以下简称“结果”);
//以灰度模式加载图像
img=imread(“D:\\ImagesForTest\\cat.bmp”,0);
img.convertTo(img,CV_32FC1,1.0/255.0);
cout这是我的代码:

   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:\\ImagesForTest\\cat.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;
    }
intmain(intargc,char*argv[])
{
//图像矩阵
Mat-img;
Mat结果;
//---------------------------------------------
//
//---------------------------------------------
namedWindow(“源图像”);
姓名(以下简称“结果”);
//以灰度模式加载图像
img=imread(“D:\\ImagesForTest\\cat.bmp”,0);
img.convertTo(img,CV_32FC1,1.0/255.0);

“它不起作用”的信息不足以让我们诊断您的问题。“它不起作用”的信息不足以让我们诊断您的问题。很好,就是这样。谢谢很好,就是这样。谢谢