安裝
選擇您的偏好並執行安裝命令。
作業系統
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 - OpenCV 計算機視覺庫
Hello OpenCV.js
OpenCV.js is loading...
imageSrc
canvasOutput
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.';
}
};
