cv2.rectangle()在图像上画矩形框的用法

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")

 

  • 17
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值