问题描述
我有这些图片
我想删除背景中的文本.只有 captcha 字符
应该保留(即 K6PwKA、YabVzu).任务是稍后使用 tesseract 识别这些字符.
这是我尝试过的方法,但准确性并不高.
我该如何改进?
注意:我尝试了所有关于这个问题的建议,但没有一个对我有用.
根据 Elias 的说法,我尝试使用 Photoshop 将验证码文本的颜色转换为介于 [100, 105] 之间的灰度.然后我根据这个范围对图像进行阈值处理.但是我得到的结果并没有从 tesseract 中得到令人满意的结果.
输出:
结果:
编辑 2:
给:
文本为
有什么办法可以缓和一点
这里有两种可能的方法和一种纠正扭曲文本的方法:
方法一:形态学运算+轮廓滤波
获取二进制图像.
轮廓区域过滤
->
反转->
应用模糊得到结果OCR 的结果
代码
方法二:颜色分割
观察到要提取的所需文本与图像中的噪声具有可区分的对比度,我们可以使用颜色阈值来隔离文本.这个想法是转换为 HSV 格式然后颜色阈值以获得使用较低/较高颜色范围的掩码.从我们是否使用相同的过程到 Pytesseract 进行 OCR.
输入图像
->
掩码->
结果代码
纠正扭曲的文字
OCR 在图像水平时效果最佳.为了确保文本是 OCR 的理想格式,我们可以执行透视变换.在去除所有噪声以隔离文本之后,我们可以执行变形关闭以将单个文本轮廓组合成单个轮廓.从这里我们可以使用
与其他图像一起输出
更新代码以包含透视变换
注意:颜色阈值范围是使用此 HSV 阈值脚本确定的
I have these images
For which I want to remove the text in the background. Only the
captcha characters
should remain(i.e K6PwKA, YabVzu). The task is to identify these characters later using tesseract.This is what I have tried, but it isn't giving much good accuracy.
How can I improve the same?
Note : I tried all the suggestion that I was getting for this question and none of them worked for me.
EDIT : According to Elias, I tried finding the color of the captcha text using photoshop by converting it to grayscale which came out to be somewhere in between [100, 105]. I then threshold the image based on this range. But the result which I got did not give satisfactory result from tesseract.
Output :
Result :
EDIT 2 :
gives :
and the text as
Is there any way to soften it a little
解决方案Here are two potential approaches and a method to correct distorted text:
Method #1: Morphological operations + contour filtering
Obtain binary image. Load image, grayscale, then Otsu's threshold.
Remove text contours. Create a rectangular kernel with
cv2.getStructuringElement()
and then perform morphological operations to remove noise.Filter and remove small noise. Find contours and filter using contour area to remove small particles. We effectively remove the noise by filling in the contour with
cv2.drawContours()
Perform OCR. We invert the image then apply a slight Gaussian blur. We then OCR using Pytesseract with the
--psm 6
configuration option to treat the image as a single block of text. Look at Tesseract improve quality for other methods to improve detection and Pytesseract configuration options for additional settings.
Input image
->
Binary->
Morph openingContour area filtering
->
Invert->
Apply blur to get resultResult from OCR
Code
Method #2: Color segmentation
With the observation that the desired text to extract has a distinguishable contrast from the noise in the image, we can use color thresholding to isolate the text. The idea is to convert to HSV format then color threshold to obtain a mask using a lower/upper color range. From were we use the same process to OCR with Pytesseract.
Input image
->
Mask->
ResultCode
Correcting distorted text
OCR works best when the image is horizontal. To ensure that the text is in an ideal format for OCR, we can perform a perspective transform. After removing all the noise to isolate the text, we can perform a morph close to combine individual text contours into a single contour. From here we can find the rotated bounding box using
cv2.minAreaRect
and then perform a four point perspective transform usingimutils.perspective.four_point_transform
. Continuing from the cleaned mask, here's the results:Mask
->
Morph close->
Detected rotated bounding box->
ResultOutput with the other image
Updated code to include perspective transform
Note: The color threshold range was determined using this HSV threshold script
这篇关于使用 OpenCV 进行图像处理,从图像中去除背景文本和噪点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!