Installation
Select your preferences and run the install command.
Operating System:
Linux
macOS
Windows
Building From Source:
Yes
No
Language:
Python
C++
Java
Android
iOS
JavaScript
Run this Command:
Default Result: pip3 install opencv-python
Verification
To ensure that OpenCV is installed correctly, we can run the following example to show how to read and display image
Python
Change path/to/image to a real path of an image, then run this demo
import cv2 as cv
img = cv.imread("path/to/image")
cv.imshow("Display window", img)
k = cv.waitKey(0) # Wait for a keystroke in the window
C++
Change path/to/image to a real path of an image, then build this demo with OpenCV package and run it
#include
#include
using namespace cv;
int main()
{
std::string image_path = "path/to/image";
Mat img = imread(image_path, IMREAD_COLOR);
imshow("Display window", img);
int k = waitKey(0); // Wait for a keystroke in the window
return 0;
}
JavaScript
Copy this code into an html file and open it in your web browser
Hello OpenCV.js
Hello OpenCV.js
OpenCV.js is loading...
let imgElement = document.getElementById('imageSrc');
let inputElement = document.getElementById('fileInput');
inputElement.addEventListener('change', (e) => {
imgElement.src = URL.createObjectURL(e.target.files[0]);
}, false);
imgElement.onload = function () {
let mat = cv.imread(imgElement);
cv.imshow('canvasOutput', mat);
mat.delete();
};
var Module = {
// https://emscripten.org/docs/api_reference/module.html#Module.onRuntimeInitialized
onRuntimeInitialized() {
document.getElementById('status').innerHTML = 'OpenCV.js is ready.';
}
};