06 计算机视觉-opencv图像金字塔与轮廓检测

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)

[外链图片转存失败(img-KOkS3qDB-1565702097995)(output_8_1.png)]

up = cv2.pyrUp(img)
cv_show("ip",up)
plt.imshow(up)
print(up.shape)
(884, 680, 3)

[外链图片转存失败(img-ksM9MSwP-1565702097996)(output_9_1.png)]

down = cv2.pyrDown(img)
cv_show("down",down)
plt.imshow(down)
print(down.shape)
(221, 170, 3)

[外链图片转存失败(img-InCEUCIJ-1565702097998)(output_10_1.png)]

拉普拉斯金字塔

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>

[外链图片转存失败(img-F8Lr7EbS-1565702097998)(output_13_1.png)]

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>

[外链图片转存失败(img-BJYllc3u-1565702097998)(output_17_1.png)]

contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

绘制轮廓

cv_show("img",img)
plt.imshow(img)
<matplotlib.image.AxesImage at 0x186afca0940>

[外链图片转存失败(img-sA1KMxvU-1565702097999)(output_20_1.png)]

#传入绘制图像,轮廓,轮廓索引,颜色模式,线条厚度
# 注意需要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>

[外链图片转存失败(img-cp99kmho-1565702098000)(output_21_1.png)]

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>

[外链图片转存失败(img-lZ0zJJIh-1565702098000)(output_22_1.png)]

轮廓特征

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>

[外链图片转存失败(img-fJ3i9zYl-1565702098001)(output_29_1.png)]

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-DtV0zrK8-1565702098001)(output_30_1.png)]

边界矩形

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>

[外链图片转存失败(img-8H8u7T2J-1565702098002)(output_32_1.png)]

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>

[外链图片转存失败(img-poUVBW4c-1565702098002)(output_35_1.png)]


发布了478 篇原创文章 · 获赞 417 · 访问量 32万+

猜你喜欢

转载自blog.csdn.net/Mind_programmonkey/article/details/99476986