node-opencv icon indicating copy to clipboard operation
node-opencv copied to clipboard

How to dot multiply and add two matrix

Open ahmed-ehsan opened this issue 11 years ago • 6 comments

Hi, Is there any way to just dot multiply two matrices AND is there any way ti simply add two matrices?

Thanks, Ahmed Ehsan

ahmed-ehsan avatar Dec 09 '14 13:12 ahmed-ehsan

You can add two matrices with addWeighted (set weights to 1.0 for both)

cv.readImage("image1.jpg", function(err, image1)
{
  if (err) throw err;
  cv.readImage("image2.jpg", function(err, image2)
  {
    if (err) throw err;
    var sum = new cv.Matrix(image1.width(), image1.height());
    sum.addWeighted(image1, 1.0, image2, 1.0);
    sum.save("sum.jpg");
  });
});

salmanulhaq avatar Dec 09 '14 14:12 salmanulhaq

But what about dot multiplication? On 9 Dec 2014 19:22, "Salman Ul Haq" [email protected] wrote:

You can add two matrices with addWeighted (set weights to 1.0 for both)

cv.readImage("image1.jpg", function(err, image1) { if (err) throw err; cv.readImage("image2.jpg", function(err, image2) { if (err) throw err; var sum = new cv.Matrix(image1.width(), image1.height()); sum.addWeighted(image1, 1.0, image2, 1.0); sum.save("sum.jpg"); }); });

— Reply to this email directly or view it on GitHub https://github.com/peterbraden/node-opencv/issues/198#issuecomment-66289100 .

ahmed-ehsan avatar Dec 09 '14 14:12 ahmed-ehsan

It's fairly simply, though not available yet. I'll add image arithmetic ops, check back in a few days :)

salmanulhaq avatar Dec 09 '14 16:12 salmanulhaq

@salmanulhaq Is it ready now? I am working on an image segmentation program, so I want to multiply a matrix by a scalar. I have used a for loop but it ends up in a segmentation fault if I do several multiplications.

for (i = 0; i <= width; i++) {
    for (j = 0; j <= height; j++) {
        var prod = mat1.get(i, j) * scalar1;
        mat1.set(i, j, prod);
        var prod = mat2.get(i, j) * scaler2;
        mat2.set(i, j, prod);
    }
}

And the console result is: [1] 7190 segmentation fault node segmentation.js Is there other options to solve this problem? Thanks in advance!

marcosgilf avatar Jun 26 '16 20:06 marcosgilf

@marcos90gil did you figure it out?

It could probably be added in easily: http://docs.opencv.org/2.4/modules/core/doc/basic_structures.html?highlight=dot#mat-dot

danschultzer avatar Sep 23 '16 03:09 danschultzer

Please i need the same!! Anyone could solve this?

mariantonia avatar Aug 01 '17 20:08 mariantonia