简介
本节为《OpenCV计算机视觉实战(Python)》版第7讲,图像金字塔与轮廓检测,的总结。
总结
图像金字塔
- 高斯金字塔
- 拉普拉斯金字塔
金字塔的每一层特征可以采取不同的方式获得,这样每一层的特征都不同。
高斯金字塔介绍
高斯金字塔:向下采样放方法(缩小)
高斯金字塔:向上采样方法(扩大)
- 将图像在每个方向扩大为原来的两倍,新增的行和列以0填充
- 使用先前同样的内核(乘以4)与放大后的图像卷积,获得近似值
高斯金字塔程序
img = cv2.imread('AM.png')
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
up = cv2.pyrUp(img) # 上采样
cv2.imshow('up', up)
cv2.waitKey(0)
cv2.destroyAllWindows()
down = cv2.pyrUp(img)# 下采样
cv2.imshow('down', down)
cv2.waitKey(0)
cv2.destroyAllWindows()
拉普拉斯金字塔
拉普拉斯程序
down =cv2.pyrDown(img)
down_up = cv2.pyrUp(down)
l_1 = img - down_up
cv2.imshow('l_1', l_1)
cv2.waitKey(0)
cv2.destroyAllWindows()
图像轮廓
轮廓检索
cv2.findContours(img, mode, method)
- mode: 轮廓检索模式
- RETR_EXTERNAL:只检索最外面的轮廓
- RETR_LIST: 检索所有的轮廓,并将其保存到一条链表当中
- RETR_CCOMP: 检索所有的轮廓,并将它们组织为两层:顶层是各部分的外部边界,第二层是空洞的边界;
- RETR_TREE: 检索所有的轮廓,并重构嵌套轮廓的整个层次
- method:轮廓逼近方法
- CHAIN_APPROX_NONE: 以Freeman链码的方式输出轮廓,所有其他方法输出多边形(顶点的序列)。
- CHAIN_APPROX_SIMPLE: 压缩水平的、垂直的和斜的部分,也就是,函数只保留它们的终点部分。
轮廓检索程序
img = cv2.imread('car.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) # 阈值
cv2.imshow('thresh', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 寻找图像边缘
binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
# 绘制轮廓
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, 0, (0,0,255),2)
cv2.imshow('res',res)
cv2.waitKey(0)
cv2.destroyAllWindows()
轮廓查找函数:cv2.drawContours(draw_img, contours, 0, (0,0,255),2)
- draw_img:输入图像
- contours: 轮廓
- 第三个参数:表示轮廓的个数,如果为“-1”表示全部,如果为0表示第一个
- (0,0,255):表示图像的颜色
- 2:中间值就可以
轮廓特征:
cnt = contours[0]
# 面积
cv2.contourArea(cnt)
# 周长,True 表示闭合的
cv2.arcLength(cnt, TRUE)
轮廓包括外轮廓和内轮廓,因此相邻的两个轮廓可能是表示同一个部位的轮廓。
轮廓近似
img = cv2.imread('contours2.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127,255,cv2.THRESH_BINARY)
binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.APPROX_NONE)
cnt = contours[0]
draw_img = img.copy()
res = cv2.drawContours(draw_img, [cnt], -1, (0,0,255),2)
cv2.imshow('res',res)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 近似函数
epsilon = 0.1 * cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, TRUE)
近似函数:受函数控制
- epsilon一般与轮廓的周长有关系,再乘以比例系数,一般用0.1即可,越大边缘细节越弱
边界矩形
img = cv2.imread('contours.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
binary, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
# 边界矩形
x,y,w,h = cv2.boundingRect(cnt)
img = cv2.rectangle(img, (x,y), (x+w, y+h), (0,255,0), 2)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 轮廓面积与边界矩形比
area = cv2.contourArea(cnt)
x,y,w,h = cv2.boundingRect(cnt)
rect_area = w*h
extent = float(area)/rect_area
print('轮廓面积与边界矩形比', extent)
外接圆
# 外接圆
(x,y), radius = cv2.minEnclosingCircle(cnt)
center = (int(x), int(y))
radius = int(radius)
img = cv2.circle(img, center, radius, (0, 255,0), 2)
cv2.imshow('img', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
模板匹配
模板匹配和卷积原理很像:
- 模板在原图像上从原点开始滑动,计算模板与(图像被模板覆盖的地方)的差别程度, 这个差别程度的计算方法在OpenCV里有6种(3种归一化,3种非归一化)然后将每次计算的结果放入一个矩阵里,作为结果输出。
- 假如原图形是AB大小,而模板是ab大小,则输出结果的矩形是(A-a+1) * (B-b+1).
methods = ['','','','','','']
for meth in methods:
img2 = img.copy()
# 匹配方法的真值
method = eval(meth)
res = cv2.matchTemplate(img, template, method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
# 如果是平方差匹配TM_SQDIFF或归一化平方差匹配TM_SQDIFF_NORMED, 取最小值
if method in [cv2.IM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
top_left = min_loc
else:
top_left = max_loc
bottom_right = (top_loc[0] + w, top_left[1]+h)
# 画矩形
cv2.rectangle(img2, top_left, bottom_right, 255,2)
plt.subplot(121), plt.imshow(res, cmap='gray')
plt.xticks([]), plt.yticks([]) # 隐藏坐标轴
plt.subplot(122), plt.imshow(img2, cmap='gray')
plt.xticks([]), plt.yticks([])
plt.suptitle(meth)
plt.show()
匹配多个对象
img_rgb = cv2.imread('m.jpg')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('m_j.jpg', 0)
h,w = template.shape[:2]
res = cv2.matchTemplate(img_gray, template, cv.TM_CCOEFF_NORMED)
threshold = 0.8
# 取匹配程序大于%80的坐标
loc = np.where(res>=threshold)
for pt in zip(*loc[::-1]:
bottom_right = (pt[0] + w, pt[1] +h)
cv2.rectangle(img_rgb, pt, bottom_right, (0,0,255), 2)
cv2.imshow('img_rgb', img_rgb)
cv2.waitKey(0)
- opencv: 从左到右, 从上到下
- 如果是模板匹配的差异性,越小越好;尽量用归一化的匹配方式
- 多匹配:需要设置阈值