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

Python Opencv实践 - 矩形轮廓绘制(直边矩形,最小外接矩形)

import cv2 as cv
import numpy as np
import matplotlib.pyplot as pltimg = cv.imread("../SampleImages/stars.png")
plt.imshow(img[:,:,::-1])img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
#通过cv.threshold转换为二值图
ret,thresh = cv.threshold(img_gray, 127, 255, 0)
plt.imshow(thresh, cmap=plt.cm.gray)#轮廓检测
contours,hierarchy = cv.findContours(thresh, 1, 2)
#绘制轮廓
img_contours_org = img.copy()
img_contours_org = cv.drawContours(img_contours_org, contours, -1, (0,255,0), 2)
plt.imshow(img_contours_org[:,:,::-1])img_rect_contour = img.copy()
for contour in contours:#1. 绘制直边界矩形#x,y,w,h = cv.boundingRect(contour)#contour: 轮廓信息#x,y,w,h: 矩形左上角(x,y)坐标,以及矩形的宽度和高度#参考资料:https://blog.csdn.net/hjxu2016/article/details/77833984x,y,w,h = cv.boundingRect(contour)img_rect_contour = cv.rectangle(img_rect_contour, (x,y), (x+w,y+h), (0,255,0), 2)#2. 绘制旋边界矩形结果#rect = cv.minAreaRect(contour)#contour:轮廓信息#rect: 最小外接矩阵的信息(中心(x,y),(w,h),旋转角度)#参考资料:https://blog.csdn.net/lanyuelvyun/article/details/76614872rect = cv.minAreaRect(contour)#使用boxPoints获得最小外接矩阵的4个顶点坐标box = cv.boxPoints(rect)#转换为int类型box = np.intp(box)#使用cv.polylines绘制外接矩形cv.polylines(img_rect_contour, [box], True, (0,0,255), 2)plt.imshow(img_rect_contour[:,:,::-1])

 

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

相关文章:

  • 大数据HBASE的详细使用
  • Sentinel 流量控制框架
  • leetcode原题: 跳水板
  • 深度学习入门(Python)学习笔记1
  • 苏州想要获得融资融券低利率账户的方法?怎么开融资融券账户?
  • 【LeetCode周赛】LeetCode第359场周赛
  • vue3+ts+tinynce在富文本编辑器菜单栏实现下拉框选择
  • 前端UI组件库深度解析:构建现代化的用户体验
  • leetcode 1326. Minimum Number of Taps to Open to Water a Garden
  • C++日期类的基本实现
  • 第六章:数据结构与算法-part3:数据结构算法提升
  • keras深度学习框架通过卷积神经网络cnn实现手写数字识别
  • Springboot启动异常 Command line is too long
  • PXE 装机(五十)
  • C++ 虚函数与纯虚函数
  • 警告:Provides transitive vulnerable dependency maven:org.yaml:snakeyaml:1.30
  • 中文命名实体识别
  • WPF CommunityToolkit.Mvvm Messenger通讯
  • 【杂言】写在研究生开学季
  • 渗透测试漏洞原理之---【任意文件读取漏洞】
  • 合宙Air724UG LuatOS-Air LVGL API控件-图片 (Image)
  • 仿京东 项目笔记2(注册登录)
  • Spark与Flink的区别
  • 未来智造:珠三角引领人工智能产业集群
  • 【Unity db】sqlite
  • Linux 指令心法(四)`touch` 创建一个新的空文件
  • 分类算法系列②:KNN算法
  • 12. 微积分 - 梯度积分
  • Large Language Models and Knowledge Graphs: Opportunities and Challenges
  • Python操作Excel教程(图文教程,超详细)Python xlwings模块详解,