opencv rectangle函数(python连接opencv库)

因为做程序图像剪切一直不太明白是怎么切片的,这里就用cv2.rectangle这个函数来看一下opencv是怎么计量图像的坐标轴的。opencv官网上给出的cv2.rectangle函数定义如下:Python:cv2.rectangle(img,pt1,pt2,color[,thickness[,lineType[,shift]]])→Noneimg–…

大家好,又见面了,我是你们的朋友全栈君。

因为做程序图像剪切一直不太明白是怎么切片的,这里就用 cv2.rectangle 这个函数来看一下 opencv 是怎么计量图像的坐标轴的。

cv2.rectangle 这个函数的作用是在图像上绘制一个简单的矩形。

opencv 官网上给出的 cv2.rectangle 函数定义 如下:

Python: cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) → None

  • img – Image.
  • pt1 – Vertex of the rectangle.
  • pt2 – Vertex of the rectangle opposite to pt1 .
  • color – Rectangle color or brightness (grayscale image).
  • thickness – Thickness of lines that make up the rectangle. Negative values, like CV_FILLED , mean that the function has to draw a filled rectangle.
  • lineType – Type of the line. See the line() description.
    • 8 (or omitted) – 8-connected line.
    • 4 – 4-connected line.
    • CV_AA – antialiased line.
  • shift – Number of fractional bits in the point coordinates.

这感觉说的不详细,不知道是不是我找的有问题。

图片

我们比较关系的是 pt1 和 pt2 这两个参数是什么含义。下面我就用一个程序为大家说明,我们程序用的图如下
图来自 https://blog.csdn.net/lonelyrains/article/details/50388999
在这里插入图片描述

pt1 和 pt2 参数

我们可以看到这个图十分的规整,你把它下下来后就可以发现它是 1200×750 的。因此每一个人物的大小就是 240×375,我们就利用这个规整性来探究一下那两个参数是什么意思。

import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\20151223182909577.png")
print(img.shape)  # 图片大小
cv2.rectangle(img, (240, 0), (480, 375), (0, 255, 0), 2)
cv2.imshow("fff", img)

输出 (750, 1200, 3) 3 是指 3 通道,表示这个图片宽度是 1200 像素,高度是 750像素。

参考 Accessing Image Properties
在这里插入图片描述
然后根据 stackoverflow 的图示 https://stackoverflow.com/questions/23720875/how-to-draw-a-rectangle-around-a-region-of-interest-in-python

import cv2
cv2.rectangle(img, (x1, y1), (x2, y2), (255,0,0), 2)

x1,y1 ------
|          |
|          |
|          |
--------x2,y2

我们就可以很容易的得出结论 cv2.rectangle 的 pt1 和 pt2 参数分别代表矩形的左上角和右下角两个点,而且 x 坐标轴是水平方向的,y 坐标轴是垂直方向的。

− − − − − − − − − − − − − − > x ————–>x >x
∣ |
∣    x 1 , y 1 − − − − − − |\space \space x_1,y_1 ——   x1,y1
∣    ∣                                   ∣ |\space \space |\space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space |                                    
∣    ∣                                   ∣ |\space \space |\space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space |                                    
∣    ∣                                   ∣ |\space \space |\space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space \space |                                    
∣    ∣ − − − − − − − − x 2 , y 2 |\space \space | ——–x_2,y_2   x2,y2
∣ |
∨ \vee
y y y

color 参数

color 参数一般用 RGB 值指定,表示矩形边框的颜色。RGB 对应的颜色可以使用 https://www.sioe.cn/yingyong/yanse-rgb-16/ 查看。

import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\20151223182909577.png")
print(img.shape)
cv2.rectangle(img, (240, 0), (480, 375), (0, 0, 255), 2)
cv2.imshow("fff", img)

在这里插入图片描述
需要注意的是这里的 (0, 0, 255) 三个分别对应 B G R。(不太懂为什么)

thickness 参数

thickness 参数表示矩形边框的厚度,如果为负值,如 CV_FILLED,则表示填充整个矩形。

import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\20151223182909577.png")
print(img.shape)
cv2.rectangle(img, (240, 0), (480, 375), (0, 255, 0), -1)
cv2.imshow("fff", img)

在这里插入图片描述

import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\20151223182909577.png")
print(img.shape)
cv2.rectangle(img, (240, 0), (480, 375), (0, 255, 0), 10)
cv2.imshow("fff", img)

在这里插入图片描述

lineType 参数

line() function 中有这样一段说明:

The function line draws the line segment between pt1 and pt2 points in the image. The line is clipped by the image boundaries. For non-antialiased lines with integer coordinates, the 8-connected or 4-connected Bresenham algorithm is used. Thick lines are drawn with rounding endings. Antialiased lines are drawn using Gaussian filtering. To specify the line color, you may use the macro CV_RGB(r, g, b)

这个参数看上去是指定 Bresenham 算法是 4 连通的还是 8 连通的,涉及到了计算机图形学的知识。如果指定为 CV_AA,则是使用高斯滤波器画反锯齿线。

shift 参数

shift 参数表示点坐标中的小数位数,但是我感觉这个参数是在将坐标右移 shift 位一样。shift 为 1 就相当于坐标全部除以 2 1 2^1 21,shift 为 2 就相当于坐标全部除以 2 2 2^2 22

import cv2
img = cv2.imread(r"C:\Users\Administrator\Desktop\20151223182909577.png")
print(img.shape)
cv2.rectangle(img, (240*2*2, 0), (480*2*2, 375*2*2), (0, 255, 0), 2, shift=2)
cv2.imshow("fff", img)

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/129572.html原文链接:https://javaforall.net

(1)
上一篇 2022年4月14日 上午8:40
下一篇 2022年4月14日 上午8:40


相关推荐

  • 敏感词过滤器的实现

    敏感词过滤器的实现敏感词过滤器的实现导包敏感词文件前缀树的实现敏感词过滤器的实现导包本文的敏感词过滤器用在SpringBoot项目中,因此,首先需要在pom.xml文件中导入如下依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency><depend

    2022年6月11日
    37
  • C++模板(函数模板/类模板)

    C++模板(函数模板/类模板)文章目录一 泛型编程二 函数模板一 泛型编程在引入泛型编程之前 我们先来看这样一个问题 怎么做到实现一个通用的交换函数呢 在 C 语言阶段我们可能会像下面这样写 需要分别实现不同类型的交换函数 又由于 C 语言不允许出现同名函数 所以函数名也需要不一样 写起来很繁琐 voidSwapi int e1 int e2 inttmp e1 e1 e2 e2 tmp voidSwapd double e1 double e2 doubletmp

    2026年3月18日
    2
  • 保姆级教程 本地化部署DeepSeek-7b大模型 构建私有知识库

    保姆级教程 本地化部署DeepSeek-7b大模型 构建私有知识库

    2026年3月16日
    2
  • 手部识别模型

    手部识别模型

    2026年3月15日
    3
  • 数据库基础:select基本查询语句

    数据库基础:select基本查询语句数据库基本查询语句规范为:select区域from表名查询指定表select*from表名*:代表所有列示例:select*fromTL_REQUEST查询指定列select列名from表名列名:代表从指定的列名中查找,:如果是查找对应的多列,则用英文逗号间隔示例:selectBU_NOfromTL_REQUEST…

    2022年6月13日
    31
  • Python机器学习-分类「建议收藏」

    Python机器学习-分类「建议收藏」监督学习下的分类模型,主要运用sklearn实践kNN分类器kNN分类器决策树决策树朴素贝叶斯朴素贝叶斯实战一:预测股市涨跌实战一:预测股市涨跌实战二:通过运动传感器采集的数据分析运

    2022年7月5日
    24

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号