1.
ComputerGraphicsNotesWeek9-01-
0418
1.1. install opencv
pip install opencv-python
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple some-
package
1.2. Examination notice
Test time: Section 78 on Tuesday afternoon of week 9, i.e. 4:00-6:00 pm on April 19
Open book examination
Review materials:
opencv24-python-tutorials-readthedocs-io-en-stable.pdf
1.3. Template Matching in OpenCV
Template matching is a technique for finding areas of an image that are similar to a patch
(template).
A patch is a small image with certain features. The goal of template matching is to find the
patch/template in an image.
Example:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('mainimage.jpg', 0)
img2 = img.copy()
template = cv2.imread('template.jpg', 0)
w, h = template.shape[::-1]
#All the 6 methods for comparison in a list
methods = ['cv2.TM_CCOEFF', 'cv2.TM_CCOEFF_NORMED', 'cv2.TM_CCORR',
'cv2.TM_CCORR_NORMED', 'cv2.TM_SQDIFF', 'cv2.TM_SQDIFF_NORMED']
for meth in methods:
img = img2.copy()
method = eval(meth)
# Apply template Matching
res = cv2.matchTemplate(img, template, method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
#If the method is TM_SQDIFF or TM_SQDIFF_NORMED, take minimum
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
cv2.rectangle(img, top_left, bottom_right, 255, 2)
plt.subplot(121), plt.imshow(res, cmap='gray')
plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
plt.subplot(122), plt.imshow(img, cmap='gray')
plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
plt.suptitle(meth)
plt.show()
1.4. Hough Circle Transform
The function we use here is cv2.HoughCircles(). It has plenty of arguments which are well
explained in the documentation.
import cv2
import numpy as np
img = cv2.imread('mainimage.jpg',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
circles = cv2.HoughCircles(img,cv2.HOUGH_GRADIENT,1,200,
param1=50,param2=30,minRadius=0,maxRadius=0)
circles = np.uint16(np.around(circles))
for i in circles[0,:]:
#draw the outer circle
cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
#draw the center of the circle
cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)
cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()
1.5. Interactive Foreground Extraction using
GrabCut Algorithm
we modify the mask such that all 0-pixels and 2-pixels are put to 0 (ie background) and all 1-pixels
and 3-pixels are put to 1(ie foreground pixels). Now our final mask is ready. Just multiply it with
input image to get the segmented image.
import numpy as np
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('mainimage.jpg')
mask = np.zeros(img.shape[:2],np.uint8)
bgdModel = np.zeros((1,65),np.float64)
fgdModel = np.zeros((1,65),np.float64)
rect = (50,0,450,500)
cv2.grabCut(img,mask,rect,bgdModel,fgdModel,5,cv2.GC_INIT_WITH_RECT)
mask2 = np.where((mask==2)|(mask==0),0,1).astype('uint8')
img = img*mask2[:,:,np.newaxis]
plt.imshow(img),plt.colorbar(),plt.show()
1.6. Video Analysis
1.6.1. Meanshift and Camshift algorithms
To use meanshift in OpenCV, first we need to setup the target, find its histogram so that we can
backproject the target on each frame for calculation of meanshift. We also need to provide initial
location of window. For histogram, only Hue is considered here. Also, to avoid false values due to
low light, low light values are discarded using cv2.inRange() function.
1.6.2. open camera and save it to avi
import cv2
def videocapture():
cap=cv2.VideoCapture(0)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = int(cap.get(cv2.CAP_PROP_FOURCC))
writer = cv2.VideoWriter("video_result.mp4", fourcc, fps, (width, height))
while cap.isOpened():
ret, frame = cap.read()
cv2.imshow('teswell', frame)
key = cv2.waitKey(24)
writer.write(frame)
#press q to quit
if key == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
if name == 'main' :
videocapture()
1.7. Homework
write a program to open camera and save it to video file if you have a camera, if not, just write a
program to open a video file and save it as jpg or another video file with different file names,
submit pdf or doc file with your information.