2018-10-06 14:12:59 +00:00
|
|
|
---
|
2018-10-06 16:49:52 +00:00
|
|
|
language: python
|
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"]
|
|
|
|
---
|
2018-10-06 14:39:15 +00:00
|
|
|
### Opencv
|
|
|
|
|
|
|
|
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).
|
|
|
|
Opencv currently supports wide variety of languages like, C++, Python, Java etc
|
|
|
|
|
|
|
|
#### Installation
|
|
|
|
Please refer to these articles for installation of OpenCV on your computer.
|
|
|
|
* 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>
|
|
|
|
|
|
|
|
* Mac Installation Instructions (High Sierra):
|
|
|
|
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/>
|
|
|
|
|
|
|
|
### Here we will be focusing on python implementation of OpenCV
|
|
|
|
|
|
|
|
```
|
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
|
|
|
|
cv2.imshow('Image',img)
|
|
|
|
# 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
|
|
|
|
cv2.waitKey(0)
|
|
|
|
# waitKey() is a keyboard binding function and takes arguement in milliseconds. For GUI events you MUST use waitKey() function.
|
2018-10-06 16:49:52 +00:00
|
|
|
|
|
|
|
# Writing an image
|
|
|
|
cv2.imwrite('catgray.png',img)
|
|
|
|
# first arguement is the file name and second is the image
|
|
|
|
|
|
|
|
# Convert image to grayscale
|
|
|
|
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
|
|
|
|
|
|
|
# Capturing Video from Webcam
|
|
|
|
cap = cv2.VideoCapture(0)
|
|
|
|
#0 is your camera, if you have multiple camera, you need to enter their id
|
|
|
|
while(True):
|
|
|
|
# Capturing frame-by-frame
|
|
|
|
_, frame = cap.read()
|
|
|
|
cv2.imshow('Frame',frame)
|
|
|
|
# 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')
|
|
|
|
while(cap.isOpened()):
|
|
|
|
_, frame = cap.read()
|
|
|
|
# Play the video in grayscale
|
|
|
|
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
|
|
cv2.imshow('frame',gray)
|
|
|
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
|
|
|
break
|
|
|
|
cap.release()
|
|
|
|
|
|
|
|
# Drawing The Line in OpenCV
|
|
|
|
# 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)
|
|
|
|
|
|
|
|
# Drawing Rectangle
|
|
|
|
# cv2.rectangle(img,(x,y),(x1,y1),(color->r,g,b->0 to 255),thickness)
|
|
|
|
# thickness = -1 used for filling the rectangle
|
|
|
|
cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
|
|
|
|
|
|
|
|
# Drawing Circle
|
|
|
|
cv2.circle(img,(xCenter,yCenter), radius, (color->r,g,b->0 to 255), thickness)
|
|
|
|
cv2.circle(img,(200,90), 100, (0,0,255), -1)
|
|
|
|
|
|
|
|
# Drawing Ellipse
|
|
|
|
cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
|
|
|
|
|
|
|
|
# Adding Text On Images
|
|
|
|
cv2.putText(img,"Hello World!!!", (x,y), cv2.FONT_HERSHEY_SIMPLEX, 2, 255)
|
|
|
|
|
|
|
|
|
|
|
|
cv2.destroyAllWindows()
|
|
|
|
# destroyAllWindows() destroys all windows. 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
|
|
|
|
|
|
|
Further Reading:
|
|
|
|
|
2018-10-06 16:49:52 +00:00
|
|
|
OpenCV drawing Functions
|
|
|
|
<https://docs.opencv.org/2.4/modules/core/doc/drawing_functions.html>
|
|
|
|
|
2018-10-06 14:12:59 +00:00
|
|
|
An up-to-date language reference can be found at
|
2018-10-06 14:39:15 +00:00
|
|
|
<https://opencv.org/>
|
2018-10-06 14:12:59 +00:00
|
|
|
|
|
|
|
Additional resources may be found at
|
|
|
|
<https://en.wikipedia.org/wiki/OpenCV>
|
2018-10-06 14:39:15 +00:00
|
|
|
|
|
|
|
Good OpenCv Tutorials
|
|
|
|
<https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html>
|
|
|
|
<https://realpython.com/python-opencv-color-spaces/>
|
|
|
|
<https://pyimagesearch.com>
|
|
|
|
<https://www.learnopencv.com>
|