cv2. rectangle

rectangle(...)
    rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) -> img
    .   @brief Draws a simple, thick, or filled up-right rectangle.
    .   
    .   The function rectangle draws a rectangle outline or a filled rectangle whose two opposite corners
    .   are pt1 and pt2.
    .   
    .   @param img Image.
    .   @param pt1 Vertex of the rectangle.
    .   @param pt2 Vertex of the rectangle opposite to pt1 .
    .   @param color Rectangle color or brightness (grayscale image).
    .   @param thickness Thickness of lines that make up the rectangle. Negative values, like CV_FILLED ,
    .   mean that the function has to draw a filled rectangle.
    .   @param lineType Type of the line. See the line description.
    .   @param shift Number of fractional bits in the point coordinates.


### 如何使用 OpenCV 的 `cv2.rectangle` 方法绘制矩形 #### 函数签名 在 OpenCV 中,`cv2.rectangle()` 接口定义如下[^3]: ```python cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) -> img ``` - **img**: 输入图像。 - **pt1**: 矩形的一个顶点坐标 `(x,y)` 或者表示矩形位置和大小的元组 `(x, y, width, height)`[^2]。 - **pt2**: 当提供两个顶点时,这是另一个相对顶点;当采用第二种方式输入时,则不需要此参数。 - **color**: BGR颜色空间中的颜色值,例如 `(B,G,R)` 形式的三元组。 - **thickness**: 线条粗细,默认为 1。如果设置为负数(如 `-1`),则填充整个形状[^5]。 - **lineType**: 可选参数,指定线型。 #### 示例代码展示 下面给出一段 Python 代码来创建并显示带有不同样式矩形的图片: ```python import numpy as np import cv2 # 创建一个空白画布作为背景 canvas = np.zeros((400, 600, 3), dtype="uint8") # 定义一些常量以便于修改 start_point_1 = (50, 75) end_point_1 = (250, 325) rect_position_2 = (300, 75, 150, 250) # x, y, w, h fill_color = (0, 255, 0) # Green Color outline_color = (255, 0, 0) # Blue Color # 绘制第一个矩形 - 轮廓模式 cv2.rectangle(canvas, start_point_1, end_point_1, outline_color, 5) # 绘制第二个矩形 - 实心模式 cv2.rectangle(canvas, rect_position_2[:2], (rect_position_2[0]+rect_position_2[2], rect_position_2[1]+rect_position_2[3]), fill_color, -1) # 显示结果图象 cv2.imshow("Canvas", canvas) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这段程序先初始化了一个纯黑底板 (`np.zeros`) 来模拟绘画区域,接着分别调用了两次 `cv2.rectangle()` :一次用来描绘边框较宽的蓝色轮廓矩形,另一次则是绿色实心底色的大矩形。最后通过窗口展示了最终效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值