安装
选择您的偏好并运行安装命令。
操作系统
Linux
macOS
Windows
从源代码构建
是
否
语言
Python
C++
Java
Android
iOS
JavaScript
运行此命令
默认结果:pip3 install opencv-python
验证
为了确保 OpenCV 正确安装,我们可以运行以下示例来演示如何读取和显示图像
Python
将 path/to/image 更改为图像的实际路径,然后运行此演示
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++
将 path/to/image 更改为图像的实际路径,然后使用 OpenCV 包构建此演示并运行它
#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
将此代码复制到 html 文件中,并在您的网络浏览器中打开它
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.webassembly.net.cn/docs/api_reference/module.html#Module.onRuntimeInitialized
onRuntimeInitialized() {
document.getElementById('status').innerHTML = 'OpenCV.js is ready.';
}
};