OpenCV图像处理技术(Python)——模板匹配与霍夫变换
© Fu Xianjun. All Rights Reserved.
文章目录
前言
经过了两次图像轮廓的学习,我们今天就要踏入模板匹配与霍夫变换的学习了。
学习目标
1.能够理解模板匹配的概念
2.能够使用模板匹配实现目标检测
3.能够理解霍夫变换的概念
4.能够根据场景使用霍夫变换
学习内容
一、模板匹配
1.模板匹配的概念
导包代码:
import cv2
import numpy as np
#Matplotlib是RGB
import matplotlib.pyplot as plt
%matplotlib inline
#定义显示图片的函数,避免重复代码
def cv_show(name, img):
cv2.imshow(name, img)
cv2.waitKey()
cv2.destroyAllWindows()
2.模板匹配的实现过程
3.模板匹配的函数使用
4.模板匹配的示例应用
1.读取模板图片
#读取模板图片
template = cv2.imread("lena_eye.jpg")
cv_show("template",template)
2.读取检测图片
img = cv2.imread("lena.jpg")
cv_show("img", img)
3.获取模板的大小
#获取到我们模板的大小h,w
h, w = template.shape[:2]
4.进行匹配
#开始模板匹配过程(采用计算归一化平方不同,计算值越接近0,越相关)
res = cv2.matchTemplate(img, template, cv2.TM_SQDIFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
top_left = min_loc
bottom_right = (top_left[0] + w, top_left[1] + h)
#画出检测到的部分
imgcpy = img.copy()
cv2.rectangle(imgcpy, top_left, bottom_right, 255, 2)
#因为matplotlib显示为RGB图像,做一次色彩空间空间转换
imgcpy = cv2.cvtColor(imgcpy, cv2.COLOR_BGR2RGB)
plt.imshow(imgcpy, cmap='gray')
二、霍夫变换
1.霍夫变换的概念
霍夫变换(Hough Transform)是图像处理中的一种特征提取技术,来检测任意能够用数学公式表达的形状,即使这个形状被破坏或者有点扭曲。
霍夫变换将图像空间转变为参数空间,在参数空间中执行投票来决定物体的形状。
2.霍夫变换的函数
3.霍夫变换案例解读
1.HoughLines
import cv2
import numpy as np
img = cv2.imread("shape.png")
# 1.轮廓检测算法检测出轮廓
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
# 2.投射到Hough空间进行形状检测
# 任何一条线都可以用(ρ,θ)这两个术语表示。
# 1)先定义一个累加器,(ρ,θ)对应直线,ρ和θ都分别依次增大(根据精度),计算每对(ρ,θ)的投票数。
# 其中,ρ以像素为单位,θ以弧度为单位。rho和theta是ρ和θ的精度。
# 2)然后,根据threshold(阈值,最低投票数)来判断是否归为一条直线
lines = cv2.HoughLines(edges, 1, np.pi / 180, 50)
# 画线
for line in lines:
rho, theta = line[0]
a = numpy.cos(theta)
b = numpy.sin(theta)
x0 = rho * a
y0 = rho * b
x1 = int(x0 + 1000 * (-b)) #这里的1000是为了求延长线,其他数值也可以
y1 = int(y0 + 1000 * a)
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * a)
# 画线
cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.imshow("img", img)
cv2.imshow("gray", gray)
cv2.imshow("edges", edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
以下是运行结果:
2.HoughLinesP
import cv2
import numpy as np
img = cv2.imread("shape.png")
# 1.轮廓检测算法检测出轮廓
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 150)
minLineLength = 10
maxLineGap = 30
lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 10,minLineLength,maxLineGap)
# 画线
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(img, (x1, y1), (x2, y2), (0, 255, 0), 2)
cv2.imshow("img", img)
cv2.imshow("gray", gray)
cv2.imshow("edges", edges)
cv2.waitKey(0)
cv2.destroyAllWindows()
以下是运行结果:
3.HoughCircles
import cv2
import numpy as np
img = cv2.imread("shape.png")
# 1.轮廓检测算法检测出轮廓
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 100)
# 2.投射到Hough空间进行形状检测
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 1, 30,\
param1=40, param2=20, minRadius=5, maxRadius=100)
# 画圆
if not circles is None:
# 转换为int
circles = np.uint16(numpy.around(circles))
for circle in circles:
x, y, r = circle[0]
# 画圆
cv2.circle(img, (x, y), r, (0, 0, 255), 2)
cv2.imshow("gray", gray)
cv2.imshow("edges", edges)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
以下是运行结果:
总结
今天的模板匹配与霍夫变换就学习到这里了,相信以兄弟们聪明的小脑瓜儿一定已经学会了。加油!