cv2.rectangle, 确实是靠 确定对角线 来画矩形的。
cv2.rectangle(img, (bbox.left, bbox.top), (bbox.right, bbox.bottom), (0,0,255), 2)
# encoding:utf-8

import cv2
import numpy as np
image = cv2.imread("girl.jpg")
GrayImage=cv2.cvtColor(image ,cv2.COLOR_BGR2GRAY)
# h, w = image.shape[:2]
# h, w = map(int, [h/4, w/4])
# print(h,w)
# # no flip
# draw_0 = cv2.rectangle(image, (100, 100), (10, 10), (0, 0, 255))#cv2.rectangle(image, pt1,pt2, color)
x, y, w, h =cv2.boundingRect(GrayImage)
draw_1=cv2.rectangle(image, (x,y), (x+w,y+h), (0,255,0), 2)
#参数:pt1,对角坐标1, pt2:对角坐标2
# 注意这里根据两个点pt1,pt2,确定了对角线的位置,进而确定了矩形的位置
#The function cv::rectangle draws a rectangle outline or a filled rectangle whose two opposite corners are pt1 and pt2.
draw_0 = cv2.rectangle(image, (2*w, 2*h), (3*w, 3*h))

cv2.imwrite("vertical_flip.jpg", draw_1)#将画过矩形框的图片保存到当前文件夹

cv2.imshow("draw_0", draw_1)#显示画过矩形框的图片
cv2.waitKey(0)
cv2.destroyWindow("draw_0")

 

Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐