python利用opencv标注bounding box

http://blog.csdn.net/xieqiaokang/article/details/60780608

1. 函数

用 OpenCV 标注 bounding box 主要用到下面两个工具——cv2.rectangle() 和 cv2.putText()。用法如下:

# cv2.rectangle()
# 输入参数分别为图像、左上角坐标、右下角坐标、颜色数组、粗细
cv2.rectangle(img, (x,y), (x+w,y+h), (B,G,R), Thickness)# cv2.putText()
# 输入参数为图像、文本、位置、字体、大小、颜色数组、粗细
cv2.putText(img, text, (x,y), Font, Size, (B,G,R), Thickness)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

2. Example

import cv2fname = '001.jpg'
img = cv2.imread(fname)
# 画矩形框
cv2.rectangle(img, (212,317), (290,436), (0,255,0), 4)
# 标注文本
font = cv2.FONT_HERSHEY_SUPLEX
text = '001'
cv2.putText(img, text, (212, 310), font, 2, (0,0,255), 1)
cv2.imwrite('001_new.jpg', img)

documents:

https://docs.opencv.org/3.1.0/dc/da5/tutorial_py_drawing_functions.html

Goal

  • Learn to draw different geometric shapes with OpenCV
  • You will learn these functions : cv2.line(), cv2.circle() , cv2.rectangle(), cv2.ellipse(), cv2.putText() etc.

Code

In all the above functions, you will see some common arguments as given below:

  • img : The image where you want to draw the shapes
  • color : Color of the shape. for BGR, pass it as a tuple, eg: (255,0,0) for blue. For grayscale, just pass the scalar value.
  • thickness : Thickness of the line or circle etc. If **-1** is passed for closed figures like circles, it will fill the shape. default thickness = 1
  • lineType : Type of line, whether 8-connected, anti-aliased line etc. By default, it is 8-connected. cv2.LINE_AA gives anti-aliased line which looks great for curves.

Drawing Line

To draw a line, you need to pass starting and ending coordinates of line. We will create a black image and draw a blue line on it from top-left to bottom-right corners.

1 import numpy as np
2 import cv2
3 
4 # Create a black image
5 img = np.zeros((512,512,3), np.uint8)
6 
7 # Draw a diagonal blue line with thickness of 5 px
8 cv2.line(img,(0,0),(511,511),(255,0,0),5)

Drawing Rectangle

To draw a rectangle, you need top-left corner and bottom-right corner of rectangle. This time we will draw a green rectangle at the top-right corner of image.

1 cv2.rectangle(img,(384,0),(510,128),(0,255,0),3)

Drawing Circle

To draw a circle, you need its center coordinates and radius. We will draw a circle inside the rectangle drawn above.

1 cv2.circle(img,(447,63), 63, (0,0,255), -1)

Drawing Ellipse

To draw the ellipse, we need to pass several arguments. One argument is the center location (x,y). Next argument is axes lengths (major axis length, minor axis length). angle is the angle of rotation of ellipse in anti-clockwise direction. startAngle and endAngle denotes the starting and ending of ellipse arc measured in clockwise direction from major axis. i.e. giving values 0 and 360 gives the full ellipse. For more details, check the documentation of cv2.ellipse(). Below example draws a half ellipse at the center of the image.

1 cv2.ellipse(img,(256,256),(100,50),0,0,180,255,-1)

Drawing Polygon

To draw a polygon, first you need coordinates of vertices. Make those points into an array of shape ROWSx1x2 where ROWS are number of vertices and it should be of type int32. Here we draw a small polygon of with four vertices in yellow color.

1 pts = np.array([[10,5],[20,30],[70,20],[50,10]], np.int32)
2 pts = pts.reshape((-1,1,2))
3 cv2.polylines(img,[pts],True,(0,255,255))
Note
If third argument is False, you will get a polylines joining all the points, not a closed shape.
cv2.polylines() can be used to draw multiple lines. Just create a list of all the lines you want to draw and pass it to the function. All lines will be drawn individually. It is a much better and faster way to draw a group of lines than calling cv2.line() for each line.

Adding Text to Images:

To put texts in images, you need specify following things.

  • Text data that you want to write
  • Position coordinates of where you want put it (i.e. bottom-left corner where data starts).
  • Font type (Check cv2.putText() docs for supported fonts)
  • Font Scale (specifies the size of font)
  • regular things like color, thickness, lineType etc. For better look, lineType = cv2.LINE_AA is recommended.

We will write OpenCV on our image in white color.

1 font = cv2.FONT_HERSHEY_SIMPLEX
2 cv2.putText(img,'OpenCV',(10,500), font, 4,(255,255,255),2,cv2.LINE_AA)

Result

So it is time to see the final result of our drawing. As you studied in previous articles, display the image to see it.

drawing_result.jpg
image

Additional Resources

  1. The angles used in ellipse function is not our circular angles. For more details, visit this discussion.

Exercises

  1. Try to create the logo of OpenCV using drawing functions available in OpenCV.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/313116.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ImportError: libicui18n.so.56 and/or libicui18n.so.58 when importing cv2

自己的解决办法: 我的报错是lib中缺少.so.56,于是我从 https://anaconda.org/conda-forge/icu/files 里面下载了放到lib中,python就能正常import cv2了。 再放上比较靠谱的其他人的办法: https://github.com/conda-forge/geo…

微软发布 SQL Server 2019 新版本

2019 年 11 月 4 日,微软在美国奥兰多举办的 Ignite 大会上发布了关系型数据库 SQL Server 的新版本。与之前版本相比,新版本的 SQL Server 2019 具备以下重要功能:在 Linux 和容器中运行的能力,连接大数据存储系统的 PolyBase 技…

Ubuntu下查看显卡型号及NVIDIA驱动版本

查看GPU型号 lspci | grep -i nvidia查看NVIDIA驱动版本 sudo dpkg --list | grep nvidia-*http://gwang-cv.github.io/2017/08/07/Ubuntu%E4%B8%8B%E6%9F%A5%E7%9C%8B%E6%98%BE%E5%8D%A1%E5%9E%8B%E5%8F%B7%E5%8F%8ANVIDIA%E9%A9%B1%E5%8A%A8%E7%89%88%E6%9C%AC/

Visual Studio Online,带来四种开发模式,未来已来。

简单来说,Visual Studio Online 由两部分组成:“前端”与“后端”。 “前端”:VS Code、VS IDE 和 Web 版 VS Code。“后端”:由云服务支撑的开发环境。 Visual Studio Online 的前端可以是本地的 VS Code 或者 VS IDE&#xff0c…

AdminLTE 3.0发布了

点击蓝字关注我们前言在11月2日,作者正式发布了AdminLTE 3.0版本。该版本基于Bootstrap 4.x。使用Bootstrap 4.x的小伙伴可以愉快的使用AdminLTE。GithubAdminLTE是一个完全响应的管理模板。基于Bootstrap 4框架。高度可定制且易于使用。适合从小型移动设备到大型台…

ubuntu改变进程优先级从而防止卡

http://blog.csdn.net/loyachen/article/details/52167124 nice 还有一个关联命令叫做 renice,它可以在运行时调整进程的 nice 值。使用 renice 命令时,要先找出进程的 PID。下面是一个例子: renice 10 12341 其中,1234是进程…

这位优秀的.NET开发者是怎样炼成的?

本文来自DotNET技术圈作者:邹溪源一,社区的小圈子今年3月的一次技术交流活动上,那是我们.NET技术社区第一次组织线下活动,由于没什么经验,所以活动组织得比较仓促,内容也比较一般,效果还是有点欠…

Block Coordinate Descent

http://www.math.ucla.edu/~wotaoyin/papers/bcu/

求知无限,刷新.NET 中国社区

2019 Microsoft Ignite The Tour 2020年1月13日至14日深圳会展中心举办,今年的大会是免费的哦,所以也很火爆,我们为您开通专属报名渠道,,扫下方二位码 请在注册时务必填写RSVPCode: MITTCE。大会全面解锁微软黑科技:&g…

使用ASP.NET Core 3.x 构建 RESTful API - 1. 开始

以前写过ASP.NET Core 2.x的REST API文章,今年再更新一下到3.0版本。预备知识:ASP.NET Core 和 C# 工具:Visual Studio 2019最新版(VSCode、VS for Mac,Rider等也凑合),POSTMAN Web API Web API…

matlab处理中文路径

https://www.douban.com/note/387532266/matlab读取文本文件很方便,textread,textscan等就能搞定。如果整个文件仅包含数字,则有更方便的命令"load".但是一旦文件包含中文,则往往读出来的是乱码。经过搜索测试,发现救命…

.NET Core 3.1 编写混合 C++ 程序

前言随着 .NET Core 3.1 的第二个预览版本发布,微软正式将 C/CLI 移植到 .NET Core 上,从此可以使用 C 编写 .NET Core 的程序了。由于目前仅有 MSVC 支持编译此类混合代码,并且由于涉及到非托管代码,因此 C/CLI 目前不能跨平台&a…

ubuntu 16.04 安装 python2.7 以及 cv2, dist-package 和 site-package 的区别, import cv2 出问题解答

ubuntu16.04默认安装python3以上,需要手动安装2.7。1、先确保make、gcc、g已经安装好,如果没有则需要先安装这三个程序,均使用apt-get的方式直接装; 2、下载python2.7包:wget https://www.python.org/ftp/python/2.7.1…

在ASP.NET Core中编写合格的中间件

这篇文章探讨了让不同的请求去使用不同的中间件,那么我们应该如何配置ASP.NET Core中间件?其实中间件只是在ASP.NET Core中处理Web请求的管道。所有ASP.NET Core应用程序至少需要一个中间件来响应请求,并且您的应用程序实际上只是中间件的集合…

nvcc找不到的问题(Ubuntu16.04 CUDA 8.0)

http://blog.csdn.net/rtygbwwwerr/article/details/73656876在terminal中输入nvcc,也是提示找不到command。但是可以确定的是,CUDA8.0,以及nvidia-cuda-toolkit已经从官方网站下载并正确安装。 于是网上找了教程,说是需要在termi…

全网首发 PowerBI 秒级实时大屏通用解决方案

双十一来了,你准备好了吗?不管你是否准备完毕,我们带来了全网首发的 PowerBI 秒级实时大屏展示方案,你可以直接用来展示双十一的实时状况。我们一步步来说明这个套件模板教程。真实效果功能如下:全实时展示 双十一 当天…

windows 编译 caffe unresolved externals 问题

windows 10 编译基于 caffe 的工程, 由于github上作者所用到的版本各不相同,版本是导致问题的根本。 遇到 LNK20xx error unresolved externals,先找是哪个.obj 造成的, 这种link问题是由声明不正确造成的,我们找到那个…

普大喜奔 | Azure 免费送网站SSL证书啦!

点击上方蓝字关注“汪宇杰博客”导语就在今晨,微软推出了 App Service Managed Certificates 预览版。简单来说,这就是在 Azure App Service 服务的一项更新,免费送你SSL证书!只要点几下鼠标就能给网站加上SSL证书!并且…