For some reason - matrix multiplication crashes [closed]

asked 2018-09-23 16:13:39 -0600

IgalO gravatar image

I know this might have some trivial solution, and I might feel like an idiot, but I've spent hours on this and I just don't get what's wrong.

I am supposed to calculate a matrix, Q, according to an existing algorithm. I broke down the initial formulate to the most basic operations to debug end see where it fails - it fails on the 2nd step.

    Mat Ui = Mat::eye(4,4, CV_32F);
    Mat ui = Mat(4, 1, CV_32F);
    Mat P = Mat(2, 4, CV_32F);

    //Calculate Ui
    //Populate ui from an existing vector (sorry for the names.. it makes sense with context I swear)
    //Initialize P

    //Calculate Q according to the algorithm
    std::cout << "P: " << P.rows << "x" << P.cols << std::endl;
    std::cout << "Ui: " << Ui.rows << "x" << Ui.cols << std::endl;
    this->Q = Ui * P.t();
    std::cout << "here0 - Q: "<< this->Q.rows<<"x"<< this->Q.cols<< std::endl;
    this->Q = P * this->Q;
    std::cout << "here1 - Q: " << this->Q.rows << "x" << this->Q.cols << std::endl;
    this->Q = this->Q -(P * ui)*((P * ui).t());
    std::cout << "here2 - Q: " << this->Q.rows << "x" << this->Q.cols << std::endl;
    this->Q = this->Q.inv();
    std::cout << "here3 - Q: " << this->Q.rows << "x" << this->Q.cols << std::endl;
    this->Q = this->Q *(1.f / 4);

The debug shows:

P: 2x4
Ui: 4x4

Ui:
0.25 0 0 0
0 0.25 0 0
0 0 0.25 0
0 0 0 0.25
P:
23 45
85 22
72 91
24 22

ui: (0.25 0.25 0.25 0.25)

here0 - Q: 4x2

This is where it crushes. I have no idea what's wrong. It was earlier written as 1 line but I wrote it this way to try to debug.

edit retag flag offensive reopen merge delete

Closed for the following reason question is not relevant or outdated by IgalO
close date 2018-09-24 07:12:41.928721

Comments

Also -

    Mat tempMat1 = Mat::zeros(2, 4, CV_32FC1);
    Mat tempMat2 = Mat::zeros(4, 2, CV_32FC1);

    this->Q = tempMat1 * tempMat2 ;

Crashes, while

    Mat tempMat1 = Mat::zeros(2, 3, CV_32FC1);
    Mat tempMat2 = Mat::zeros(3, 2, CV_32FC1);

    this->Q = tempMat1 * tempMat2 ;

Works perfectly. What gives?

IgalO gravatar imageIgalO ( 2018-09-23 19:29:01 -0600 )edit

Made a function myself - a basic, not optimal multiplication function:

    if (m1.cols != m2.rows)
        throw new std::exception("Matrix multiplication failed - dimentions need to be m1:a x b, m2: b x c");
    if (m1.type() != m2.type())
        throw new std::exception("Cannot multiply matrices of different types.");
    //Result matrix
    Mat res = Mat::zeros(m1.rows, m2.cols, CV_32F);

    //The most basic matrix mul
    for (int i = 0; i < m1.rows; i++) {
        for (int j = 0; j < m2.cols; j++) {
            for (int k = 0; k < m1.cols; k++) { //Coul also be k<m2.rows
                res.at<float>(Point(i, j)) += m1.at<float>(Point(i, k)) * m2.at<float>(Point(k, j));
            }
        }
    }

    return res;

For some reason - still doesn't work with 2x4 | 4x2, citing some access violation.

PLEASE HELP!

IgalO gravatar imageIgalO ( 2018-09-23 20:32:40 -0600 )edit

I am doing some matrix calculations with the openCV Mat. Though, it is not the best to do it with. Can you try this for me? Perhaps it works: cv::Mat Q = Ui * cv::Mat(P.t());

Grillteller gravatar imageGrillteller ( 2018-09-24 03:58:01 -0600 )edit

res.at<float>(Point(i, j)) += m1.at<float>(Point(i, k)) * m2.at<float>(Point(k, j));

please DONT write loops like this, it's slow and error prone (you have i and j in reverse)

i also can't reproduce any of your crashes (with master branch)

please try to show us the algorithm you're trying to implement, else this is some XY problem.

berak gravatar imageberak ( 2018-09-24 06:10:10 -0600 )edit

@berak I've written it at around 4am just to test if it produces the same crash. Yeah it's not going into anything other than testing.
Anyway I've made progress in the debugging, turns out the crash is not directly related to multiplication or dim, but rather somehow crashes due another code snippet. I'll mark the question as solved.

Ps: Just took a look - where is the error in the matrix multiplication? The index 0,0 corresponds to 0-th row of m1 * 0th col of m2, the 0,1 index corresponds to the 0th row of m1 * 1st row of m2, etc. Why are you saying it's in reverse?

IgalO gravatar imageIgalO ( 2018-09-24 07:12:25 -0600 )edit

it is Mat.at(y,x) or Mat.at(Point(x,y)) and your 'i' counts over rows.

berak gravatar imageberak ( 2018-09-24 07:16:32 -0600 )edit
1

Thanks for this reply.
I've actually found it out in the last 15 minutes - that's what was responsible for my bug, as my earlier function had Mat.at(x,y) then I changed it all to Mat.at(Point(x,y)) to be consistent with the rest of the code, but forgot to switch the x's and y's -_-
Oh well..

IgalO gravatar imageIgalO ( 2018-09-24 07:33:53 -0600 )edit