Massive Multiplication by OpenCV Elements

OpenCV docs say A.mul(B)- multiplication by each element. However, the following code produces the following output and then gives this error:

OpenCV Error: Sizes of input arguments do not match

.

cout << laplacian_pyramids[i][numLevels - 1 - l].rows << endl;
cout << gaussian_weight_pyramids[i][l].rows << endl;
cout << laplacian_pyramids[i][numLevels - 1 - l].cols << endl;
cout << gaussian_weight_pyramids[i][l].cols << endl;

gives:

339
339
571
571

Then:

Mat prod = gaussian_weight_pyramids[i][l].mul(laplacian_pyramids[i][numLevels - 1 - l]);

gives an error. I tried a Mat::multiplysimilar effect.

+4
source share
2 answers

I would recommend converting one channel to three channels:

    Mat A = Mat::zeros(100, 200, CV_32FC1);
    Mat B = Mat::zeros(100, 200, CV_32FC3);

    // Mat C = A.mul(B); // Sizes of input arguments do not match

    Mat Afc3;
    Mat t[] = {A, A, A};
    merge(t, 3, Afc3);

    Mat C = Afc3.mul(B); // now Afc3 has 3 channels ans it is type of 32_FC3 
                         // we can multiply each elem in B by the same coef from A

But if B is a type CV_8UC3, it does not work, because opencv will not allow multiplying Mats that have different types of pixels. In this case, convert 8UC3 to 32FC3 to convert each pixel to 1 / 255.0, each pixel in 32FC3 has a value from 0.0 to 1.0 (and, of course, each pixel in 8UC3 has a value from 0 to 255).

    Mat A = Mat::zeros(100, 200, CV_32FC1);
    Mat B = Mat::zeros(100, 200, CV_8UC3);

    // Mat C = A.mul(B);

    Mat Afc3, Bfc3;
    Mat t[] = {A, A, A};
    merge(t, 3, Afc3);

    B.convertTo(Bfc3, CV_32FC3, 1/255.0);

    Mat C = Afc3.mul(Bfc3);
+4

: (, unsigned char, unsigned short). , . , 3 , , :

1) 'for', . , OpenCV, . , .

2) , 'merge' 'convertTo', . . , @marol . . . , . , .

3) . , , , reshape :

// two matrices of same size but different number of channels
Mat laplac(100, 200, CV_32FC3);
Mat gauss(100, 200, CV_32FC1);

// turn them into single channel matrices. they have NxM rows and 1 or 3 columns.
// note that there no copy of data. any change in them will affect original matrices
Mat laplac2 = laplac.reshape( 1, laplac.rows*laplac.cols );
Mat gauss2 = gauss.reshape( 1, gauss.rows*gauss.cols ;

// perform multiplication
laplac2.col(0) = laplac2.col(0).mul(gauss2);
laplac2.col(1) = laplac2.col(1).mul(gauss2);
laplac2.col(2) = laplac2.col(2).mul(gauss2);

, OpenCV . , , -1, -1 .

, : (

+2

Source: https://habr.com/ru/post/1538142/


All Articles