华域联盟 Python OpenCV半小时掌握基本操作之图像轮廓

OpenCV半小时掌握基本操作之图像轮廓

目录

【OpenCV】️高手勿入! 半小时学会基本操作 ️ 图像轮廓

概述

OpenCV 是一个跨平台的计算机视觉库, 支持多语言, 功能强大. 今天小白就带大家一起携手走进 OpenCV 的世界.

图像轮廓

cv2.findContours可以帮助我们查找轮廓.

格式:

cv2.findContours(image, mode, method, contours=None, hierarchy=None, offset=None)

参数:

image: 需要查找轮廓的图片

mode: 模式

  • RETR_EXTERNAL: 只检测最外面的轮廓
  • RETR_LIST: 检测所有的轮廓, 并将其保存到一条链表中
  • RETR_CCOMP: 检索所有的轮廓, 将他们组织为两层: 顶部是各分部法外部边界, 第二层是空洞的边界
  • RRTR_TREE: 检索所有的轮廓, 并重构嵌套轮廓的整个层次

method: 轮廓逼近的方法

  • CHAIN_APPROX_NONE: 以 Freeman 链码的方式输出轮廓, 所有其他方法输出多边形 (定点的序列)
  • CHAIN_APPROX_SIMPLE: 压缩水平的, 垂直的和斜的部分, 只保留他们的终点部分

返回值:

  • contours:轮廓本身
  • hierarchy: 轮廓的对应编号

原图:

绘制轮廓

cv2.drawContours可以实现轮廓绘制.

格式:

cv2.drawContours(image, contours, contourIdx, color, thickness=None, lineType=None, hierarchy=None, maxLevel=None, offset=None): 

参数:

  • image: 需要绘制轮廓的图片
  • contours: 轮廓
  • color: 颜色
  • thickness: 轮廓粗细

绘制所有轮廓:

# 读取图片
img = cv2.imread("contours.jpg")

# 转换成灰度图
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)

# 绘制轮廓
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, -1, (0, 0, 255), 2)

# 图片展示
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出结果:

绘制单个轮廓:

# 读取图片
img = cv2.imread("contours.jpg")

# 转换成灰度图
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)

# 绘制轮廓 (单一)
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, 0, (0, 0, 255), 2)

# 图片展示
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出结果:

轮廓特征

# 获取轮廓
cnt = contours[0]  # 取第一个轮廓

# 面积
area = cv2.contourArea(cnt)
print("轮廓面积:", area)

# 周长, True表示合并
perimeter = cv2.arcLength(cnt, True)
print("轮廓周长:", perimeter)

输出结果:

轮廓面积: 8500.5
轮廓周长: 437.9482651948929

轮廓近似

原图:

代码:

# 读取图片
img = cv2.imread("contours2.jpg")

# 转换成灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# 二值化
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)

# 获取轮廓
contours, hieratchy = 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()

# 取外围轮廓
cnt = contours[0]

# 轮廓近似
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)

# 图片展示
cv2.imshow("res", res)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出结果:

直接绘制轮廓:

轮廓近似:

边界矩形

cv2.boundingRect可以帮助我们得到边界矩形的位置和长宽.

例子:

# 读取图片
img = cv2.imread("contours.jpg")

# 转换成灰度图
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)
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# 轮廓面积
area = cv2.contourArea(cnt)

# 边界矩形面积
rect_area = w * h

# 占比
extent = area / rect_area
print('轮廓面积与边界矩形比:', extent)

输出结果:

轮廓面积与边界矩形比: 0.5154317244724715

外接圆

cv2.minEnclosingCircle可以帮助我们得到外接圆的位置和半径.

例子:

# 读取图片
img = cv2.imread("contours.jpg")

# 转换成灰度图
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), radius = cv2.minEnclosingCircle(cnt)

# 获取图片
img = cv2.circle(img, (int(x), int(y)), int(radius), (255, 100, 0), 2)

# 图片展示
cv2.imshow("img", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

输出结果:

到此这篇关于OpenCV半小时掌握基本操作之图像轮廓的文章就介绍到这了,更多相关OpenCV图像轮廓内容请搜索华域联盟以前的文章或继续浏览下面的相关文章希望大家以后多多支持华域联盟!

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » OpenCV半小时掌握基本操作之图像轮廓

转载请保留出处和原文链接:https://www.cnhackhy.com/33713.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部