[opencv/*] fix empty links and format code

This commit is contained in:
Boris Verkhovskiy 2024-05-19 03:24:27 -06:00
parent 781b19ef41
commit 34c5f5ad9a
3 changed files with 126 additions and 124 deletions

View File

@ -8,7 +8,6 @@ translators:
- ["Dennis Keller", "https://github.com/denniskeller"] - ["Dennis Keller", "https://github.com/denniskeller"]
lang: de-de lang: de-de
--- ---
### OpenCV
OpenCV (Open Source Computer Vision) ist eine Bibliothek von Programmierfunktionen, OpenCV (Open Source Computer Vision) ist eine Bibliothek von Programmierfunktionen,
die hauptsächlich auf maschinelles Sehen in Echtzeit ausgerichtet ist. die hauptsächlich auf maschinelles Sehen in Echtzeit ausgerichtet ist.
@ -20,9 +19,9 @@ OpenCV unterstützt derzeit eine Vielzahl von Sprachen, wie C++, Python, Java uv
Bitte lies diesen Artikel für die Installation von OpenCV auf deinem Computer. Bitte lies diesen Artikel für die Installation von OpenCV auf deinem Computer.
* Windows Installationsanleitung: [https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html#install-opencv-python-in-windows]() * [Windows Installationsanleitung](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html#install-opencv-python-in-windows)
* Mac Installationsanleitung (High Sierra): [https://medium.com/@nuwanprabhath/installing-opencv-in-macos-high-sierra-for-python-3-89c79f0a246a]() * [Mac Installationsanleitung](https://medium.com/@nuwanprabhath/installing-opencv-in-macos-high-sierra-for-python-3-89c79f0a246a) (High Sierra)
* Linux Installationsanleitung (Ubuntu 18.04): [https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv]() * [Linux Installationsanleitung](https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv) (Ubuntu 18.04)
### Hier werden wir uns auf die Pythonimplementierung von OpenCV konzentrieren. ### Hier werden wir uns auf die Pythonimplementierung von OpenCV konzentrieren.
@ -33,7 +32,7 @@ img = cv2.imread('Katze.jpg')
# Bild darstellen # Bild darstellen
# Die imshow() Funktion wird verwendet um das Display darzustellen. # Die imshow() Funktion wird verwendet um das Display darzustellen.
cv2.imshow('Image',img) cv2.imshow('Image', img)
# Das erste Argument ist der Titel des Fensters und der zweite Parameter ist das Bild # Das erste Argument ist der Titel des Fensters und der zweite Parameter ist das Bild
# Wenn du den Fehler Object Type None bekommst, ist eventuell dein Bildpfad falsch. # Wenn du den Fehler Object Type None bekommst, ist eventuell dein Bildpfad falsch.
# Bitte überprüfe dann den Pfad des Bildes erneut. # Bitte überprüfe dann den Pfad des Bildes erneut.
@ -42,7 +41,7 @@ cv2.waitKey(0)
# Millisekunden an. Für GUI Ereignisse MUSST du die waitKey() Funktion verwenden. # Millisekunden an. Für GUI Ereignisse MUSST du die waitKey() Funktion verwenden.
# Ein Bild schreiben # Ein Bild schreiben
cv2.imwrite('graueKatze.png',img) cv2.imwrite('graueKatze.png', img)
# Das erste Argument ist der Dateiname und das zweite ist das Bild # Das erste Argument ist der Dateiname und das zweite ist das Bild
# Konvertiert das Bild zu Graustufen # Konvertiert das Bild zu Graustufen
@ -51,10 +50,10 @@ gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Videoaufnahme von der Webcam # Videoaufnahme von der Webcam
cap = cv2.VideoCapture(0) cap = cv2.VideoCapture(0)
# 0 ist deine Kamera, wenn du mehrere Kameras hast musst du deren Id eingeben # 0 ist deine Kamera, wenn du mehrere Kameras hast musst du deren Id eingeben
while(True): while True:
# Erfassen von Einzelbildern # Erfassen von Einzelbildern
_, frame = cap.read() _, frame = cap.read()
cv2.imshow('Frame',frame) cv2.imshow('Frame', frame)
# Wenn der Benutzer q drückt -> beenden # Wenn der Benutzer q drückt -> beenden
if cv2.waitKey(1) & 0xFF == ord('q'): if cv2.waitKey(1) & 0xFF == ord('q'):
break break
@ -63,59 +62,60 @@ cap.release()
# Wiedergabe von Videos aus einer Datei # Wiedergabe von Videos aus einer Datei
cap = cv2.VideoCapture('film.mp4') cap = cv2.VideoCapture('film.mp4')
while(cap.isOpened()): while cap.isOpened():
_, frame = cap.read() _, frame = cap.read()
# Das Video in Graustufen abspielen # Das Video in Graustufen abspielen
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray) cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'): if cv2.waitKey(1) & 0xFF == ord('q'):
break break
cap.release() cap.release()
# Zeichne eine Linie in OpenCV # Zeichne eine Linie in OpenCV
# cv2.line(img,(x,y),(x1,y1),(color->r,g,b->0 to 255),thickness) # cv2.line(img, (x,y), (x1,y1), (color->r,g,b->0 to 255), thickness)
cv2.line(img,(0,0),(511,511),(255,0,0),5) cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)
# Zeichne ein Rechteck # Zeichne ein Rechteck
# cv2.rectangle(img,(x,y),(x1,y1),(color->r,g,b->0 to 255),thickness) # cv2.rectangle(img, (x,y), (x1,y1), (color->r,g,b->0 to 255), thickness)
# thickness = -1 wird zum Füllen des Rechtecks verwendet # thickness = -1 wird zum Füllen des Rechtecks verwendet
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3) cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)
# Zeichne ein Kreis # Zeichne ein Kreis
cv2.circle(img,(xCenter,yCenter), radius, (color->r,g,b->0 to 255), thickness) # cv2.circle(img, (xCenter,yCenter), radius, (color->r,g,b->0 to 255), thickness)
cv2.circle(img,(200,90), 100, (0,0,255), -1) cv2.circle(img, (200, 90), 100, (0, 0, 255), -1)
# Zeichne eine Ellipse # Zeichne eine Ellipse
cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1) cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 180, 255, -1)
# Text auf Bildern hinzufügen # Text auf Bildern hinzufügen
cv2.putText(img,"Hello World!!!", (x,y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255) cv2.putText(img, "Hello World!!!", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
# Bilder zusammenfügen # Bilder zusammenfügen
img1 = cv2.imread('Katze.png') img1 = cv2.imread('Katze.png')
img2 = cv2.imread('openCV.jpg') img2 = cv2.imread('openCV.jpg')
dst = cv2.addWeighted(img1,0.5,img2,0.5,0) dst = cv2.addWeighted(img1, 0.5, img2, 0.5, 0)
# Schwellwertbild # Schwellwertbild
# Binäre Schwellenwerte # Binäre Schwellenwerte
_,thresImg = cv2.threshold(img,127,255,cv2.THRESH_BINARY) _, thresImg = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# Anpassbare Schwellenwerte # Anpassbare Schwellenwerte
adapThres = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2) adapThres = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# Weichzeichnung von einem Bild # Weichzeichnung von einem Bild
# Gaußscher Weichzeichner # Gaußscher Weichzeichner
blur = cv2.GaussianBlur(img,(5,5),0) blur = cv2.GaussianBlur(img, (5, 5), 0)
# Rangordnungsfilter # Rangordnungsfilter
medianBlur = cv2.medianBlur(img,5) medianBlur = cv2.medianBlur(img, 5)
# Canny-Algorithmus # Canny-Algorithmus
img = cv2.imread('Katze.jpg',0) img = cv2.imread('Katze.jpg', 0)
edges = cv2.Canny(img,100,200) edges = cv2.Canny(img, 100, 200)
# Gesichtserkennung mit Haarkaskaden # Gesichtserkennung mit Haarkaskaden
# Lade die Haarkaskaden von https://github.com/opencv/opencv/blob/master/data/haarcascades/ herunter # Lade die Haarkaskaden von https://github.com/opencv/opencv/blob/master/data/haarcascades/ herunter
import cv2 import cv2
import numpy as np import numpy as np
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
@ -123,15 +123,15 @@ img = cv2.imread('Mensch.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
aces = face_cascade.detectMultiScale(gray, 1.3, 5) aces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces: for x, y, w, h in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y+h, x:x+w] roi_gray = gray[y : y + h, x : x + w]
roi_color = img[y:y+h, x:x+w] roi_color = img[y : y + h, x : x + w]
eyes = eye_cascade.detectMultiScale(roi_gray) eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes: for ex, ey, ew, eh in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2) cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('img',img) cv2.imshow('img', img)
cv2.waitKey(0) cv2.waitKey(0)
cv2.destroyAllWindows() cv2.destroyAllWindows()
@ -141,13 +141,14 @@ cv2.destroyAllWindows()
``` ```
### Weiterführende Literatur: ### Weiterführende Literatur:
* Lade Kaskade hier herunter [https://github.com/opencv/opencv/blob/master/data/haarcascades]()
* OpenCV Zeichenfunktionen [https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html]() * Lade Kaskade hier herunter [https://github.com/opencv/opencv/blob/master/data/haarcascades](https://github.com/opencv/opencv/blob/master/data/haarcascades)
* Eine aktuelle Sprachenreferenz kann hier gefunden werden [https://opencv.org]() * OpenCV Zeichenfunktionen [https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html](https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html)
* Zusätzliche Ressourcen können hier gefunden werden [https://en.wikipedia.org/wiki/OpenCV]() * Eine aktuelle Sprachenreferenz kann hier gefunden werden [https://opencv.org](https://opencv.org)
* Zusätzliche Ressourcen können hier gefunden werden [https://en.wikipedia.org/wiki/OpenCV](https://en.wikipedia.org/wiki/OpenCV)
* Gute OpenCV Tutorials * Gute OpenCV Tutorials
* [https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html]() * [https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html)
* [https://realpython.com/python-opencv-color-spaces]() * [https://realpython.com/python-opencv-color-spaces](https://realpython.com/python-opencv-color-spaces)
* [https://pyimagesearch.com]() * [https://pyimagesearch.com](https://pyimagesearch.com)
* [https://www.learnopencv.com]() * [https://www.learnopencv.com](https://www.learnopencv.com)
* [https://docs.opencv.org/master/]() * [https://docs.opencv.org/master/](https://docs.opencv.org/master/)

View File

@ -9,7 +9,7 @@ contributors:
OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision. OpenCV (Open Source Computer Vision) is a library of programming functions mainly aimed at real-time computer vision.
Originally developed by Intel, it was later supported by Willow Garage then Itseez (which was later acquired by Intel). Originally developed by Intel, it was later supported by Willow Garage then Itseez (which was later acquired by Intel).
Opencv currently supports wide variety of languages like, C++, Python, Java etc OpenCV currently supports wide variety of languages like, C++, Python, Java etc
#### Installation #### Installation
Please refer to these articles for installation of OpenCV on your computer. Please refer to these articles for installation of OpenCV on your computer.
@ -27,14 +27,14 @@ img = cv2.imread('cat.jpg')
# Displaying the image # Displaying the image
# imshow() function is used to display the image # imshow() function is used to display the image
cv2.imshow('Image',img) cv2.imshow('Image', img)
# Your first arguement is the title of the window and second parameter is image # Your first arguement is the title of the window and second parameter is image
# If you are getting error, Object Type None, your image path may be wrong. Please recheck the pack to the image # If you are getting error, Object Type None, your image path may be wrong. Please recheck the pack to the image
cv2.waitKey(0) cv2.waitKey(0)
# waitKey() is a keyboard binding function and takes arguement in milliseconds. For GUI events you MUST use waitKey() function. # waitKey() is a keyboard binding function and takes arguement in milliseconds. For GUI events you MUST use waitKey() function.
# Writing an image # Writing an image
cv2.imwrite('catgray.png',img) cv2.imwrite('catgray.png', img)
# first arguement is the file name and second is the image # first arguement is the file name and second is the image
# Convert image to grayscale # Convert image to grayscale
@ -42,11 +42,11 @@ gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Capturing Video from Webcam # Capturing Video from Webcam
cap = cv2.VideoCapture(0) cap = cv2.VideoCapture(0)
#0 is your camera, if you have multiple camera, you need to enter their id # 0 is your camera, if you have multiple camera, you need to enter their id
while(True): while True:
# Capturing frame-by-frame # Capturing frame-by-frame
_, frame = cap.read() _, frame = cap.read()
cv2.imshow('Frame',frame) cv2.imshow('Frame', frame)
# When user presses q -> quit # When user presses q -> quit
if cv2.waitKey(1) & 0xFF == ord('q'): if cv2.waitKey(1) & 0xFF == ord('q'):
break break
@ -55,59 +55,60 @@ cap.release()
# Playing Video from file # Playing Video from file
cap = cv2.VideoCapture('movie.mp4') cap = cv2.VideoCapture('movie.mp4')
while(cap.isOpened()): while cap.isOpened():
_, frame = cap.read() _, frame = cap.read()
# Play the video in grayscale # Play the video in grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray) cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'): if cv2.waitKey(1) & 0xFF == ord('q'):
break break
cap.release() cap.release()
# Drawing The Line in OpenCV # Drawing The Line in OpenCV
# cv2.line(img,(x,y),(x1,y1),(color->r,g,b->0 to 255),thickness) # cv2.line(img, (x,y), (x1,y1), (color->r,g,b->0 to 255), thickness)
cv2.line(img,(0,0),(511,511),(255,0,0),5) cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)
# Drawing Rectangle # Drawing Rectangle
# cv2.rectangle(img,(x,y),(x1,y1),(color->r,g,b->0 to 255),thickness) # cv2.rectangle(img, (x,y), (x1,y1), (color->r,g,b->0 to 255), thickness)
# thickness = -1 used for filling the rectangle # thickness = -1 used for filling the rectangle
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3) cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)
# Drawing Circle # Drawing Circle
cv2.circle(img,(xCenter,yCenter), radius, (color->r,g,b->0 to 255), thickness) # cv2.circle(img, (xCenter,yCenter), radius, (color->r,g,b->0 to 255), thickness)
cv2.circle(img,(200,90), 100, (0,0,255), -1) cv2.circle(img, (200, 90), 100, (0, 0, 255), -1)
# Drawing Ellipse # Drawing Ellipse
cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1) cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 180, 255, -1)
# Adding Text On Images # Adding Text On Images
cv2.putText(img,"Hello World!!!", (x,y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255) cv2.putText(img, "Hello World!!!", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
# Blending Images # Blending Images
img1 = cv2.imread('cat.png') img1 = cv2.imread('cat.png')
img2 = cv2.imread('openCV.jpg') img2 = cv2.imread('openCV.jpg')
dst = cv2.addWeighted(img1,0.5,img2,0.5,0) dst = cv2.addWeighted(img1, 0.5, img2, 0.5, 0)
# Thresholding image # Thresholding image
# Binary Thresholding # Binary Thresholding
_,thresImg = cv2.threshold(img,127,255,cv2.THRESH_BINARY) _, thresImg = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# Adaptive Thresholding # Adaptive Thresholding
adapThres = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2) adapThres = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# Blur Image # Blur Image
# Gaussian Blur # Gaussian Blur
blur = cv2.GaussianBlur(img,(5,5),0) blur = cv2.GaussianBlur(img, (5, 5), 0)
# Median Blur # Median Blur
medianBlur = cv2.medianBlur(img,5) medianBlur = cv2.medianBlur(img, 5)
# Canny Edge Detection # Canny Edge Detection
img = cv2.imread('cat.jpg',0) img = cv2.imread('cat.jpg', 0)
edges = cv2.Canny(img,100,200) edges = cv2.Canny(img, 100, 200)
# Face Detection using Haar Cascades # Face Detection using Haar Cascades
# Download Haar Cascades from https://github.com/opencv/opencv/blob/master/data/haarcascades/ # Download Haar Cascades from https://github.com/opencv/opencv/blob/master/data/haarcascades/
import cv2 import cv2
import numpy as np import numpy as np
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml') eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
@ -115,17 +116,17 @@ img = cv2.imread('human.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5) faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces: for x, y, w, h in faces:
# Draw a rectangle around detected face # Draw a rectangle around detected face
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y+h, x:x+w] roi_gray = gray[y : y + h, x : x + w]
roi_color = img[y:y+h, x:x+w] roi_color = img[y : y + h, x : x + w]
eyes = eye_cascade.detectMultiScale(roi_gray) eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes: for ex, ey, ew, eh in eyes:
# Draw a rectangle around detected eyes # Draw a rectangle around detected eyes
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2) cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('img',img) cv2.imshow('img', img)
cv2.waitKey(0) cv2.waitKey(0)
cv2.destroyAllWindows() cv2.destroyAllWindows()
@ -139,7 +140,7 @@ cv2.destroyAllWindows()
* OpenCV drawing Functions [https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html](https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html) * OpenCV drawing Functions [https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html](https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html)
* An up-to-date language reference can be found at [https://opencv.org](https://opencv.org) * An up-to-date language reference can be found at [https://opencv.org](https://opencv.org)
* Additional resources may be found at [https://en.wikipedia.org/wiki/OpenCV](https://en.wikipedia.org/wiki/OpenCV) * Additional resources may be found at [https://en.wikipedia.org/wiki/OpenCV](https://en.wikipedia.org/wiki/OpenCV)
* Good OpenCv Tutorials * Good OpenCV Tutorials
* [https://realpython.com/python-opencv-color-spaces](https://realpython.com/python-opencv-color-spaces) * [https://realpython.com/python-opencv-color-spaces](https://realpython.com/python-opencv-color-spaces)
* [https://pyimagesearch.com](https://pyimagesearch.com) * [https://pyimagesearch.com](https://pyimagesearch.com)
* [https://www.learnopencv.com](https://www.learnopencv.com) * [https://www.learnopencv.com](https://www.learnopencv.com)

View File

@ -8,18 +8,18 @@ translators:
- ["GengchenXU", "https://github.com/GengchenXU"] - ["GengchenXU", "https://github.com/GengchenXU"]
lang: zh-cn lang: zh-cn
--- ---
### Opencv
Opencv开源计算机视觉是一个编程功能库主要面向实时计算机视觉。最初由英特尔开发后来由Willow Garage然后Itseez后来被英特尔收购支持。Opencv 目前支持多种语言如C++、Python、Java 等 OpenCV开源计算机视觉是一个编程功能库主要面向实时计算机视觉。最初由英特尔开发后来由Willow Garage然后Itseez后来被英特尔收购支持。OpenCV 目前支持多种语言如C++、Python、Java 等
#### 安装 #### 安装
有关在计算机上安装 OpenCV请参阅这些文章。 有关在计算机上安装 OpenCV请参阅这些文章。
* Windows 安装说明: [https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html#install-opencv-python-in-windows]() * [Windows 安装说明](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html#install-opencv-python-in-windows)
* Mac 安装说明 (High Sierra): [https://medium.com/@nuwanprabhath/installing-opencv-in-macos-high-sierra-for-python-3-89c79f0a246a]() * [Mac 安装说明](https://medium.com/@nuwanprabhath/installing-opencv-in-macos-high-sierra-for-python-3-89c79f0a246a) (High Sierra)
* Linux 安装说明 Ubuntu 18.04): [https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv]() * [Linux 安装说明](https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv) Ubuntu 18.04)
### 在这里,我们将专注于 OpenCV 的 python 实现 ### 在这里,我们将专注于 OpenCV 的 Python 实现
```python ```python
# OpenCV读取图片 # OpenCV读取图片
@ -28,14 +28,14 @@ img = cv2.imread('cat.jpg')
# 显示图片 # 显示图片
# imshow() 函数被用来显示图片 # imshow() 函数被用来显示图片
cv2.imshow('Image',img) cv2.imshow('Image', img)
# 第一个参数是窗口的标题第二个参数是image # 第一个参数是窗口的标题第二个参数是image
# 如果你得到错误对象类型为None你的图像路径可能是错误的。请重新检查图像包 # 如果你得到错误对象类型为None你的图像路径可能是错误的。请重新检查图像包
cv2.waitKey(0) cv2.waitKey(0)
# waitKey() 是一个键盘绑定函数参数以毫秒为单位。对于GUI事件必须使用waitKey()函数。 # waitKey() 是一个键盘绑定函数参数以毫秒为单位。对于GUI事件必须使用waitKey()函数。
# 保存图片 # 保存图片
cv2.imwrite('catgray.png',img) cv2.imwrite('catgray.png', img)
# 第一个参数是文件名,第二个参数是图像 # 第一个参数是文件名,第二个参数是图像
# 转换图像灰度 # 转换图像灰度
@ -43,11 +43,11 @@ gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 从摄像头捕捉视频 # 从摄像头捕捉视频
cap = cv2.VideoCapture(0) cap = cv2.VideoCapture(0)
#0 是你的相机如果你有多台相机你需要输入他们的id # 0 是你的相机如果你有多台相机你需要输入他们的id
while(True): while True:
# 一帧一帧地获取 # 一帧一帧地获取
_, frame = cap.read() _, frame = cap.read()
cv2.imshow('Frame',frame) cv2.imshow('Frame', frame)
# 当用户按下q ->退出 # 当用户按下q ->退出
if cv2.waitKey(1) & 0xFF == ord('q'): if cv2.waitKey(1) & 0xFF == ord('q'):
break break
@ -56,54 +56,54 @@ cap.release()
# 在文件中播放视频 # 在文件中播放视频
cap = cv2.VideoCapture('movie.mp4') cap = cv2.VideoCapture('movie.mp4')
while(cap.isOpened()): while cap.isOpened():
_, frame = cap.read() _, frame = cap.read()
# 灰度播放视频 # 灰度播放视频
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray) cv2.imshow('frame', gray)
if cv2.waitKey(1) & 0xFF == ord('q'): if cv2.waitKey(1) & 0xFF == ord('q'):
break break
cap.release() cap.release()
# 在OpenCV中画线 # 在OpenCV中画线
# cv2.line(img,(x,y),(x1,y1),(color->r,g,b->0 to 255),thickness)(注 color颜色rgb参数 thickness粗细) # cv2.line(img, (x,y), (x1,y1), (color->r,g,b->0 to 255), thickness)(注 color颜色rgb参数 thickness粗细)
cv2.line(img,(0,0),(511,511),(255,0,0),5) cv2.line(img, (0, 0), (511, 511), (255, 0, 0), 5)
# 画矩形 # 画矩形
# cv2.rectangle(img,(x,y),(x1,y1),(color->r,g,b->0 to 255),thickness) # cv2.rectangle(img, (x,y), (x1,y1), (color->r,g,b->0 to 255), thickness)
# 粗细= -1用于填充矩形 # 粗细= -1用于填充矩形
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3) cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)
# 画圆 # 画圆
cv2.circle(img,(xCenter,yCenter), radius, (color->r,g,b->0 to 255), thickness) # cv2.circle(img, (xCenter,yCenter), radius, (color->r,g,b->0 to 255), thickness)
cv2.circle(img,(200,90), 100, (0,0,255), -1) cv2.circle(img, (200, 90), 100, (0, 0, 255), -1)
# 画椭圆 # 画椭圆
cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1) cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 180, 255, -1)
# 在图像上增加文字 # 在图像上增加文字
cv2.putText(img,"Hello World!!!", (x,y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255) cv2.putText(img, "Hello World!!!", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
# 合成图像 # 合成图像
img1 = cv2.imread('cat.png') img1 = cv2.imread('cat.png')
img2 = cv2.imread('openCV.jpg') img2 = cv2.imread('openCV.jpg')
dst = cv2.addWeighted(img1,0.5,img2,0.5,0) dst = cv2.addWeighted(img1, 0.5, img2, 0.5, 0)
# 阈值图像 # 阈值图像
# 二进制阈值 # 二进制阈值
_,thresImg = cv2.threshold(img,127,255,cv2.THRESH_BINARY) _, thresImg = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
# Adaptive Thresholding # Adaptive Thresholding
adapThres = cv2.adaptiveThreshold(img,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY,11,2) adapThres = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# 模糊的形象 # 模糊的形象
# 高斯模糊 # 高斯模糊
blur = cv2.GaussianBlur(img,(5,5),0) blur = cv2.GaussianBlur(img, (5, 5), 0)
# 模糊中值 # 模糊中值
medianBlur = cv2.medianBlur(img,5) medianBlur = cv2.medianBlur(img, 5)
# Canny 边缘检测 # Canny 边缘检测
img = cv2.imread('cat.jpg',0) img = cv2.imread('cat.jpg', 0)
edges = cv2.Canny(img,100,200) edges = cv2.Canny(img, 100, 200)
# 用Haar Cascades进行人脸检测 # 用Haar Cascades进行人脸检测
# 下载 Haar Cascades 在 https://github.com/opencv/opencv/blob/master/data/haarcascades/ # 下载 Haar Cascades 在 https://github.com/opencv/opencv/blob/master/data/haarcascades/
@ -116,15 +116,15 @@ img = cv2.imread('human.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
aces = face_cascade.detectMultiScale(gray, 1.3, 5) aces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces: for x, y, w, h in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y:y+h, x:x+w] roi_gray = gray[y : y + h, x : x + w]
roi_color = img[y:y+h, x:x+w] roi_color = img[y : y + h, x : x + w]
eyes = eye_cascade.detectMultiScale(roi_gray) eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes: for ex, ey, ew, eh in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2) cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
cv2.imshow('img',img) cv2.imshow('img', img)
cv2.waitKey(0) cv2.waitKey(0)
cv2.destroyAllWindows() cv2.destroyAllWindows()
@ -134,12 +134,12 @@ cv2.destroyAllWindows()
### 进一步阅读: ### 进一步阅读:
* Download Cascade from [https://github.com/opencv/opencv/blob/master/data/haarcascades]() * Download Cascade from [https://github.com/opencv/opencv/blob/master/data/haarcascades](https://github.com/opencv/opencv/blob/master/data/haarcascades)
* OpenCV 绘图函数 [https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html]() * OpenCV 绘图函数 [https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html](https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html)
* 最新的语言参考 [https://opencv.org]() * 最新的语言参考 [https://opencv.org](https://opencv.org)
* 更多的资源 [https://en.wikipedia.org/wiki/OpenCV]() * 更多的资源 [https://en.wikipedia.org/wiki/OpenCV](https://en.wikipedia.org/wiki/OpenCV)
* 优秀的的 OpenCV 教程 * 优秀的的 OpenCV 教程
* [https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html]() * [https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html](https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html)
* [https://realpython.com/python-opencv-color-spaces]() * [https://realpython.com/python-opencv-color-spaces](https://realpython.com/python-opencv-color-spaces)
* [https://pyimagesearch.com]() * [https://pyimagesearch.com](https://pyimagesearch.com)
* [https://www.learnopencv.com]() * [https://www.learnopencv.com](https://www.learnopencv.com)