在实际的图像处理项目中,画矩形(rectangle) 的使用频率甚至比画线还要高。
无论是目标检测中的边界框(bounding box)、ocr识别中的文字区域高亮,还是隐私保护中的马赛克遮挡,本质上都是在画矩形。
今天,我们将继续使用 opencv 和 pillow 这两个利器,深入讲解如何在 python 中优雅地画矩形。
一、 为什么矩形如此重要?
- 目标检测:yolo、faster r-cnn 等模型输出的结果通常是矩形框坐标
(x, y, w, h)。 - roi 提取:通过矩形框选感兴趣区域(region of interest),进行裁剪或单独处理。
- 数据脱敏:用矩形色块覆盖身份证号、车牌、人脸。
二、 核心参数解析
在开始写代码前,先记住画矩形的几个核心要素:
- 左上角坐标
(x1, y1) - 右下角坐标
(x2, y2)- 注意:有些库也支持传入
[x1, y1, x2, y2]列表。
- 注意:有些库也支持传入
- 颜色:rgb 或 bgr 元组。
- 线宽/填充:是只画边框,还是填充整个矩形?
三、 opencv 实现:cv2.rectangle()
opencv 是计算机视觉的首选,它的 rectangle 函数非常高效,支持 numpy 数组操作。
1. 基础用法:画一个红色边框
import cv2
import numpy as np
# 创建一张黑色背景图
img = np.zeros((500, 500, 3), dtype=np.uint8)
# 定义左上角和右下角坐标
pt1 = (50, 50) # (x, y)
pt2 = (400, 300) # (x, y)
# 定义颜色 (opencv 是 bgr,所以 (0, 0, 255) 是红色)
color = (0, 0, 255)
# 定义线宽 (如果是 -1,表示填充)
thickness = 3
# 画矩形
cv2.rectangle(img, pt1, pt2, color, thickness)
cv2.imshow("opencv rectangle", img)
cv2.waitkey(0)
cv2.destroyallwindows()
2. 进阶技巧
a. 画实心矩形(马赛克遮挡神器)
只需将 thickness 设置为 -1 即可填充矩形。这常用于遮挡敏感信息。
# 画一个绿色的实心矩形 cv2.rectangle(img, (50, 350), (450, 450), (0, 255, 0), -1)
b. 画圆角矩形(opencv 没有直接函数,需要自己实现)
opencv 原生不支持圆角矩形,但我们可以通过画线和圆组合,或者用循环画短线来模拟。这里教大家一个简单的“伪圆角”方法:
# 简单的圆角矩形函数
def draw_rounded_rectangle(img, pt1, pt2, color, radius, thickness):
x1, y1 = pt1
x2, y2 = pt2
# 画四条边
cv2.line(img, (x1 + radius, y1), (x2 - radius, y1), color, thickness)
cv2.line(img, (x1 + radius, y2), (x2 - radius, y2), color, thickness)
cv2.line(img, (x1, y1 + radius), (x1, y2 - radius), color, thickness)
cv2.line(img, (x2, y1 + radius), (x2, y2 - radius), color, thickness)
# 画四个角
cv2.circle(img, (x1 + radius, y1 + radius), radius, color, thickness)
cv2.circle(img, (x2 - radius, y1 + radius), radius, color, thickness)
cv2.circle(img, (x1 + radius, y2 - radius), radius, color, thickness)
cv2.circle(img, (x2 - radius, y2 - radius), radius, color, thickness)
# 使用
draw_rounded_rectangle(img, (50, 50), (250, 200), (255, 255, 0), 20, 2)
四、 pillow 实现:imagedraw.rectangle()
pillow 的 api 在处理坐标时更加灵活,支持直接传入列表,且颜色是直观的 rgb。
1. 基础用法
from pil import image, imagedraw
# 创建白色背景图
img = image.new('rgb', (500, 500), color='white')
draw = imagedraw.draw(img)
# 坐标可以是 [左上角x, 左上角y, 右下角x, 右下角y]
box = [50, 50, 400, 300]
# 画蓝色边框,宽度为 3
draw.rectangle(box, outline='blue', width=3)
# 画一个红色实心矩形
draw.rectangle([50, 350, 450, 450], fill='red')
img.show()
2. pillow 的独特优势:支持多边形裁剪
pillow 的 rectangle 其实可以接受任意多边形坐标(只要是矩形框选逻辑),或者配合 imagedraw.polygon 使用更自由。
五、 实战案例:三个高价值场景
场景 1:给检测到的物体打标签(带文字的矩形)
在目标检测中,我们通常在矩形上方贴一个带背景色的文字标签。
import cv2
img = cv2.imread("test.jpg") # 假设有一张原图
x, y, w, h = 100, 100, 200, 300 # 模拟检测框
# 1. 画矩形框
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
# 2. 画文字背景(实心小矩形)
label = "person"
(text_width, text_height), _ = cv2.gettextsize(label, cv2.font_hershey_simplex, 0.6, 2)
cv2.rectangle(img, (x, y - 25), (x + text_width, y), (0, 255, 0), -1) # 填充
# 3. 写文字
cv2.puttext(img, label, (x, y - 5), cv2.font_hershey_simplex, 0.6, (255, 255, 255), 2)
cv2.imshow("labeled", img)
cv2.waitkey(0)
场景 2:高级马赛克(网格状矩形)
简单的实心矩形太生硬,真正的马赛克是由很多小矩形组成的。
def apply_mosaic(img, x, y, w, h, block_size=10):
"""
对指定区域 (x, y, w, h) 进行马赛克处理
"""
roi = img[y:y+h, x:x+w]
# 缩小再放大,模拟马赛克
roi = cv2.resize(roi, (w // block_size, h // block_size), interpolation=cv2.inter_linear)
roi = cv2.resize(roi, (w, h), interpolation=cv2.inter_nearest)
img[y:y+h, x:x+w] = roi
return img
# 使用
img = cv2.imread("face.jpg")
img = apply_mosaic(img, 50, 50, 200, 200)
cv2.imshow("mosaic", img)
cv2.waitkey(0)
注:虽然这用了 resize,但原理上是把像素块变成了“大矩形”。
场景 3:半透明遮罩(highlight)
有时候我们需要高亮某个区域,但又不想完全遮住原图。
import cv2
import numpy as np
img = cv2.imread("test.jpg")
overlay = img.copy()
# 定义区域
x, y, w, h = 100, 100, 300, 300
# 在覆盖层上画一个半透明矩形
# 先画一个实心矩形
cv2.rectangle(overlay, (x, y), (x+w, y+h), (255, 0, 0), -1)
# 混合原图和覆盖层 (alpha=0.3 表示覆盖层透明度)
cv2.addweighted(overlay, 0.3, img, 0.7, 0, img)
cv2.imshow("transparent overlay", img)
cv2.waitkey(0)
六、 opencv vs pillow:画矩形该选谁?
| 特性 | opencv (cv2.rectangle) | pillow (imagedraw.rectangle) |
|---|---|---|
| 坐标系 | 左上角(0,0),向右下增长 | 左上角(0,0),向右下增长 |
| 参数 | (img, pt1, pt2, color, thickness) | (xy, outline, fill, width) |
| 填充 | thickness = -1 | fill = 'color' |
| 性能 | 极快 (底层c++,支持numpy) | 中等 (纯python实现部分) |
| 颜色 | bgr (易错点) | rgb (直观) |
| 推荐场景 | 视频处理、实时检测、算法开发 | 批量图片处理、web后端生成图 |
七、 总结
画矩形看似简单,但在实际应用中往往需要结合坐标计算、颜色空间转换和图层混合。
- 如果你在做 yolo/ssd 目标检测 或 opencv 项目,请熟练掌握
cv2.rectangle,特别是利用-1做遮挡,利用addweighted做半透明高亮。 - 如果你在做 pillow 脚本 处理,记得它的参数更人性化,支持直接传列表。
以上就是使用python在图片上画矩形全攻略的详细内容,更多关于python在图片上画矩形的资料请关注代码网其它相关文章!
发表评论