2018-10-06 14:12:59 +00:00
---
2021-03-10 03:43:53 +00:00
category: framework
framework: OpenCV
2018-10-06 14:39:15 +00:00
filename: learnopencv.py
2018-10-06 14:12:59 +00:00
contributors:
- ["Yogesh Ojha", "http://github.com/yogeshojha"]
---
2024-08-19 12:02:19 +00:00
### OpenCV
2018-10-06 14:39:15 +00:00
2024-05-19 09:24:27 +00:00
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).
2024-08-19 12:02:19 +00:00
OpenCV currently supports wide variety of languages like, C++, Python, Java, etc.
2018-10-06 14:39:15 +00:00
#### Installation
Please refer to these articles for installation of OpenCV on your computer.
2018-10-13 14:41:59 +00:00
2020-07-20 06:55:40 +00:00
* Windows Installation Instructions: [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 ](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 Installation Instructions (High Sierra): [https://medium.com/@nuwanprabhath/installing-opencv-in-macos-high-sierra-for-python-3-89c79f0a246a ](https://medium.com/@nuwanprabhath/installing-opencv-in-macos-high-sierra-for-python-3-89c79f0a246a )
* Linux Installation Instructions (Ubuntu 18.04): [https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv ](https://www.pyimagesearch.com/2018/05/28/ubuntu-18-04-how-to-install-opencv )
2018-10-14 07:10:30 +00:00
2024-08-19 12:02:19 +00:00
### Here we will be focusing on Python implementation of OpenCV
2018-10-14 07:10:30 +00:00
2018-10-10 14:36:34 +00:00
```python
2018-10-06 16:49:52 +00:00
# Reading image in OpenCV
2018-10-06 14:39:15 +00:00
import cv2
img = cv2.imread('cat.jpg')
2018-10-06 16:49:52 +00:00
2018-10-06 14:39:15 +00:00
# Displaying the image
# imshow() function is used to display the image
2024-05-19 09:24:27 +00:00
cv2.imshow('Image', img)
2024-08-19 12:02:19 +00:00
# Your first argument is the title of the window and second parameter is image
# If you are getting an error, Object Type None, your image path may be wrong. Please recheck the path to the image
2018-10-06 14:39:15 +00:00
cv2.waitKey(0)
2024-08-19 12:02:19 +00:00
# waitKey() is a keyboard binding function and takes an argument in milliseconds. For GUI events you MUST use waitKey() function.
2018-10-06 16:49:52 +00:00
# Writing an image
2024-05-19 09:24:27 +00:00
cv2.imwrite('catgray.png', img)
2024-08-19 12:02:19 +00:00
# The first argument is the file name and second is the image
2018-10-06 16:49:52 +00:00
# Convert image to grayscale
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Capturing Video from Webcam
cap = cv2.VideoCapture(0)
2024-08-19 12:02:19 +00:00
# 0 is your camera, if you have multiple cameras, you need to enter their id
2024-05-19 09:24:27 +00:00
while True:
2018-10-06 16:49:52 +00:00
# Capturing frame-by-frame
_, frame = cap.read()
2024-05-19 09:24:27 +00:00
cv2.imshow('Frame', frame)
2018-10-06 16:49:52 +00:00
# When user presses q -> quit
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Camera must be released
cap.release()
# Playing Video from file
cap = cv2.VideoCapture('movie.mp4')
2024-05-19 09:24:27 +00:00
while cap.isOpened():
2018-10-06 16:49:52 +00:00
_, frame = cap.read()
# Play the video in grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
2024-05-19 09:24:27 +00:00
cv2.imshow('frame', gray)
2018-10-06 16:49:52 +00:00
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
# Drawing The Line in OpenCV
2024-05-19 09:24:27 +00:00
# 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)
2018-10-06 16:49:52 +00:00
# Drawing Rectangle
2024-05-19 09:24:27 +00:00
# cv2.rectangle(img, (x,y), (x1,y1), (color->r,g,b->0 to 255), thickness)
2018-10-06 16:49:52 +00:00
# thickness = -1 used for filling the rectangle
2024-05-19 09:24:27 +00:00
cv2.rectangle(img, (384, 0), (510, 128), (0, 255, 0), 3)
2018-10-06 16:49:52 +00:00
# Drawing Circle
2024-05-19 09:24:27 +00:00
# cv2.circle(img, (xCenter,yCenter), radius, (color->r,g,b->0 to 255), thickness)
cv2.circle(img, (200, 90), 100, (0, 0, 255), -1)
2018-10-06 16:49:52 +00:00
# Drawing Ellipse
2024-05-19 09:24:27 +00:00
cv2.ellipse(img, (256, 256), (100, 50), 0, 0, 180, 255, -1)
2018-10-06 16:49:52 +00:00
# Adding Text On Images
2024-05-19 09:24:27 +00:00
cv2.putText(img, "Hello World!!!", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
2018-10-06 16:49:52 +00:00
2018-10-06 17:03:37 +00:00
# Blending Images
img1 = cv2.imread('cat.png')
img2 = cv2.imread('openCV.jpg')
2024-05-19 09:24:27 +00:00
dst = cv2.addWeighted(img1, 0.5, img2, 0.5, 0)
2018-10-06 17:03:37 +00:00
# Thresholding image
# Binary Thresholding
2024-05-19 09:24:27 +00:00
_, thresImg = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
2018-10-06 17:03:37 +00:00
# Adaptive Thresholding
2024-05-19 09:24:27 +00:00
adapThres = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
2018-10-06 17:03:37 +00:00
# Blur Image
# Gaussian Blur
2024-05-19 09:24:27 +00:00
blur = cv2.GaussianBlur(img, (5, 5), 0)
2018-10-06 17:03:37 +00:00
# Median Blur
2024-05-19 09:24:27 +00:00
medianBlur = cv2.medianBlur(img, 5)
2018-10-06 17:03:37 +00:00
# Canny Edge Detection
2024-05-19 09:24:27 +00:00
img = cv2.imread('cat.jpg', 0)
edges = cv2.Canny(img, 100, 200)
2018-10-06 17:03:37 +00:00
# Face Detection using Haar Cascades
# Download Haar Cascades from https://github.com/opencv/opencv/blob/master/data/haarcascades/
import cv2
import numpy as np
2024-05-19 09:24:27 +00:00
2018-10-06 17:03:37 +00:00
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
img = cv2.imread('human.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
2023-11-12 07:38:41 +00:00
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
2024-05-19 09:24:27 +00:00
for x, y, w, h in faces:
2023-11-12 07:38:41 +00:00
# Draw a rectangle around detected face
2024-05-19 09:24:27 +00:00
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
roi_gray = gray[y : y + h, x : x + w]
roi_color = img[y : y + h, x : x + w]
2018-10-06 17:03:37 +00:00
eyes = eye_cascade.detectMultiScale(roi_gray)
2024-05-19 09:24:27 +00:00
for ex, ey, ew, eh in eyes:
2023-11-12 07:38:41 +00:00
# Draw a rectangle around detected eyes
2024-05-19 09:24:27 +00:00
cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 2)
2018-10-06 17:03:37 +00:00
2024-05-19 09:24:27 +00:00
cv2.imshow('img', img)
2018-10-06 17:03:37 +00:00
cv2.waitKey(0)
2018-10-06 16:49:52 +00:00
cv2.destroyAllWindows()
2024-05-19 09:24:27 +00:00
# destroyAllWindows() destroys all windows.
2018-10-06 17:03:37 +00:00
# If you wish to destroy specific window pass the exact name of window you created.
2018-10-06 14:39:15 +00:00
```
2018-10-06 14:12:59 +00:00
2018-10-10 14:36:34 +00:00
### Further Reading:
2020-07-20 06:55:40 +00:00
* Download Cascade from [https://github.com/opencv/opencv/blob/master/data/haarcascades ](https://github.com/opencv/opencv/blob/master/data/haarcascades )
* 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 )
* Additional resources may be found at [https://en.wikipedia.org/wiki/OpenCV ](https://en.wikipedia.org/wiki/OpenCV )
2024-05-19 09:24:27 +00:00
* Good OpenCV Tutorials
2020-07-20 06:55:40 +00:00
* [https://realpython.com/python-opencv-color-spaces ](https://realpython.com/python-opencv-color-spaces )
* [https://pyimagesearch.com ](https://pyimagesearch.com )
* [https://www.learnopencv.com ](https://www.learnopencv.com )