Python OpenCV cv2绘制带有文本的矩形 [英] Python OpenCV cv2 drawing rectangle with text

查看:93
本文介绍了Python OpenCV cv2绘制带有文本的矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用

cv2.rectangle(frame,(x,y),(x1,y1),(0,255,0),2)

我想绘制带有文本信息的矩形.我该怎么做?是否有任何可用的现成实现?还是我应该匹配矩形的左上角坐标,并尝试显示与cv2 rect元素不同的cv2文本元素?

I would like to draw rectangles with text information on them. How do I do it? Are there any ready to use implementations available? Or should I match the top left coordinate of the rectangle and try to display a different cv2 text element with the cv2 rect element?

您能指导我执行任何代码实现/解决方法吗?

Can you direct me to any code implementation/workaround?

P.S:我不想使用object_detection. TF附带的可视化工具.

P.S: I don't want to use the object_detection. visualisation utils available with tf.

我为像我这样的懒惰人创建了他们所需的复制代码.享受.惰性编码!

I created this post for lazy people like me to come copy code they need. Enjoy. Lazy coding!

推荐答案

您可以使用 cv2.putText() 将文本信息覆盖在矩形顶部.例如,您可以抓取轮廓坐标,绘制一个矩形,然后通过向上移动将其放在文本上方.

You can use cv2.putText() to overlay text information on top of a rectangle. For example, you can grab the contour coordinates, draw a rectangle, and put text on top of it by shifting it upwards.

x,y,w,h = cv2.boundingRect(contour)
image = cv2.rectangle(image, (x, y), (x + w, y + h), (36,255,12), 1)
cv2.putText(image, 'Fedex', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2)

您会得到类似的东西

这篇关于Python OpenCV cv2绘制带有文本的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆