基于Python的OpenCV学习
第六章、图像金字塔、轮廓检测与模板匹配
00_思维导图
01_cv2.pyrUp_cv2.pyrDown
import cv2
import matplotlib.pyplot as plt
img = cv2.imread('AM.png')
img_up = cv2.pyrUp(img)
img_down = cv2.pyrDown(img)
img_list = [img,img_up,img_down]
img_title = ['init','up','down']
for i in range(3):
plt.subplot(1,3,i+1)
plt.imshow(img_list[i])
plt.title(img_title[i])
plt.show()
cv2.destroyAllWindows()
运行结果:
02_cv2.findContours
import cv2
img = cv2.imread('contours.png',cv2.IMREAD_GRAYSCALE)
ret,img_thresh = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(img_thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
03_cv2.drawContours
import cv2
img = cv2.imread('car.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,img_thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(img_thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
img2 = img.copy()
result = cv2.drawContours(img2,contours,-1,(0,0,255),2)
cv2.imshow('',result)
cv2.waitKey()
cv2.destroyAllWindows()
运行结果:
04_cv2.contoursArea_cv2.arcLength.py
import cv2
img = cv2.imread('car.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,img_thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(img_thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
print(cv2.contourArea(cnt))
print(cv2.arcLength(cnt,True))
05_cv2.approxPolyDP
import cv2
img = cv2.imread('contours2.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,img_thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours,hierarchy = cv2.findContours(img_thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
epsilon1 = 0.1 * cv2.arcLength(cnt,True)
cnt2 = cv2.approxPolyDP(cnt,epsilon1,True)
img2 = img.copy()
result = cv2.drawContours(img2,[cnt2],-1,(0,0,255),2)
cv2.imshow('',result)
cv2.waitKey()
cv2.destroyAllWindows()
运行结果:
06_cv2.boundingRect
import cv2
img = cv2.imread('car.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours,h = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
x,y,w,h = cv2.boundingRect(cnt)
print(x,y,w,h)
07_cv2.rectangle
import cv2
img = cv2.imread('contours.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours,h = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
x,y,w,h = cv2.boundingRect(cnt)
rect = cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.imshow('',rect)
cv2.waitKey()
cv2.destroyAllWindows()
08_cv2.minEnclosingCircle
import cv2
img = cv2.imread('contours.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours,h = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
(x,y),r = cv2.minEnclosingCircle(cnt)
09_cv2.circle
import cv2
img = cv2.imread('contours.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours,h = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
(x,y),r = cv2.minEnclosingCircle(cnt)
x = int(x)
y = int(y)
r = int(r)
circle = cv2.circle(img,(x,y),r,(0,0,255),2)
cv2.imshow('',circle)
cv2.waitKey()
cv2.destroyAllWindows()
运行结果:
10_cv2.matchTemplate
import cv2
template = cv2.imread('face.jpg')
img = cv2.imread('lena.jpg')
res = cv2.matchTemplate(img,template,cv2.TM_SQDIFF_NORMED)
print(res.shape)
cv2.waitKey()
cv2.destroyAllWindows()
11_cv2.minMaxLoc
import cv2
template = cv2.imread('face.jpg')
img = cv2.imread('lena.jpg')
res = cv2.matchTemplate(img,template,cv2.TM_SQDIFF_NORMED)
min_value,max_value,min_loc,max_loc = cv2.minMaxLoc(res)
(x,y) = min_loc
(h,w) = template.shape[:2]
rect = cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
cv2.imshow('',rect)
cv2.waitKey()
cv2.destroyAllWindows()
运行结果:
图片素材