OpenCV-Python是旨在解决计算机视觉问题的Python绑定库。cv2.rectangle()方法用于在任何图像上绘制矩形。

用法: cv2.rectangle(image, start_point, end_point, color, thickness)

参数:

image:它是要在其上绘制矩形的图像。

start_point:它是矩形的起始坐标。坐标表示为两个值的元组,即(X坐标值,Y坐标值)。

end_point:它是矩形的结束坐标。坐标表示为两个值的元组,即(X坐标值ÿ坐标值)。

color:它是要绘制的矩形的边界线的颜色。对于BGR,我们通过一个元组。例如:(255,0,0)为蓝色。

thickness:它是矩形边框线的粗细像素。厚度-1像素将以指定的颜色填充矩形形状。

返回值:它返回一个图像。

用于以下所有示例的图像:

示例1:

# Python program to explain cv2.rectangle() method

# importing cv2

import cv2

# path

path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'

# Reading an image in default mode

image = cv2.imread(path)

# Window name in which image is displayed

window_name = 'Image'

# Start coordinate, here (5, 5)

# represents the top left corner of rectangle

start_point = (5, 5)

# Ending coordinate, here (220, 220)

# represents the bottom right corner of rectangle

end_point = (220, 220)

# Blue color in BGR

color = (255, 0, 0)

# Line thickness of 2 px

thickness = 2

# Using cv2.rectangle() method

# Draw a rectangle with blue line borders of thickness of 2 px

image = cv2.rectangle(image, start_point, end_point, color, thickness)

# Displaying the image

cv2.imshow(window_name, image)

输出:

示例2:

使用-1 px的厚度用黑色填充矩形。

# Python program to explain cv2.rectangle() method

# importing cv2

import cv2

# path

path = r'C:\Users\Rajnish\Desktop\geeksforgeeks\geeks.png'

# Reading an image in grayscale mode

image = cv2.imread(path, 0)

# Window name in which image is displayed

window_name = 'Image'

# Start coordinate, here (100, 50)

# represents the top left corner of rectangle

start_point = (100, 50)

# Ending coordinate, here (125, 80)

# represents the bottom right corner of rectangle

end_point = (125, 80)

# Black color in BGR

color = (0, 0, 0)

# Line thickness of -1 px

# Thickness of -1 will fill the entire shape

thickness = -1

# Using cv2.rectangle() method

# Draw a rectangle of black color of thickness -1 px

image = cv2.rectangle(image, start_point, end_point, color, thickness)

# Displaying the image

cv2.imshow(window_name, image)

输出:

python cv.rectangle_Python OpenCV cv2.rectangle()用法及代码示例相关推荐

  1. python axes_Python Matplotlib.axes.Axes.axis()用法及代码示例

    Matplotlib是Python中的一个库,它是数字的-NumPy库的数学扩展.轴类包含大多数图形元素:Axis,Tick,Line2D,Text,Polygon等,并设置坐标系. Axes实例通过 ...

  2. python memory usage_Python Pandas Index.memory_usage()用法及代码示例

    Python是进行数据分析的一种出色语言,主要是因为以数据为中心的python软件包具有奇妙的生态系统. Pandas是其中的一种,使导入和分析数据更加容易. Pandas Index.memory_ ...

  3. python iloc iat_Python Pandas Dataframe.iat[ ]用法及代码示例

    Python是进行数据分析的一种出色语言,主要是因为以数据为中心的Python软件包具有奇妙的生态系统. Pandas是其中的一种,使导入和分析数据更加容易. Pandas iat []方法用于返回数 ...

  4. python barh_Python matplotlib.axes.Axes.barh()用法及代码示例

    Matplotlib是Python中的一个库,它是数字的-NumPy库的数学扩展.轴类包含大多数图形元素:Axis,Tick,Line2D,Text,Polygon等,并设置坐标系. Axes实例通过 ...

  5. python中 mul_Python Pandas Series.mul()用法及代码示例

    Python是进行数据分析的一种出色语言,主要是因为以数据为中心的Python软件包具有奇妙的生态系统. Pandas是其中的一种,使导入和分析数据更加容易. Python Series.mul()用 ...

  6. python中strftime函数_Python strftime()用法及代码示例

    在Python中,日期和时间不是其自身的数据类型,而是名为 strftime()函数用于将日期和时间对象转换为其字符串表示形式.它需要一个或多个格式化代码输入,并返回字符串表示形式. 用法: strf ...

  7. python cos函数_Python Tensorflow cos()用法及代码示例

    Tensorflow是Google开发的开源机器学习库.它的应用之一是开发深度神经网络. 模块tensorflow.math为许多基本的数学运算提供支持.功能tf.cos()[别名tf.math.co ...

  8. python s append_Python Pandas Series.append()用法及代码示例

    Pandas 系列是带有轴标签的一维ndarray.标签不必是唯一的,但必须是可哈希的类型.该对象同时支持基于整数和基于标签的索引,并提供了许多方法来执行涉及索引的操作. Pandas Series. ...

  9. python中repeat_Python Pandas Series.repeat()用法及代码示例

    Pandas 系列是带有轴标签的一维ndarray.标签不必是唯一的,但必须是可哈希的类型.该对象同时支持基于整数和基于标签的索引,并提供了许多方法来执行涉及索引的操作. Pandas Series. ...

最新文章

  1. java如何调用thrift_java – 我想在一个传输上使用多个服务(Thrift)
  2. Python中的线程、进程、协程以及区别
  3. python函数修饰符@的使用
  4. 前端学习(2693):重读vue电商网站14之步骤条的使用与美化
  5. Tez 0.9安装部署+hive on tez配置 + Tez-UI
  6. 实例方法 java 1614780086
  7. java获取响应网页源代码
  8. 电源大师课笔记 1.5
  9. 云智推任务提交版拉新系统源码-任务分销系统
  10. 零基础【绘画板绘】SAI仿水墨笔刷教程(干货)
  11. BLP读书摘录和笔记——make
  12. 【ShaderLab学习】AlphaTest AlphaBlend理解[2]
  13. B站网页端下载视频,直接浏览器下载或者Java实现下载
  14. WEBSHELL 提权方法总结
  15. Android学习笔记--菜单
  16. mybatis mapperLocations配置失效
  17. Docker学习(八):Docker可视化与监控
  18. NT_iOS笔记—判断iPhone6
  19. Dijkstra算法(最短路径)
  20. python编写程序,生成包含 20 个随机数的列表,然后将前 10 个元素升序排列,后 10 个元 素降序排列,并输出结果。

热门文章

  1. oracle的declare声明语法
  2. PhoneGap极光推送 cordova消息推送
  3. NAS与SAN架构:如何比较这两个存储系统
  4. gevent的碎碎念(一)——协程
  5. Android — 创建和修改 Fragment 的方法及相关注意事项
  6. 2016 版 Laravel 系列入门教程(三)【最适合中国人的 Laravel 教程】
  7. MonetDB heap bug?
  8. 关于40G/100G高速PHY的测试
  9. Android Handler机制分析
  10. C#系列四《C#运算符与表达式》