Примеры с cv2.imshow требуют сборки OpenCV с GUI. В ноутбуке с headless-версией используйте вывод изображений через Matplotlib.
Getting Started with Images
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.
First, create an ImageData obj from canvas:
letcanvas = .();
= .();
= .(, , ., .);
document
getElementById
canvasInputId
let
ctx
canvas
getContext
'2d'
let
imgData
ctx
getImageData
0
0
canvas
width
canvas
height
Then, use cv.matFromImageData to construct a cv.Mat:
letsrc = cv.matFromImageData(imgData);
Display an image
First, convert the type of src to cv.CV_8UC4:
letdst = newcv.Mat();
// scale and shift are used to map the data to [0, 255].src.convertTo(dst, cv.CV_8U, scale, shift);
// *** is GRAY, RGB, or RGBA, according to src.channels() is 1, 3 or 4.cv.cvtColor(dst, dst, cv.COLOR_***2RGBA);