Goals
- Learn how to read an image and how to display it in a web.
Read an image
OpenCV.js saves images as cv.Mat type. We use HTML canvas element to transfer cv.Mat to the web or in reverse. The ImageData interface can represent or set the underlying pixel data of an area of a canvas element.
- Note
- Please refer to canvas docs for more details.
First, create an ImageData obj from canvas:
Then, use cv.matFromImageData to construct a cv.Mat:
- Note
- Because canvas only support 8-bit RGBA image with continuous storage, the cv.Mat type is cv.CV_8UC4. It is different from native OpenCV because images returned and shown by the native imread and imshow have the channels stored in BGR order.
Display an image
First, convert the type of src to cv.CV_8UC4:
Then, new an ImageData obj from dst:
Finally, display it:
In OpenCV.js
OpenCV.js implements image reading and showing using the above method.
We use cv.imread (imageSource) to read an image from html canvas or img element.
- Parameters
-
imageSource canvas element or id, or img element or id.
- Returns
- mat with channels stored in RGBA order.
We use cv.imshow (canvasSource, mat) to display it. The function may scale the mat, depending on its depth:
- If the mat is 8-bit unsigned, it is displayed as is.
- If the mat is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].
- If the mat is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].
- Parameters
-
canvasSource canvas element or id. mat mat to be shown.
The above code of image reading and showing could be simplified as below.
Try it