python opencv 画框
时间: 2023-09-25 16:17:29 浏览: 121
你可以使用Python OpenCV中的cv2.rectangle()函数来实现画框效果。这个函数有几个参数,包括img(图像),pt1(矩形左上角的坐标),pt2(矩形右下角的坐标),color(矩形的颜色),thickness(线条的粗细),lineType(线条的类型)等。具体的步骤如下所示:
1. 首先,导入必要的库:
import numpy as np
import cv2 as cv
2. 生成一个空灰度图像:
img = np.zeros((320, 320, 3), np.uint8)
3. 定义要绘制的矩形的左上角和右下角的坐标:
ptLeftTop = (60, 60)
ptRightBottom = (260, 260)
4. 定义矩形的颜色(这里以绿色为例):
point_color = (0, 255, 0) # BGR
5. 定义线条的粗细和类型:
thickness = 1
lineType = 4
6. 使用cv2.rectangle()函数绘制矩形:
cv.rectangle(img, ptLeftTop, ptRightBottom, point_color, thickness, lineType)
7. 如果需要绘制多个矩形,可以重复步骤3到步骤6。
8. 创建一个窗口并显示图像:
cv.namedWindow("AlanWang")
cv.imshow('AlanWang', img)
9. 设置等待时间(这里设定为10秒):
cv.waitKey(10000)
10. 关闭窗口:
cv.destroyAllWindows()
这样,你就可以使用Python OpenCV画框了。希望对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [Python OpenCV实现鼠标画框效果](https://download.csdn.net/download/weixin_38608189/12860604)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [opencv-python绘制矩形框](https://blog.csdn.net/Vertira/article/details/123788719)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
阅读全文