OpenCV / C ++-分段错误:mat乘法后为11

问题描述

我是OpenCV的新手,正在尝试将代码从Matlab复制到C ++ 并有细分错误:11进行矩阵乘法时。 我发现这可能是由C ++代码中的trans*U引起的。 trans的大小为16032768x3(行x col),U的大小为3x3,因此,我很确定可以将其相乘。

这是我的照片的链接Photos

希望有人可以帮助我解决我的问题,谢谢!

这是我的C ++代码

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>  

using namespace std;
using namespace cv;

int main(int argc,char const *argv[])
{
    //import image
    Mat_<double> img1,img2,img3;
    img1 = imread("S1.jpg",IMREAD_GRAYSCALE);
    img2 = imread("S2.jpg",IMREAD_GRAYSCALE);
    img3 = imread("S3.jpg",IMREAD_GRAYSCALE);
    
    //Push all the image it to one matrix for SVDecomp
    Mat_<double> svd_use;
    svd_use.push_back(img1.reshape(0,1));
    svd_use.push_back(img2.reshape(0,1));
    svd_use.push_back(img3.reshape(0,1));
    
    
    Mat_<double> source,trans,B,W,U,VT;
    trans = svd_use.t();
    source = svd_use*trans;
    SVDecomp(source,VT);
    
    //To make sure the value is the same as matlab
    W = (Mat_<double>(3,3) << W[0][0],W[1][0],W[2][0]);
    U = (Mat_<double>(3,3) << -U[0][0],-U[0][1],U[0][2],-U[1][0],-U[1][1],U[1][2],-U[2][0],-U[2][1],U[2][2]); 
    B = trans*U;//<---this part causes the  Segmentation fault: 11

    return 0;
}

这是我的Matlab代码

%Import images
source_img1 = rgb2gray(imread('S1.JPG'));
source_img2 = rgb2gray(imread('S2.JPG'));
source_img3 = rgb2gray(imread('S3.JPG'));

%Vectorize images
img_vector1 = source_img1(:);
img_vector2 = source_img2(:);
img_vector3 = source_img3(:);


%Calculate SVD
t = double([img_vector1'; img_vector2'; img_vector3']);
[U,S,V] = svd(t*t');
B = t'*V*S^(-1/2);

此外,我对矩阵的功效提出了疑问:在Matlab中,我可以直接计算S^(1/2), 有什么办法可以在OpenCV中做同样的事情?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)