因为做程序图像剪切一直不太明白是怎么切片的,这里就用 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 参数
  • color 参数
  • thickness 参数
  • lineType 参数
  • shift 参数

图片

我们比较关系的是 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
∣|∣
∣x1,y1−−−−−−|\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 |∣  ∣                                 ∣
∣∣−−−−−−−−x2,y2|\space \space | --------x_2,y_2∣  ∣−−−−−−−−x2​,y2​
∣|∣
∨\vee∨
yyy

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 就相当于坐标全部除以 212^121,shift 为 2 就相当于坐标全部除以 222^222

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)

python opencv cv2.rectangle 参数含义相关推荐

  1. python opencv cv2在图片中画mask掩码/掩膜

    python opencv cv2在图片中画mask掩膜 import cv2 import numpy as np from PIL import Image import matplotlib.p ...

  2. python opencv cv2.imread

    python opencv cv2.imread 环境说明 opencv 安装 pip install opencv-python opencv:opencv_python 4.5.5.64 介绍 方 ...

  3. python cv.rectangle_Python OpenCV cv2.rectangle()用法及代码示例

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

  4. python opencv cv2.cvtColor()方法(将图像从一种颜色空间转换为另一种颜色空间)(转换成灰度图)

    def cvtColor(src, code, dst=None, dstCn=None): # real signature unknown; restored from __doc__" ...

  5. python opencv cv2.resize()函数

    **def resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None): # real signature unknown; ...

  6. python opencv cv2.VideoCapture(),read(),waitKey()的使用 ret,frame参数

    参考文章:python-OpenCV2中 cv2.VideoCapture(),read(),waitKey()的使用 VideoCapture.py # encoding: utf-8 # modu ...

  7. Python+Opencv实现无参数、全自动的Canny算法

    目录 一.什么是Canny边缘检测算法? 二.最优边缘准则是什么? 三.Canny算法实现步骤 四.Canny算法在使用中的问题? 五.无参数.自动化Canny算法代码实现 六.改进算法效果展示 七. ...

  8. python中cv2.putText参数详解

    cv2.putText(img, str(i), (123,456)), font, 2, (0,255,0), 3) 各参数依次是:图片,添加的文字,左上角坐标,字体,字体大小,颜色,字体粗细 其中 ...

  9. Python OpenCV(cv2) 摄像头开启+录像

    最近在研究opencv,也就是cv2库,想实现开启摄像头实时显示画面+录像,从网上看了不少案例和代码,动手实践时发现很多案例和代码都没有经过严格验证,导致bug不断,生成的视频文件也不能播放.经过持续 ...

最新文章

  1. 走进云计算与虚拟化的底层核心
  2. 不得不爱开源 Wijmo jQuery 插件集(4)-【手风琴效果】(附页面展示和源码)
  3. 【Python】青少年蓝桥杯_每日一题_11.27_电梯用电量
  4. JAX-RS开发 hello world
  5. python3.6 websocket异步高并发_在Python3.6上的websocket客户端中侦听传入消息时出现问题...
  6. 有关UITableView--cell复用问题
  7. 车机没有carlife可以自己下载吗_路咖评:新宝骏的车机系统 革了百度Carlife的命?...
  8. 【网络爬虫入门01】应用Requests和BeautifulSoup联手打造的第一条网络爬虫
  9. mysql开发平台_搭建mysql编程平台
  10. 用友NC63 医药行业 消耗汇总 出库单批次模糊查询
  11. STM32固件库的下载
  12. 分享一款代码生成工具,可自定义模板生成不同的代码
  13. 神经网络学习小记录64——Pytorch 图像处理中注意力机制的解析与代码详解
  14. Spark 内存管理之Tungsten
  15. 马云研判当前经济形势,超震撼
  16. pytorch多卡教程
  17. 蔡高厅高等数学13-极限的四则运算公式
  18. 共享租车平台“车便利租车”完成A轮融资
  19. 基于订单号可重入的交易系统接口设计探讨
  20. Feign传输MultipartFile 报错 Error converting request body

热门文章

  1. 4399个人中心登录_4399手机开放平台
  2. Ubuntu新手入门
  3. ARP原理概述——基于WinPcap发送ARP请求数据包获取远程MAC地址
  4. 【综合类型第 18 篇】什么是 MVC 模式?
  5. jquery对元素隐藏的操作
  6. 聚观早报 | 微念与李子柒达成和解;天才少年稚晖君从华为离职
  7. 什么是序列化? 如何实现(反)序列化 序列化的应用
  8. Java中的序列化到底是什么
  9. 五子棋程序设计实现技术文档
  10. 南邮 OJ 2083 法师