Python OpenCV Tutorial

Loading an Image

Below example loads an image and plots it using matplotlib. Opencv uses BGR format to load the image , where as Matplotlib uses RGB. so it is always advised to look at the format

In [7]:
import matplotlib.pyplot as plt
import numpy as np
import cv2

img = cv2.imread('C:\\Dino.jpg',cv2.IMREAD_GRAYSCALE)
%matplotlib inline

plt.imshow(img,cmap='gray')
plt.show()

Capturing Video using webcam

In [33]:
import numpy as np
import cv2

cap = cv2.VideoCapture(0) # 0 here indicates the webcam number.
#Capture the output to a file
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('MyVideo.avi',fourcc,20.0,(640,480))
while True:
    ret,frame = cap.read()
    #Perform some Transformation
    #grayframe = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) #Convert Color to Gray
    
    #Capture the output
    out.write(frame)
    
    cv2.imshow('MyVideo',frame)
    #cv2.imshow('Gray Video', grayframe)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release() # It is important to release webcam
out.release
cv2.destroyAllWindows()

How to load and process a Video

Process Remains same as above except the capturing from webcam,it will be read from file

In [3]:
import cv2
cap = cv2.VideoCapture('MyVideo.avi')
while True:
    grabbed,frame = cap.read()
    if not grabbed:
        break
    cv2.imshow('Video',frame)
    
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

Performing Convolution

Two separate kernels are define. One for detecting Horizontal Edges and one for Vertical edges

In [20]:
horiz_kernel = np.array([[-1,-1,-1],
                   [2,2,2],
                   [-1,-1,-1]])
ver_kernel = np.array([[-1,2,-1],
                       [-1,2,-1],
                       [-1,2,-1]])
diag_kernerl = np.array([[2,-2,-1],
                       [-1,2,-1],
                       [-1,-1,2]])
HaarKernel = np.array([-1,1,-1])

Note How only Diaganol edges are highlighted with Diag Kernel

In [31]:
import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('C:\\lines.jpg',cv2.IMREAD_GRAYSCALE)
dst = np.empty_like(img)
%matplotlib inline

cv2.filter2D(img,-1,diag_kernerl,dst)
f, axarr = plt.subplots(1,2)
axarr[0].imshow(img,cmap='gray')
axarr[1].imshow(dst,cmap = 'gray')
Out[31]:
<matplotlib.image.AxesImage at 0x8849470>

Highlighting only Vertical edges

In [32]:
cv2.filter2D(img,-1,ver_kernel,dst)
f, axarr = plt.subplots(1,2)
axarr[0].imshow(img,cmap='gray')
axarr[1].imshow(dst,cmap = 'gray')
Out[32]:
<matplotlib.image.AxesImage at 0x8907630>

Drawing Ploygon Rectangle on the Image

In [5]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

img = cv2.imread("C:\\Dino.jpg",cv2.IMREAD_GRAYSCALE)
f,ax = plt.subplots(1,2)
ax[0].imshow(img,cmap='gray')
#Draw Rectangle
cv2.rectangle(img,(500,250),(700,450),(0,255,255),10) # 10 is for thickness
ax[1].imshow(img,cmap='gray')

#Draw a Polygon
pts = np.array([[1,3],
                 [25,40],
                 [69,75],
                 [99,167]])
cv2.polylines(img,[pts],True,(0,0,255),5)
ax[1].imshow(img,cmap='gray')
Out[5]:
<matplotlib.image.AxesImage at 0x6bf2f28>

Quiver Plots to Observe Gradients

Its Nice to see the Gradients on the Image . Change in x direction, change in Y direction is computed. Magnitude and Direction of Change is then computed from them.

The change is represented as a vector field

In [28]:
test_array = np.array([[   0.,    0.,    0.,    0.,    0.],
                       [   0.,   64.,  128.,   64.,    0.],
                       [   0.,  127.,  255.,  127.,    0.],
                       [   0.,   64.,  127.,   64.,    0.],
                       [   0.,    0.,    0.,    0.,    0.]])


dy,dx = np.gradient(test_array) # Note: We get Change in Y first. i.e Horizontal
f,ax=plt.subplots(1,3) 
ax[0].imshow(test_array,cmap='gray')
ax[1].imshow(dx,cmap='gray')
ax[2].imshow(dy,cmap='gray')
plt.show()

plt.imshow(test_array,origin='lower')
plt.quiver(dx,dy)
plt.show()
# This is specified as the resultant of gradients expects the origins to ne at the lowerleft corner

Edge detection on a camera feed using Canny Edge detector

In [7]:
import cv2
import numpy as np

cap = cv2.VideoCapture(0) # From Zeroth Webcam

while True:
    _,frame = cap.read()
    laplacian = cv2.Laplacian(frame,cv2.CV_64F) # cv2.CV_64F is the data type used for performing operation
    sobelx = cv2.Sobel(frame,cv2.CV_64F,1,0,ksize=5)  # 1,0 is the direction X
    sobely = cv2.Sobel(frame,cv2.CV_64F,0,1,ksize=5) # 0,1 is the direction Y
    
    edges = cv2.Canny(frame,200,200)
    cv2.imshow('orig', frame)
    cv2.imshow('laplacian',laplacian)
    cv2.imshow('Edges', edges)
    #cv2.imshow('sobelx',sobelx)
    #cv2.imshow('sobely',sobely)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    
cv2.destroyAllWindows()
cap.release()

Face and Eye Detection using Haar Cascades using Open CV

  • Code below shows how to use Haar cascases to detect eyes and Face
In [3]:
import numpy as np
import cv2

cap = cv2.VideoCapture(0)
eye_cascade = cv2.CascadeClassifier('haarcascadeeye.xml')
face_cascade = cv2.CascadeClassifier('haarcascadeface.xml')
while True:
    _,frame = cap.read()
    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray)
    for (x,y,w,h) in faces:
        faceroiGray = gray[y:y+h, x:x+w]
        faceroiColor = frame[y:y+h, x:x+w]
        cv2.rectangle(frame,(x,y),(x+w,y+h),(0,0,255),2)
        eyes = eye_cascade.detectMultiScale(faceroiGray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(faceroiColor,(ex,ey),(ex+ew,ex+eh),(0,255,0),2)
        
    cv2.imshow('orig',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyAllWindows()
cap.release()
In [17]:
import numpy as np
import cv2
import matplotlib.pyplot as plt

face_cascade = cv2.CascadeClassifier('haarcascadeface.xml')
img = cv2.imread('C:\\PV.jpg',cv2.IMREAD_COLOR)
%matplotlib inline

faces = face_cascade.detectMultiScale(img)
for (x,y,w,h) in faces:
    cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)  # Conversion between BGR (opencv ) to RGB (matplotlib) is necessary
plt.imshow(RGB_img,cmap='gray')
plt.show()
In [ ]: