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

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)

输出:

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`cv2.rectangle()`是OpenCV中用于绘制矩形的函数。它可以在图像上绘制一个矩形,可以指定矩形的左上角和右下角坐标,线条颜色和线条宽度等参数。下面是一个简单的例子: ```python import cv2 import numpy as np # 创建一个黑色的图像 img = np.zeros((512,512,3), np.uint8) # 绘制一个矩形 cv2.rectangle(img,(384,0),(510,128),(0,255,0),3) # 显示图像 cv2.imshow('rectangle',img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这个例子创建了一个黑色的图像,然后在图像上绘制了一个矩形,左上角坐标为(384,0),右下角坐标为(510,128),线条颜色为绿色,线条宽度为3。 `cv2.rectangle()`函数的完整语法如下: ```python cv2.rectangle(img, pt1, pt2, color[, thickness[, lineType[, shift]]]) ``` 其中,参数含义如下: - `img`:要绘制矩形的图像。 - `pt1`:矩形的左上角坐标。 - `pt2`:矩形的右下角坐标。 - `color`:矩形的颜色,可以是RGB元组或灰度值。 - `thickness`:矩形边框的线条宽度,如果为负数,则表示填充整个矩形。 - `lineType`:线条类型,可以是8、4或CV_AA等。 - `shift`:坐标点小数点位数。 如果要绘制一个半透明的矩形,可以使用`cv2.addWeighted()`函数。下面是一个例子: ```python import cv2 import numpy as np # 读取一张图片 img = cv2.imread('test.jpg') # 绘制一个半透明的矩形 overlay = img.copy() cv2.rectangle(overlay, (100, 100), (300, 300), (0, 0, 255), -1) alpha = 0.5 cv2.addWeighted(overlay, alpha, img, 1 - alpha, 0, img) # 显示图像 cv2.imshow('rectangle',img) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这个例子读取了一张图片,然后在图片上绘制了一个左上角坐标为(100,100),右下角坐标为(300,300),颜色为红色的矩形,并将矩形设置为半透明。最后使用`cv2.addWeighted()`函数将矩形叠加到原始图片上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值