如何使用Python OpenCV在cv2.putText中制作黑色背景

21

我有一个使用 opencv 的项目,在帧上使用 cv2.putText() 显示一些文本。目前它看起来像下面这样:

enter image description here

正如您在左上角所看到的,文本存在,但不够清晰可见。是否有可能使背景为黑色,以便文本将显得更好看。像下面的图片一样:

enter image description here

即使黑色背景覆盖了帧的右侧,也没有关系。以下是我用于在帧上放置文本的代码:

cv2.putText(frame, "Data: N/A", (5, 30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)
cv2.putText(frame, "Room: C1", (5, 60), cv2.FONT_HERSHEY_COMPLEX_SMALL, 1, (0, 0, 255), 1)

在OpenCV中是否有可以完成这项任务的预构建方法/库?能否请有经验的人士提供好的建议?

4个回答

31

使用这个函数:

import cv2

def draw_text(img, text,
          font=cv2.FONT_HERSHEY_PLAIN,
          pos=(0, 0),
          font_scale=3,
          font_thickness=2,
          text_color=(0, 255, 0),
          text_color_bg=(0, 0, 0)
          ):

    x, y = pos
    text_size, _ = cv2.getTextSize(text, font, font_scale, font_thickness)
    text_w, text_h = text_size
    cv2.rectangle(img, pos, (x + text_w, y + text_h), text_color_bg, -1)
    cv2.putText(img, text, (x, y + text_h + font_scale - 1), font, font_scale, text_color, font_thickness)

    return text_size

然后你可以像这样调用该函数:

image = 127 * np.ones((100, 200, 3), dtype="uint8")
pos = (10, 10)
w, h = draw_text(image, "hello", pos=(10, 10))
draw_text(image, "world", font_scale=4, pos=(10, 20 + h), text_color_bg=(255, 0, 0))
cv2.imshow("image", image)
cv2.waitKey()

使用OpenCV在背景上绘制文本

请注意,默认情况下,它会绘制黑色背景,但如果需要可以使用不同的颜色。


2
这似乎是比所选方案更好的解决方案! - Mohsin
2
不错的解决方案,但是在这一行出现了类型错误:cv2.putText(img, text, (x, y + text_h + font_scale - 1), font, font_scale, text_color, font_thickness)。我必须将表达式“y + text_h + font_scale - 1”转换为整数。那么这一行应该改为:cv2.putText(img, text, (x, int(y + text_h + font_scale - 1)), font, font_scale, text_color, font_thickness)。 - gundrabur

11

没有预先构建的方法,但一个简单的方法是使用 cv2.rectangle + cv2.putText。您只需在图像上画出黑色矩形,然后放置文本即可。你可以根据自己需要调整 x,y,w,h 参数来控制矩形的大小。以下是一个示例:

输入图像:

enter image description here

结果:

enter image description here

import cv2
import numpy as np

# Load image, define rectangle bounds
image = cv2.imread('1.jpg')
x,y,w,h = 0,0,175,75

# Draw black background rectangle
cv2.rectangle(image, (x, x), (x + w, y + h), (0,0,0), -1)

# Add text
cv2.putText(image, "THICC flower", (x + int(w/10),y + int(h/2)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255), 2)

# Display
cv2.imshow('image', image)
cv2.waitKey()

6
这种方法的问题在于,除非你计算出文字需要的字体度量,否则你无法知道文本是否会超出黑色矩形边界或者文本是否太小而无法填满黑色矩形。 - fmw42

3

以下是使用Python的OpenCV进行此操作的一种方法。

  • 读取输入图像
  • 创建一个与输入图像相同大小的所需背景颜色的图像
  • 在背景图像上绘制文本
  • 获取文本区域的边界矩形
  • 将文本区域从背景颜色图像复制到输入图像的副本中
  • 保存结果

输入:

enter image description here

import cv2
import numpy as np

# load image
img = cv2.imread("zelda1.jpg")

# create same size image of background color
bg_color = (0,0,0)
bg = np.full((img.shape), bg_color, dtype=np.uint8)

# draw text on bg
text_color = (0,0,255)
cv2.putText(bg, "Data: N/A", (5,30), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.75, text_color, 1)

# get bounding box
# use channel corresponding to color so that text is white on black background
x,y,w,h = cv2.boundingRect(bg[:,:,2])
print(x,y,w,h)

# copy bounding box region from bg to img
result = img.copy()
result[y:y+h, x:x+w] = bg[y:y+h, x:x+w]

# write result to disk
cv2.imwrite("zelda1_background_text.jpg", bg)
cv2.imwrite("zelda1_text.jpg", result)

# display results
cv2.imshow("TEXT", bg)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
cv2.destroyAllWindows()


背景色图片上的文字:

在这里输入图片描述

输入图片上的文字:

在这里输入图片描述

附注:在进行裁剪时,您可以调整包围矩形(x,y,w,h)的值以添加一些填充。


3
import cv2 \
import numpy as np

#### Load image, define rectangle bounds
image = cv2.imread(r'C:\Users\Bharath\Downloads\test.jpg')

#### overlay space
x,y,w,h = 40,30,300,60


#### alpha, the 4th channel of the image
alpha = 0.3

overlay = image.copy()
output = image.copy()


##### corner
cv2.rectangle(overlay, (x, x), (x + w, y + h), (0,0,0), -1)

##### putText
cv2.putText(overlay, "HELLO WORLD..!", (x + int(w/10),y + int(h/1.5)), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255,255,255), 2)

#### apply the overlay
cv2.addWeighted(overlay, alpha, output, 1 - alpha,0, output)


##### Display
cv2.imshow("Output", output)\
cv2.waitKey(0)

输入在此输入图像描述

输出在此输入图像描述


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接