图像金字塔与轮廓检测

图像金字塔

  • 高斯金字塔
  • 拉普拉斯金字塔

在这里插入图片描述

高斯金字塔

上采样与下采样:
在这里插入图片描述

拉普拉斯金字塔

在这里插入图片描述

down=cv2.pyrDown(img)
down_up=cv2.pyrUp(down)
l_1=img-down_up
cv_show(l_1,'l_1')

拉普拉斯金字塔第一层的结果:
在这里插入图片描述

图像轮廓

在这里插入图片描述
1、为了提高准确率,使用二值图像

img=cv2.imread('../res/contours.png')
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh=cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
cv_show(thresh,'thresh')
binary, contours, hierarchy=cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)

2、绘制轮廓

draw_img=img.copy()
result=cv2.drawContours(draw_img,contours,-1,(0, 0, 255), 2)
cv_show(result,'result')

在这里插入图片描述

轮廓特征

轮廓特征包括面积、周长、边界矩形、外接圆等。

cnt=contours[0]
# 面积
cv2.contourArea(cnt)
# 周长
cv2.arcLength(cnt,True)
# 边界矩形
x, y, w, h=cv2.boundingRect(cnt)

轮廓近似

img=cv2.imread('../res/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.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')

在这里插入图片描述

epsilon=0.1*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')

在这里插入图片描述

发布了102 篇原创文章 · 获赞 93 · 访问量 9632

猜你喜欢

转载自blog.csdn.net/Deep___Learning/article/details/103935443