当前位置:网站首页>【opencv图像处理】-- 6.图像轮廓(面积周长)、多边形逼近、外接矩形、查找/绘制轮廓
【opencv图像处理】-- 6.图像轮廓(面积周长)、多边形逼近、外接矩形、查找/绘制轮廓
2022-08-05 20:30:00 【终问鼎】
希望有能力的朋友还是拿C++做。
本节讨论查找、绘制图像轮廓,轮廓的面积,周长,多边形逼近,多边形凸包,轮廓的外接矩形
1. 图形轮廓(contours)
具有相同颜色或灰度的连续点的曲线,轮廓是形状分析和物体的检测和识别中很有用
- 图形分析
- 物体的识别和检测
- 注意:
- 为了检测的准确性,首先要二值化或canny操作
- 画轮廓时会修改输入的图像,如果之后想继续使用原始图像,应该先将原始图像储存到其他变量
1.1 查找轮廓
- cv2.findContours(image, mode, method…)
- mode:查找轮廓模式
- RETR_EXTERNAL=0 只检测外围轮廓
- RETR_LIST=1 检测的轮廓不建立等级关系,检测所有轮廓
- RETR_CCOMP=2 每层最多两级,从小到大,从里到外
- RETR_TREE = 3 按照树形储存轮廓,从大到小,从右到左
- method轮廓近似方法
- CHAIN_APPROX_NONE 保存轮廓上的所有点
- CHAIN_APPROX_SIMPLE 只保存焦点,比如四边形只存储四个角,常用
- 返回值:contour和hierachy即轮廓和等级
- mode:查找轮廓模式
import cv2
import numpy as np
cat = cv2.imread('cat.jpeg')
img = cv2.cvtColor(cat, cv2.COLOR_BGR2GRAY)
#二值化,有两个返回值,阈值,结果
ret, binary = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
cv2.imshow('img', img)
# cv2.imshow('img2', binary)
contours, hierarchy = cv2.findContours(binary, mode = cv2.RETR_TREE, method=cv2.CHAIN_APPROX_SIMPLE)
print(contours)
cv2.waitKey(0)
cv2.destroyAllWindows()
1.2 绘制轮廓
- drawContours(image, contours, contourIdx, color[, thickness[, lineType[, hierarchy[, maxLevel[, offset]]]]])
- image 要绘制的轮廓图像
- contours: 轮廓点
- contouridx 要绘制的轮廓编号 -1表示绘制所有轮廓
- color 轮廓的颜色 (0,0,255)表示红色
- thickness 线宽 -1 表示全部填充
import cv2
import numpy as np
cat = cv2.imread('cat.jpeg')
img = cv2.cvtColor(cat, cv2.COLOR_BGR2GRAY)
#二值化,有两个返回值,阈值,结果
ret, binary = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
cat_cp = cat.copy()
contours, hierarchy = cv2.findContours(binary, mode = cv2.RETR_TREE, method=cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(cat_cp, contours, -1, (0, 0, 255), 2)
cv2.imshow('cat', cat)
cv2.imshow('contours', cat_cp)
cv2.waitKey(0)
cv2.destroyAllWindows()
1.3 轮廓的面积和周长
单位为像素,再找到轮廓后,会有很多细小的轮廓,可以通过轮廓面积进行过滤
- 轮廓面积 contourArea(contour)
- 轮廓周长 arcLengh(curve, closed)
- curve即轮廓
- closed是否是闭合的轮廓
import cv2
import numpy as np
cat = cv2.imread('cat.jpeg')
img = cv2.cvtColor(cat, cv2.COLOR_BGR2GRAY)
#二值化,有两个返回值,阈值,结果
ret, binary = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
cat_cp = cat.copy()
contours, hierarchby = cv2.findContours(binary, mode = cv2.RETR_TREE, method=cv2.CHAIN_APPROX_SIMPLE)
#一个轮廓的面积
area = cv2.contourArea(contours[0])
print(area)
#一个轮廓周长
perimeter = cv2.arcLength(contours[1], closed=True)
print(perimeter)
2. 多边形逼近
findContours后的轮廓信息contours可能过于复杂不平花,可以用approxPolyDP函数对该多边形曲线近似,这就是轮廓的多边形逼近。
- approxPolyDP就是以多边形去逼近轮廓,采用的是Douglas-Peucker算法(DP)
- DP算法简单,就是不断找多边形最远的点加入形成新的多边形,指导最短距离小于指定精度。
- approxPolyDP(curbe, epsilon, closed[])
- curve就是要近似逼近的轮廓
- epsilon 即DP算法使用的阈值,偏差值5分半开始有详解
- closed轮廓是否闭合
- approxPolyDP(curbe, epsilon, closed[])
import cv2
import numpy as np
hand = cv2.imread('hand.jpg')
img = cv2.cvtColor(hand, cv2.COLOR_BGR2GRAY)
#二值化,有两个返回值,阈值,结果
ret, binary = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
#hand_cp = img.copy()
hand_cp2 = img.copy()
contours, hierarchy = cv2.findContours(binary, mode = cv2.RETR_TREE, method=cv2.CHAIN_APPROX_SIMPLE)
#cv2.drawContours(hand_cp, contours, -1, (0, 0, 255), 2)
#cv2.imshow('cat', hand)
#cv2.imshow('contours', hand_cp)
print(len(contours))
#使用多边形逼近,近似模拟手的轮廓
#阈值给20,阈值越大,逼近效果越粗糙
approx = cv2.approxPolyDP(contours[0], 20, closed=True)
#本质还是一个轮廓数据
cv2.drawContours(hand_cp2, [approx], 0, (0, 0, 255), 2)
cv2.imshow('moni', hand_cp)
cv2.waitKey(0)
cv2.destroyAllWindows()
3. 多边形凸包
多边形逼近是高度近似,凸包和多边形逼近很像,只不过它是物体最外层的凸多边形,凸包是指完全包含所有轮廓,并且仅由轮廓上的点构成的多边形。
- 完全包含所有轮廓
- 凸包的每一处都是凸的,在凸包内,任意三个连续点的内角小于180°
- convexHull(points[, hull[, clockwise[r, eturnPoints[]]]])
- points 轮廓
- clockwise 顺时针绘制
import cv2
import numpy as np
hand = cv2.imread('hand.jpg')
img = cv2.cvtColor(hand, cv2.COLOR_BGR2GRAY)
#二值化
ret, binary = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
hand_cp2 = img.copy()
contours, hierarchy = cv2.findContours(binary, mode = cv2.RETR_TREE, method=cv2.CHAIN_APPROX_SIMPLE)
#计算凸包
hull = cv2.convexHull(contours[0])
cv2.namedWindow('moni', cv2.WINDOW_NORMAL)
cv2.resizeWindow('moni', 640, 480)
cv2.drawContours(hand_cp2, [hull], 0, (0, 0, 255), 2)
cv2.imshow('moni', hand_cp2)
cv2.waitKey(0)
cv2.destroyAllWindows()
4. 外接矩形
- 最小外接矩形 minAreaRect(points):有可能会有旋转
- 最大外接矩形 boundingRect(points):没有旋转,方方正正,返回4点坐标
import cv2
import numpy as np
cv2.namedWindow('img', cv2.WINDOW_NORMAL)
cv2.resizeWindow('img', 640, 480)
hand = cv2.imread('hand.jpg')
img = cv2.cvtColor(hand, cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(img, 150, 255, cv2.THRESH_BINARY)
hand_cp2 = img.copy()
contours, hierarchy = cv2.findContours(binary, mode = cv2.RETR_TREE, method=cv2.CHAIN_APPROX_SIMPLE)
# rect是一个rotated 的矩形,包含起始坐标(x,y),长宽,旋转角度
# 最小外接矩形
rect = cv2.minAreaRect(contours[540])
print(rect)
# 最大外接矩形,方方正正,返回四个点x,y w,h
x,y,w,h = cv2.boundingRect(contours[540])
cv2.rectangle(hand_cp2, (x,y), (x+w, y+h), (0,0,255), 2)
#画出矩形,画旋转矩形有用的
#帮我们计算出旋转矩形的4个坐标点
#坐标为整数
box = cv2.boxPoints(rect)
#四舍五入,再取整
box = np.round(box).astype('int')
cv2.drawContours(hand_cp2, [box], 0, (255,0,0), 2)
cv2.imshow('img', hand_cp2)
cv2.waitKey(0)
cv2.destroyAllWindows()
边栏推荐
- Shell(5) array
- vi learning (1) [introduction/basic usage/work mode]
- 【Harmony OS】WebView调用JS并获取执行结果
- leetcode-137.只出现一次的数字 II
- 钉钉编辑在线表格突然闪退,整个钉钉就关了
- 【ML】机器学习数据集:sklearn中回归数据集介绍
- 【StoneDB子查询优化】subquery子查询-exists子查询的剔除遍历处理
- 爱看广场舞的老爷爷的笔记----strip命令
- Aurora push custom sound stepping record (continuous update)
- After importing the web project into idea, the blue dots of the file disappeared (web file resource root path)
猜你喜欢

【idea插件】MetricsReloaded使用

samba的运用以及LVS服务,LVS中DR模式

国内多位架构大牛强烈推荐的大型分布式手册

泰山OFFICE技术讲座:按照WORD的做法,底纹高亮边框的效果示意图

Win10 开机系统蓝屏代码0xc0000098,ntfs.sys损坏或丢失

Introducing Inheritance Abstract Classes

At this stage, how do you grasp the opportunities in the metaverse NFG industry?

【ML】机器学习数据集:sklearn中回归数据集介绍

PID Control Theory

D碳社区推荐读物:《碳中和与中国未来》:从大国博弈角度解读碳中和
随机推荐
波场Poloniex为行业首家支持ETH升级并上线潜在分叉币期货
数据分析的必备能力:数据敏感度是什么,应该怎样培养?
面试字节跳动—真实面试题分享
Day 15 (Part 1) & uploads and downloads of poetman
爱看广场舞的老爷爷的笔记----strip命令
【idea插件】MetricsReloaded使用
C语言中的三大自建类型
TypeError:列表索引必须是整数或片,不是str
Win10 Anaconda 虚拟环境安装 Pytorch GPU 2022-7-24
第十七章 源代码文件 REST API 教程(二)
TRON Poloniex is the first in the industry to support ETH upgrade and launch potential forked currency futures
C# implements the singleton pattern and implementation ideas
国产手机羡慕,中国年轻人追捧iPhone,推动苹果成最赚钱科技企业
day15--使用postman进行文件的上传与下载
使用compose实现康威生命游戏之二:我是如何将计算速度缩减将近十倍的
Introducing Inheritance Abstract Classes
PHP基础笔记-NO.3
【StoneDB子查询优化】subquery子查询-exists子查询的剔除遍历处理
【UiPath2022+C#】UiPathExcel 和数据表
【Harmony OS】【FAQ】鸿蒙问题合集2