当前位置:首页>编程日记>正文

python opencv 画矩形框 外接矩形

画矩形


import cv2
image = cv2.imread('1.jpg')
cv2.rectangle(image, (xmin, ymin), (xmax, ymax), (0, 0, 255), 2)  
cv2.imwrite('2.jpg', image)

画圆点: 

  point_size = 1point_color = (0, 0, 255)  # BGRthickness = 4  # 可以为 0 、4、8for point in points:x1,y1=pointcv2.circle(img, (int(x1),int(y1)), point_size, point_color, thickness)

外接矩形,最小外接矩形,cnt其实就是轮廓坐标:

import osimport cv2
import joblib
import numpy as npimage=cv2.imread("first.jpg")roi=np.array([[10,10],[100,20],[100,150],[20,80]])x, y, w, h = cv2.boundingRect(roi)
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)# 最小外接矩形框,有方向角
rect = cv2.minAreaRect(roi)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(image, [box], 0, (0, 0, 255), 2)

以下内容转自:

OpenCv-Python 最小外接圆、最小外接矩形、椭圆拟合、直线拟合_牧野的博客-CSDN博客

最小外接圆、最小外接矩形、椭圆拟合、直线拟合


#coding: utf-8
import cv2
import numpy as np
import imutilsim = cv2.imread('./5121.png')
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)ret,thresh = cv2.threshold(imgray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)  # 大津阈值
contours = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)  #cv2.RETR_EXTERNAL 定义只检测外围轮廓cnts = contours[0] if imutils.is_cv2() else contours[1]  #用imutils来判断是opencv是2还是2+for cnt in cnts:# 外接矩形框,没有方向角x, y, w, h = cv2.boundingRect(cnt)cv2.rectangle(im, (x, y), (x + w, y + h), (0, 255, 0), 2)# 最小外接矩形框,有方向角rect = cv2.minAreaRect(cnt)box = cv2.cv.Boxpoints() if imutils.is_cv2()else cv2.boxPoints(rect)box = np.int0(box)cv2.drawContours(im, [box], 0, (0, 0, 255), 2)# 最小外接圆(x, y), radius = cv2.minEnclosingCircle(cnt)center = (int(x), int(y))radius = int(radius)cv2.circle(im, center, radius, (255, 0, 0), 2)# 椭圆拟合ellipse = cv2.fitEllipse(cnt)cv2.ellipse(im, ellipse, (255, 255, 0), 2)# 直线拟合rows, cols = im.shape[:2][vx, vy, x, y] = cv2.fitLine(cnt, cv2.DIST_L2, 0, 0.01, 0.01)lefty = int((-x * vy / vx) + y)righty = int(((cols - x) * vy / vx) + y)im = cv2.line(im, (cols - 1, righty), (0, lefty), (0, 255, 255), 2)cv2.imshow('a',im)
cv2.imwrite('./result.jpg',im)
cv2.waitKey(0)


http://www.coolblog.cn/news/6e6c1f77b4e21410.html

相关文章:

  • asp多表查询并显示_SpringBoot系列(五):SpringBoot整合Mybatis实现多表关联查询
  • s7day2学习记录
  • 【求锤得锤的故事】Redis锁从面试连环炮聊到神仙打架。
  • 矿Spring入门Demo
  • 拼音怎么写_老师:不会写的字用圈代替,看到孩子试卷,网友:人才
  • Linux 实时流量监测(iptraf中文图解)
  • Win10 + Python + GPU版MXNet + VS2015 + RTools + R配置
  • 美颜
  • shell访问php文件夹,Shell获取某目录下所有文件夹的名称
  • 如何优雅的实现 Spring Boot 接口参数加密解密?
  • LeCun亲授的深度学习入门课:从飞行器的发明到卷积神经网络
  • Mac原生Terminal快速登录ssh
  • java受保护的数据与_Javascript类定义语法,私有成员、受保护成员、静态成员等介绍...
  • mysql commit 机制_1024MySQL事物提交机制
  • 支撑微博千亿调用的轻量级RPC框架:Motan
  • jquery 使用小技巧
  • 2019-9
  • 法拉利虚拟学院2010 服务器,法拉利虚拟学院2010
  • vscode pylint 错误_将实际未错误的py库添加到pylint白名单
  • 科学计算工具NumPy(3):ndarray的元素处理
  • 工程师在工作电脑存 64G 不雅文件,被公司开除后索赔 41 万,结果…
  • linux批量创建用户和密码
  • newinsets用法java_Java XYPlot.setInsets方法代碼示例
  • js常用阻止冒泡事件
  • 气泡图在开源监控工具中的应用效果
  • 各类型土地利用图例_划重点!国土空间总体规划——土地利用
  • php 启动服务器监听
  • dubbo简单示例
  • 【设计模式】 模式PK:策略模式VS状态模式
  • [iptables]Redhat 7.2下使用iptables实现NAT
  • Ubuntu13.10:[3]如何开启SSH SERVER服务
  • CSS小技巧——CSS滚动条美化
  • JS实现-页面数据无限加载
  • 阿里巴巴分布式服务框架 Dubbo
  • 最新DOS大全
  • Django View(视图系统)
  • 阿里大鱼.net core 发送短信
  • 程序员入错行怎么办?
  • 两张超级大表join优化
  • 第九天函数
  • Linux软件安装-----apache安装
  • HDU 5988 最小费用流
  • Sorenson Capital:值得投资的 5 种 AI 技术
  • 《看透springmvc源码分析与实践》读书笔记一
  • 正式开课!如何学习相机模型与标定?(单目+双目+鱼眼+深度相机)
  • Arm芯片的新革命在缓缓上演
  • nagios自写插件—check_file
  • python3 错误 Max retries exceeded with url 解决方法
  • 行为模式之Template Method模式
  • 通过Spark进行ALS离线和Stream实时推荐