当前位置: 首页 > news >正文

OpenCV-Python实战(13)——图像轮廓

一、找轮廓 cv2.findContours()

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

contours:找到的所有轮廓数组,数组内的元素为轮廓像素点坐标。

hierarchy轮廓间的层次关系。

image:二值图像(cv2.threshold())。

mode:轮廓检测模式,常见方法如下:

模式解释
RETR_EXTERNAL0只检测外部轮廓
RETR_LIST1检测所有轮廓,但不建立层级关系
RETR_CCOMP2检测所有轮廓,同时建立两个层级关系,如果内部还有轮廓则此轮廓与最外层轮廓同级
RETR_TREE3检测所有轮廓,同时建立一个树状层级关系

method:保存轮廓的方法,常见方法如下:

方法解释
CHAIN_APPROX_NONE1存储所有轮廓点坐标
CHAIN_APPROX_SIMPLE2只保存轮廓顶点坐标
CHAIN_APPROX_TC89L13使用CHAIN_APPROX_TC89L1 近视算法保存轮廓坐标
CHAIN_APPROX_TC89KCOS4使用CHAIN_APPROX_TC89KCOS近视算法保存轮廓坐标

二、绘轮廓 cv2.drawContours()

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

 img:目标图像。

image:二值图像,用于填画上轮廓。

contours:cv2.findContours()函数返回的轮廓列表 list。

contourIdx:需要绘制的轮廓,在轮廓列表中的索引。-1 表示绘制列表中的所有轮廓。

color:(B,G,R)颜色。

thickness:轮廓粗细,-1 表示实心。

lineType:线条类型。

hierarchy:cv2.findContours() 输出的层次关系。

maxLevel:轮廓层次关系的深度,0表示绘制第0层次关系的轮廓。

offset:常数值,轮廓偏移量(相较于原轮廓坐标)

三、检测模式 

3.1 外轮廓 RETR_EXTERNAL

import cv2
# 图像前处理
img = cv2.imread('contours.png')  # 原图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # GRAY
thresh,img_threshold = cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY)  # 二值contours,hierarchy = cv2.findContours(img_threshold,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
img1 = cv2.drawContours(img,contours,-1,(255,0,0),3)cv2.imshow('img',img)
cv2.imshow('img_threshold',img_threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()

 3.2 所有轮廓 cv2.RETR_LIST

import cv2
# 图像前处理
img = cv2.imread('contours.png')  # 原图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # GRAY
thresh,img_threshold = cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY)  # 二值contours,hierarchy = cv2.findContours(img_threshold,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
img1 = cv2.drawContours(img,contours,-1,(255,0,0),2)cv2.imshow('img',img)
cv2.imshow('img_threshold',img_threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()

 3.3 RETR_CCOMP

import cv2
import numpy as np# 图像前处理
img = cv2.imread('m.png')  # 原图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # GRAY
thresh,img_threshold = cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY)  # 二值contours,hierarchy = cv2.findContours(img_threshold,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
print(hierarchy)
img1 = cv2.drawContours(img,contours,-1,(255,0,0),2)cv2.imshow('img',img)
cv2.imshow('img_threshold',img_threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()

 hierarchy:详细解释请参考:《OpenCV计算机视觉项目实战(Python版)---p265》

print(hierarchy)结果
[[[ 1 -1 -1 -1][-1  0  2 -1][ 3 -1 -1  1][-1  2 -1  1]]]

 3.4 RETR_TREE

import cv2
import numpy as np# 图像前处理
img = cv2.imread('m.png')  # 原图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # GRAY
thresh,img_threshold = cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY)  # 二值contours,hierarchy = cv2.findContours(img_threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print(hierarchy)
img1 = cv2.drawContours(img,contours,-1,(255,0,0),2)cv2.imshow('img',img)
cv2.imshow('img_threshold',img_threshold)
cv2.waitKey(0)
cv2.destroyAllWindows()

  hierarchy:详细解释请参考:《OpenCV计算机视觉项目实战(Python版)---p265》

print(hierarchy) 结果:
[[[-1 -1  1 -1][ 3 -1  2  0][-1 -1 -1  1][-1  1 -1  0]]]

四、轮廓面积、周长

4.1 面积 cv2.contourArea()

area = cv2.contourArea(contour=*,oriented=*)

area:轮廓面积。

countour:要计算轮廓。

oriented:默认为:False,换回面积的绝对值。

import cv2
import numpy as np# 图像前处理
img = cv2.imread('contours.png')  # 原图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # GRAY
thresh,img_threshold = cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY)  # 二值contours,hierarchy = cv2.findContours(img_threshold,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
img1 = cv2.drawContours(img,contours,-1,(255,0,0),2)
areas = []
for i in range(len(contours)):area = cv2.contourArea(contours[i])areas.append(area)
print(areas)
cv2.waitKey(0)
cv2.destroyAllWindows()
[8500.5, 15986.0, 11396.0, 11560.0, 7136.5]

4.2  面积 cv2.arcLength()

arc = cv2.arcLength(contours,closed=*)

arc:轮廓周长。

countours:要计算轮廓。

closed:Ture表示轮廓是封闭的。

import cv2
import numpy as np# 图像前处理
img = cv2.imread('contours.png')  # 原图
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  # GRAY
thresh,img_threshold = cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY)  # 二值contours,hierarchy = cv2.findContours(img_threshold,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
img1 = cv2.drawContours(img,contours,-1,(255,0,0),2)
areas = []
for i in range(len(contours)):arc = cv2.arcLength(contours[i],closed=True)areas.append(arc)
print(areas)
cv2.waitKey(0)
cv2.destroyAllWindows()
[437.9482728242874, 492.6173119544983, 696.3086559772491, 403.98989498615265, 558.1147834062576]

http://www.lryc.cn/news/514120.html

相关文章:

  • javascript变量
  • 在K8S中,如何查看kubelet组件的日志?
  • android studio android sdk下载地址
  • Fetch处理大模型流式数据请求与解析
  • FPGA自学之路:到底有多崎岖?
  • 从0到机器视觉工程师(二):封装调用静态库和动态库
  • [极客大挑战 2019]Knife1
  • 【在Python中生成随机字符串】
  • 【three.js】场景搭建
  • Singleton: WebRTC中ThreadManager中的单例模式
  • MySQL数据库笔记——多版本并发控制MVCC
  • 【0x0037】HCI_Write_Link_Supervision_Timeout命令详解
  • Linux下如何进行内存泄漏分析
  • Colyseus Metadata 详解
  • C语言day5:shell脚本
  • 微记录-Linux字符设备的write函数如何避免文件系统重复调用?
  • 本地调试自定义Maven Plugin步骤
  • 二、github基础
  • 如何在 Vue 2 中使用 Swiper 5.4.5 处理静态与后端数据不能切换问题
  • request.getSession().getAttribute(Constants.ADMIN_ID)
  • 线性回归模型的构建与训练
  • 【JavaWeb后端学习笔记】MySQL的常用函数(字符串函数,数值函数,日期函数,流程函数)
  • 【推送】主流的服务端推送技术的对比
  • 直观解读 JuiceFS 的数据和元数据设计(一)
  • nginx配置文件没有语法颜色
  • PCB层叠结构设计
  • 电子应用设计方案83:智能 AI 打印机系统设计
  • windows安装rsync Shell语句使用rsync
  • Django 模型
  • CentOS — 压缩解压