gpt4 book ai didi

python - 如何在 Python 中将前景文本与嘈杂的背景分开?

转载 作者:太空宇宙 更新时间:2023-11-04 01:47:42 25 4
gpt4 key购买 nike

我正在尝试从此图像中提取文本

this image .

我尝试使用 OpenCV 调整对比度和亮度、平滑(例如 GaussianBlur、medianblur)和阈值技术(例如 Otsu),仍然有很多残留噪声。

还有什么我可以尝试的吗?

最佳答案

您可以尝试结合使用高斯模糊、阈值和形态学操作来隔离文本。这是一个管道

Blur -> Threshold -> Opening -> Dilation -> Bitwise-and

enter image description here enter image description here enter image description here enter image description here enter image description here

import cv2

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]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,2))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=3)

# Repair text
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7,7))
dilate = cv2.dilate(opening, kernel, iterations=2)

# Bitwise-and with input image
result = cv2.bitwise_and(image,image,mask=dilate)
result[dilate==0] = (255,255,255)

cv2.imshow('thresh', thresh)
cv2.imshow('opening', opening)
cv2.imshow('dilate', dilate)
cv2.imshow('result', result)
cv2.waitKey()

关于python - 如何在 Python 中将前景文本与嘈杂的背景分开?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58739151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com