import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
def cv_show(name,img):
cv2.imshow(name,img)
cv2.waitKey(0)
cv2.destroyAllWindows()
1 图像金字塔
- 高斯金字塔
- 拉普拉斯金字塔
高斯金字塔
高斯金字塔:向下采样方法(缩小)
高斯金字塔:向上采样方法(放大)
img = cv2.imread("AM.png")
cv_show("img",img)
plt.imshow(img)
print(img.shape)
(442, 340, 3)
up = cv2.pyrUp(img)
cv_show("ip",up)
plt.imshow(up)
print(up.shape)
(884, 680, 3)
down = cv2.pyrDown(img)
cv_show("down",down)
plt.imshow(down)
print(down.shape)
(221, 170, 3)
拉普拉斯金字塔
down=cv2.pyrDown(img)
down_up=cv2.pyrUp(down)
l_1=img-down_up
cv_show("l-1",1-1)
plt.imshow(l_1)
<matplotlib.image.AxesImage at 0x186bf016320>
2 图像轮廓
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("contours.png")
# 灰度
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 二值化阈值
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
cv_show("thresh",thresh)
plt.imshow(thresh)
<matplotlib.image.AxesImage at 0x186b17100f0>
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
绘制轮廓
cv_show("img",img)
plt.imshow(img)
<matplotlib.image.AxesImage at 0x186afca0940>
#传入绘制图像,轮廓,轮廓索引,颜色模式,线条厚度
# 注意需要copy,要不原图会变。。。
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, -1, (0, 0, 255), 2)
cv_show("res",res)
plt.imshow(res)
<matplotlib.image.AxesImage at 0x186afcac828>
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, 0, (0, 0, 255), 2)
cv_show("res",res)
plt.imshow(res)
<matplotlib.image.AxesImage at 0x186bfc04ef0>
轮廓特征
cnt = contours[0]
#面积
cv2.contourArea(cnt)
8500.5
#周长,True表示闭合的
cv2.arcLength(cnt,True)
437.9482651948929
轮廓近似
img = cv2.imread("contours2.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
draw_img = img.copy()
res = cv2.drawContours(draw_img, [cnt], -1, (0, 0, 255), 2)
cv_show("res",res)
plt.imshow(res)
<matplotlib.image.AxesImage at 0x186c008dd68>
epsilon = 0.15*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
draw_img = img.copy()
res = cv2.drawContours(draw_img, [approx], -1, (0, 0, 255), 2)
cv_show("res",res)
plt.imshow(res)
<matplotlib.image.AxesImage at 0x186bfbaa9b0>
边界矩形
img = cv2.imread("contours.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_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)
cv_show("img",img)
plt.imshow(img)
<matplotlib.image.AxesImage at 0x186bfd338d0>
area = cv2.contourArea(cnt)
x, y, w, h = cv2.boundingRect(cnt)
rect_area = w * h
extent = float(area) / rect_area
print ('轮廓面积与边界矩形比',extent)
轮廓面积与边界矩形比 0.5154317244724715
外接圆
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
img = cv2.circle(img,center,radius,(0,255,0),2)
cv_show("img",img)
plt.imshow(img)
<matplotlib.image.AxesImage at 0x186b1636748>