# Is there a way to convert opencv object (mat object) to blob in javascript?

**URL:** https://forum.opencv.org/t/is-there-a-way-to-convert-opencv-object-mat-object-to-blob-in-javascript/7051
**Category:** Uncategorized
**Tags:** javascript, opencvjs
**Created:** [January 10, 2022, 7:18am UTC](https://forum.opencv.org/t/is-there-a-way-to-convert-opencv-object-mat-object-to-blob-in-javascript/7051 "2022-01-10T07:18:30Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![vnodrajendran](https://avatars.discourse-cdn.com/v4/letter/v/dec6dc/32.png) [@vnodrajendran](https://forum.opencv.org/u/vnodrajendran)
#### Post date: [January 10, 2022, 7:18am UTC](https://forum.opencv.org/t/is-there-a-way-to-convert-opencv-object-mat-object-to-blob-in-javascript/7051/1 "2022-01-10T07:18:30Z")

</div>

Currently I am figuring out how can we convert directly opencv object to blob without using canvas? Any help here

---

<div class="post-metadata">

### Author: ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)
#### Post date: [January 10, 2022, 7:26am UTC](https://forum.opencv.org/t/is-there-a-way-to-convert-opencv-object-mat-object-to-blob-in-javascript/7051/2 "2022-01-10T07:26:51Z")

</div>

which kind of `blob` are you talking about ? please explain !

---

<div class="post-metadata">

### Author: ![vnodrajendran](https://avatars.discourse-cdn.com/v4/letter/v/dec6dc/32.png) [@vnodrajendran](https://forum.opencv.org/u/vnodrajendran)
#### Post date: [January 10, 2022, 7:35am UTC](https://forum.opencv.org/t/is-there-a-way-to-convert-opencv-object-mat-object-to-blob-in-javascript/7051/3 "2022-01-10T07:35:31Z")

</div>

Image blob. Currently I am writing opencv object into canvas using`imshow` and then converting blob using `toBlob()`. If there is a way in opencv which would directly convert images without using canvas.

---

<div class="post-metadata">

### Author: ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)
#### Post date: [January 10, 2022, 8:07am UTC](https://forum.opencv.org/t/is-there-a-way-to-convert-opencv-object-mat-object-to-blob-in-javascript/7051/4 "2022-01-10T08:07:01Z")

</div>

what do you want to do with it ?  
would [ImageData](https://developer.mozilla.org/en-US/docs/Web/API/ImageData) do the trick ?

you could look at the imshow() code:

> <https://github.com/opencv/opencv/blob/a1143c4ea02afa7c45c2a1e86be431b81a83bcd1/modules/js/src/helpers.js#L109>

---

<div class="post-metadata">

### Author: ![ivan-ngchakming](https://avatars.discourse-cdn.com/v4/letter/i/cab0a1/32.png) [@ivan-ngchakming](https://forum.opencv.org/u/ivan-ngchakming)
#### Post date: [May 25, 2022, 4:37pm UTC](https://forum.opencv.org/t/is-there-a-way-to-convert-opencv-object-mat-object-to-blob-in-javascript/7051/5 "2022-05-25T16:37:00Z")

</div>

I used `cv.blobFromImage` to convert opencv `Mat` object directly to blob, no canvas needed.

```nohighlight
const blob = cv.blobFromImage(
    image, // cv.Mat
    1 / 128, // scalefactor
    new cv.Size(inputWeight, inputHeight), // size
    new cv.Scalar(inputMean, inputMean, inputMean), // mean
    true // swapRB
);

```

See [OpenCV: Deep Neural Network module](https://docs.opencv.org/4.5.5/d6/d0f/group__dnn.html#ga29f34df9376379a603acd8df581ac8d7) for API docs.

---

<div class="post-metadata">

### Author: ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)
#### Post date: [May 25, 2022, 5:19pm UTC](https://forum.opencv.org/t/is-there-a-way-to-convert-opencv-object-mat-object-to-blob-in-javascript/7051/6 "2022-05-25T17:19:30Z")

</div>

nice, but imo you misread it.  
question seems not about opencv’s dnn blobs, but about html5 image
