OpenCV Python Tutorial For Beginners
OpenCV is a computer vision library free and open-source that can quickly create image recognition applications in real-time. this OpenCV python tutorial covers various operations for dealing with images and Videos.
Install OpenCV in python
# pip install
pip install opencv-python
# conda install
conda install -c conda-forge opencv
Read and Write image in OpenCV
First thing first for performing any operation on OpenCV the images should be loaded and after loading images it could be rewritten.
Read Image
For loading an image OpenCV provides imread() function.it takes the first argument as the path of the image and the second one is the color flag.
- Flag 1 cv2.IMREAD_COLOR: Load the image in color. Any image transparency is ignored. It’s the default flag.
- Flag 0 cv2.IMREAD_GRAYSCALE: Loads in grayscale image mode.
- Flag -1 cv2.IMREAD_UNCHANGED: Load the image, including the alpha channel.
import cv2
img = cv2.imread('imagePath.jpg',0)
Display image
Function cv2.imshow() is used to display image.first argument is image name and second is image variable.
cv2.imshow('image',img)
# wait till window close key pressed.
cv2.waitKey(0)
# destroy all window
cv2.destroyAllWindows()
Write image
Function cv2.imwrite() is used to write or save image.it take file name and flag type to save image.
import cv2
img = cv2.imread('imagePath.jpg',0)
cv2.imshow('image',img)
k = cv2.waitKey(0)
if k == 27: # wait for ESC key to exit
cv2.destroyAllWindows()
elif k == ord('q'): # wait for 'q' key to save and exit
cv2.imwrite('imageSave.jpg',0)
cv2.destroyAllWindows()
Read and Write Videos in OpenCV
For executing any operation on videos in OpenCV we have to load video either from web came or by giving the path of the video and then save it.
Read Video from Camera
Loading video from camera cv2.VideoCapture() is used and camera index is passed like 0 and 1 etc.
import cv2
cap = cv2.VideoCapture(0)
while(True):
# Capture frames
ret, frame = cap.read()
# Operations on the frame
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display frame
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release the capture
cap.release()
cv2.destroyAllWindows()
Video from file
Loading video from camera cv2.VideoCapture() is used but this time pass the path.
import cv2
cap = cv2.VideoCapture('mtest.avi')
while(cap.isOpened()):
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
cv2.imshow('frame',gray)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Save Video
Save Video from cv2.VideoWriter() function.pass the out put file name, FourCC code, frames per second (fps) and frame size.
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
Drawing in OpenCV
In OpenCV we can draw line, circle, rectangle, ellipse and putText by using these functions : cv2.line(), cv2.circle() , cv2.rectangle(), cv2.ellipse(), cv2.putText() for more info on Drawing visite (Drawing Functions).
import cv2
# Create a black image
img = np.zeros((512,512,3), np.uint8)
# Drawing Line
# Draw a diagonal blue line with thickness of 5 px
img = cv2.line(img,(0,0),(511,511),(255,0,0),5)
# Drawing Rectangle
img = cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)
# Drawing Circle
img = cv2.circle(img,(447,63), 63, (0,0,255), -1)
# Drawing Ellipse
img = cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)
# Drawing Polygon
pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
pts = pts.reshape((-1,1,2))
img = cv2.polylines(img,[pts],True,(0,255,255))
# Text
font = cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)
If you have questions, feel free to ask, if you liked the post comment and share.
Thank you!
0 Comments