如何替换opencv中的裁剪矩形?

alv*_*vas -1 python opencv numpy image-processing

我已经成功地用文本裁剪了一个边界框,例如给定这个图像:

在此输入图像描述

我能够准确地给出以下框:

在此输入图像描述

用这个代码:

import re
import shutil

from IPython.display import Image

import requests
import pytesseract, cv2

"""https://www.geeksforgeeks.org/text-detection-and-extraction-using-opencv-and-ocr/"""
# Preprocessing the image starts
# Convert the image to gray scale
img = cv2.imread('img.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Performing OTSU threshold
ret, thresh1 = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU | cv2.THRESH_BINARY_INV)

# Specify structure shape and kernel size.
# Kernel size increases or decreases the area
# of the rectangle to be detected.
# A smaller value like (10, 10) will detect
# each word instead of a sentence.
rect_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (18, 18))

# Applying dilation on the threshold image
dilation = cv2.dilate(thresh1, rect_kernel, iterations = 1)

# Finding contours
contours, hierarchy = cv2.findContours(dilation, cv2.RETR_EXTERNAL,
                                                 cv2.CHAIN_APPROX_NONE)

# Creating a copy of image
im2 = img.copy()


for cnt in contours:
    x, y, w, h = cv2.boundingRect(cnt)
    # Drawing a rectangle on copied image
    rect = cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2)
    # Cropping the text block for giving input to OCR
    cropped = im2[y:y + h, x:x + w]
    
cv2.imwrite('image-notxt.png', cropped)
Image(filename='image-notxt.png',  width=200)
Run Code Online (Sandbox Code Playgroud)

第 1 部分:如何替换裁剪后的文本框并放回清晰的文本框?例如得到类似的东西:

在此输入图像描述

我试过了:

    for cnt in contours:
        x, y, w, h = cv2.boundingRect(cnt)
        # Drawing a rectangle on copied image
        rect = cv2.rectangle(im2, (x, y), (x + w, y + h), (0, 255, 0), 2)
        # Cropping the text block for giving input to OCR
        cropped = im2[y:y + h, x:x + w]
        text = pytesseract.image_to_string(cropped).strip('\x0c').strip()
        text = re.sub(' +', ' ', text.replace('\n', ' ')).strip()
        if text:
            # White out the cropped box.
            cropped.fill(255)
            # Create the image with the translation.
            cv2.putText(img=cropped, text="foobar", org=(12, 15), fontFace=cv2.FONT_HERSHEY_TRIPLEX, fontScale=0.3, color=(0, 0, 0),thickness=1)
            cv2.imwrite('image-notxt.png', cropped)
            Image(filename='image-notxt.png',  width=200)

Run Code Online (Sandbox Code Playgroud)

这成功地将裁剪后的框变白并插入如下文本:

在此输入图像描述

第 2 部分:如何创建与裁剪框大小相同的 opencv 文本框矩形?例如给定一个字符串foobar,如何获得最终图像,如下所示:

在此输入图像描述

fmw*_*w42 9

在Python/OpenCV/Numpy中,使用Numpy将颜色写入该区域,格式如下:

img[y:y+h, x:x+w] = color tuple 
Run Code Online (Sandbox Code Playgroud)

例如:

在此输入图像描述

# Part 1 --- Blank Area

# read image
photo = cv2.imread('Wurst.png')

# define region arguments
x,y,w,h = 40,40,150,45

# blank area
result1 = photo.copy()
result1[40:40+45, 40:40+150] = (255,255,255)

# save results
cv2.imwrite("Wurst_blank_result.png", result1)

# show the results
cv2.imshow("blanked result", result1)
cv2.waitKey(0)

# Part 2 --- Write text

# define text arguments
text = "FOOBAR"
thickness = 2
scale = .8
pad = 2

# determine size for text image
(wd, ht), baseLine = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, scale, thickness)
print (wd, ht, baseLine)

# add black text to white background image padded all around
th = ht + 2 * pad
tw = wd + 2 * pad
text_img = np.full((th,tw,3), (255,255,255), dtype=np.uint8)
text_img = cv2.putText(text_img, text, (pad,ht+pad), cv2.FONT_HERSHEY_SIMPLEX, scale, (0,0,0), thickness)
print(text_img.shape)

# insert text image into region of photo
if w>=tw and h>=th:
    # compute offset
    offx = int((w-tw)/2)
    offy = int((h-th)/2)
    # combine the text with the image
    result2 = result1.copy()
    result2[y+offy:y+offy+th, x+offx:x+offx+tw] = text_img
    # save results
    cv2.imwrite("Wurst_text_result.png", result2)
    # show the results
    cv2.imshow("text image", text_img)
    cv2.imshow("text result", result2)
    cv2.waitKey(0)
else:
    print("Text Is Too Large")
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

请参阅https://docs.opencv.org/4.1.1/d6/d6e/group__imgproc__draw.html#ga3d2abfcb995fd2db908c8288199dba82上的 cv2.getTextSize()和https://docs.opencv.org/4.1.1上的 cv2.putText() /d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576

  • 他有两个问题。我回答第一个。我现在在答案中添加了一条注释,以使用 cv2.putText() 在框中添加文本 (2认同)