justadudewhohacks/opencv4nodejs

How to use a Javascript image as input?

Closed this issue · 2 comments

I have a Javascript image object, how can I use this or convert this for use with this opencv4nodejs?
I mean really specifically a Javascript image https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/Image

This is what I have so far, my image I can get the buffer and then what to do?... the following does not work, just a blank image:

      const width = 1000;
      const height = 800;
      const buf: Buffer = image.getBitmap();
      const buf2: Buffer = buf.slice(4 * width * dirty.y, 4 * width * (dirty.y + dirty.height));
      const myMat: Mat = new Mat(width, height, CV_8UC4);
      buf.copy(myMat.getData());
      imwrite('./testit.png', myMat);


I think I answered my own question since this works.

      const width: number = 1000;
      const height: number = 800;
      const buf: Buffer = image.getBitmap();
      const myMat: Mat = new Mat(buf, height, width, CV_8UC4);

Is this ok? or the best?

Works for me