opencv: cv2.rectangle 通过确定对角线 画矩形

疑问

在做项目的过程中,标记数据是记录每个 bounding box 的左上角和右下角坐标。因为用到了数据增强,所以我有了一个疑虑:

将标记数据翻转后输入 cv2.rectangle,此时输入格式已不再是 cv2.rectangle(image, 左上角坐标, 右下角坐标, color, 线条粗度),而是变成了诸如 cv2.rectangle(image, 左下角坐标, 右上角坐标, color, 线条粗度)之类的,那么矩形框还能正常画么?cv2.rectangle是通过 确定对角线 来画矩形的么?

Demo

no flip:
opencv: cv2.rectangle 通过确定对角线 画矩形_第1张图片

vertical flip:
opencv: cv2.rectangle 通过确定对角线 画矩形_第2张图片

horizontal flip:
opencv: cv2.rectangle 通过确定对角线 画矩形_第3张图片

h & v flip:
opencv: cv2.rectangle 通过确定对角线 画矩形_第4张图片

经验证:

  • cv2.rectangle确实是靠 确定对角线 来画矩形的。

Code

附上自己写的实验代码:

# encoding:utf-8

import cv2
image = cv2.imread("girl.jpg")
h, w = image.shape[:2]
h, w = map(int, [h/4, w/4])

# no flip
draw_0 = cv2.rectangle(image, (2*w, 2*h), (3*w, 3*h), (255, 0, 0), 2)
# vertical flip
draw_1 = cv2.rectangle(image, (2*w, 3*h), (3*w, 2*h), (255, 0, 0), 2)
# horizontal flip
draw_2 = cv2.rectangle(image, (3*w, 2*h), (2*w, 3*h), (255, 0, 0), 2)
# h & v flip
draw_3 = cv2.rectangle(image, (3*w, 3*h), (2*w, 2*h), (255, 0, 0), 2)

cv2.imwrite("origin.jpg", draw_0)
cv2.imwrite("vertical_flip.jpg", draw_1)
cv2.imwrite("horizontal_flip.jpg", draw_2)
cv2.imwrite("hv_flip.jpg", draw_3)

你可能感兴趣的:(OpenCV,OpenCV-Python)