[Solved] How to multiply Mat A * B?


this is to improve concept

    Mat A = (Mat_<float>(3, 4) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 0.1, 0.1, 0.3);
    Mat B = (Mat_<float>(3, 4) << 1, 2, 3, 4, 5, 6, 7, 8, 9, 0.1, 0.1, 0.3);

    Mat AB0;
    multiply(A, B, AB0);
    cout << A << endl;
    cout << B << endl;
    cout << AB0 << endl;
    Mat AB1 = Mat(A.size(), CV_32FC1);

        for (int x = 0; x < B.cols; x++)
        {
            for (int y = 0; y < B.rows; y++)
            {
                AB1.at<float>(y, x) = A.at<float>(y, x) * B.at<float>(y, x);
            }
        }

    cout << AB1 << endl;

this should work to you

Mat A, B, AB, src;
src = imread("as.jpg", CV_LOAD_IMAGE_GRAYSCALE);
Sobel(src, A, CV_32FC1, 1, 0, 3, BORDER_DEFAULT);
Sobel(src, B, CV_32FC1, 0, 1, 3, BORDER_DEFAULT);

AB = Mat(A.size(), CV_32FC1);

for (int x = 0; x < B.cols; x++)
{
    for (int y = 0; y < B.rows; y++)
    {
        AB.at<float>(y, x) = A.at<float>(y, x) * B.at<float>(y, x);
    }
}


namedWindow("AB");
imshow("AB", AB);
waitKey();

but keep in mind that this code will work slow. you need use pointers to speed up the process

4

solved How to multiply Mat A * B?