import cv2
import numpy as np
# Load image, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (7,7), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV cv2.THRESH_OTSU)[1]
# Create rectangular structuring element and dilate
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
dilate = cv2.dilate(thresh, kernel, iterations=4)
cv2.imshow('dilate', dilate)
cv2.waitKey()
我試圖掩蓋影像中的文本元素并回傳僅包含剩余部分的影像。我已經應用了閾值和擴張,但我怎樣才能保留背景。

在上圖中,所有文本區域及其邊界都被遮蓋了。
現在用原始影像的背景替換那些被遮蔽的區域。為此,首先我記下文本在mask_ind. 然后將這些區域中的像素值替換為原始影像的背景image[0,0]
mask_ind = (dilate == 255)
res[mask_ind] = image[0,0]
cv2.imshow(res)

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/461999.html
