当前位置:网站首页>OPENCV study DAY10
OPENCV study DAY10
2022-08-06 11:55:00 【SKYWALKERS_2397】
图像轮廓
1、轮廓绘制
Use the built-in function directlycv2.findContours(img,mode,method)
# cv2.findContours(img,mode,method)
# mode:轮廓检索模式
# RETR_EXTERNAL:只检索最外面的轮廓
# RETR_LIST:检索所有的轮廓,并将其保存到一条链表中
# RETR_CCOMP:检索所有的轮廓,And violently organize him into two layers,顶层是各部分的外部边界,第二层是空洞的边界
# RETR_TREE:检索所有的轮廓,并重构嵌套轮廓的整个层次
# method:轮廓逼近方法
# CHAIN_APPROX_NONE:以Freeman链码的方式输出轮廓,All other ways output polygons(顶点的序列)
# CHAIN_APPROX_SIMPLE:压缩水平的、垂直的和斜的部分,也就是,函数只保留他们的终点部分
import cv2
img = cv2.imread('image/similar.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)
# 绘制轮廓
# 传入绘制图像,轮廓,轮廓索引,颜色模式,线条厚度
# 注意需要copy,Otherwise, the original image will change
draw_img = img.copy()
res = cv2.drawContours(draw_img,contours, -1, (0,0,255),1)
cv2.imshow('init',img)
cv2.imshow('res',res)
原图:
轮廓:
复杂图形:

2、轮廓特征
2.1面积和周长
cnt = contours[0]
# 面积
area = cv2.contourArea(cnt)
print(area)
# 周长,True表示闭合的
C = cv2.arcLength(cnt,True)
print(C)
2.2、轮廓近似
epsilon = 0.07*C
approx = cv2.approxPolyDP(cnt,epsilon, True)
draw_img = img.copy()
res1 = cv2.drawContours(draw_img,[approx], -1, (0,0,255),1)
cv2.imshow('res1',res1)

参数epsilon可自己设置,值越大,Approximate graphics are simpler
2.3、外接矩形
包括两种类型,Right-angled rectangles and rotated rectangles.
直角矩形不考虑物体的旋转,So his area is not the smallest.
Rotate the rectangle It also takes rotation into account,is drawn with minimum area.
# 外接矩形
#直角矩形
x,y,w,h = cv2.boundingRect(cnt)
rect_img = img.copy()
cv2.rectangle(rect_img,(x,y),(x+w,y+h),(0,255,0),1)
cv2.imshow('rect1',rect_img)
#旋转矩形
rect = cv2.minAreaRect(cnt)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(rect_img,[box],0,(0,0,255),2)
cv2.imshow('rect2',rect_img)
rect_area = w*h
extent = float(area)/rect_area
print('轮廓面积与边界矩形比',extent)
直角矩形:
旋转矩形:
2.4、外接圆
#外接圆
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
circle_img = img.copy()
cv2.circle(circle_img,center,radius,(0,255,0),2)
cv2.imshow('circle',circle_img)

边栏推荐
猜你喜欢

How to use wireless serial communication module to realize long-distance communication between touch screen and PLC?

LeetCode_50_Pow(x,n)

Notion 中文:客户端、网页端汉化方案

实践|美创助力“云学堂”数据安全风险管控建设

CISP-PTE实操练习题讲解一(新版)

Offer harvester!The giant's latest interview guide, quick to see see!

Golang gin 配置腾讯云cos实现单文件与多文件上传

Kubernetes 怎么优雅升级

Kubernetes微服务、容器介绍

AIRIOT答疑第7期|如何快速提升物联网项目交付速度?
随机推荐
The vant component van-dropdown-menu covers the list page in the applet, causing the bindtap click event to fail from time to time
Kubernetes operation and maintenance experience sharing
PG core technology articles--system fields in the table
教你画像素画每周分享195期
XML usage
RedTiger‘s Hackit -- level 10
stp simple configuration
Can't verify the mobile phone number of registered Google account in 2022?The last step of Google account registration is stuck on this number can not be used for verification on the successful soluti
CISP-PTE实操练习题讲解一(新版)
【C# 学习笔记 ①】C# 程序结构和基本语法(VSCode工具配置、数据类型、修饰符、构造函数)
The entry node of the ring in the NC3 linked list
ES6 new feature - generator
How to use wireless serial communication module to realize long-distance communication between touch screen and PLC?
后缀自动机(其二)
微软新服务,允许企业扩大对其威胁情报库的访问权限
CobalStrike(CS)上线隐藏IP和流量
Small program product list paging implementation
HCIP Day 15 Notes
Flexible and easy-to-use sql monitoring script part4
Kubernetes 集群故障案例