簡體   English   中英

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

[英]How to make black background in cv2.putText with Python OpenCV

我有一個opencv項目,我在框架上使用cv2.putText()顯示一些文本。 目前它看起來如下:

在此處輸入圖片說明

正如您在左上角看到的那樣,文本存在但不清晰可見。 是否可以將背景設為黑色,以便文本看起來不錯。 類似於下圖:

在此處輸入圖片說明

即使黑色背景覆蓋到框架的右側,也可以。 下面是我用來在框架上放置文本的代碼:

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 中是否有任何可以執行此操作的預構建方法/庫。 任何人都可以請提出一個好方法嗎?

沒有預先構建的方法,但一個簡單的方法是使用cv2.rectangle + cv2.putText 您需要做的就是在圖像上繪制黑色矩形,然后放置文本。 您可以根據您想要矩形的大小來調整x,y,w,h參數。 下面是一個例子:

輸入圖像:

在此處輸入圖片說明

結果:

在此處輸入圖片說明

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

使用這個功能:

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 中繪制帶有背景的文本

請注意,默認情況下它會繪制黑色背景,但您可以根據需要使用其他顏色。

這是在 Python OpenCV 中執行此操作的一種方法。

  • 讀取輸入
  • 創建與輸入大小相同的所需背景顏色的圖像
  • 在背景圖像上繪制文本
  • 獲取文本區域的邊界矩形
  • 將文本區域從背景顏色圖像復制到輸入圖像的副本
  • 保存結果

輸入:

在此處輸入圖片說明

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


背景顏色圖像上的文字:

在此處輸入圖片說明

輸入圖像上的文本:

在此處輸入圖片說明

PS 您可以在裁剪時調整邊界矩形 (x,y,w,h) 值以添加一些填充。

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)

`

輸入在此處輸入圖片說明

輸出在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM