Segmentation fault: 11 after matrix multiplication

asked 2020-08-10 23:02:00 -0600

updated 2020-08-11 01:25:51 -0600

berak gravatar image

Hi everyone: I am a newbie to OpenCV and try to duplicate the work from matlab to C++. Here is my C++ code below

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 vector 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,U;
    trans = svd_use.t();
    source = svd_use*trans;
    SVDecomp(source, W, U, VT);

    //To make sure the value is the same as matlab
    W = (Mat_<double>(3,3) << W[0][0], 0, 0, 0, W[1][0], 0, 0, 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 cause the  Segmentation fault: 11
    return 0;
}

Here is my matlab code

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);

Could someone know how to solve this situation? Thanks!! The size of trans is 3 * 16032768 (row* col) and U is 3*3, so I am pretty sure that can be multiply.

edit retag flag offensive close merge delete

Comments

opencv version / os / compiler ?

(cannot reproduce your problem)

berak gravatar imageberak ( 2020-08-11 02:32:16 -0600 )edit