python - 删除验证码文本中不需要的行 - opencv - python

标签 python opencv image-processing ocr captcha

我尝试使用 opencv 从验证码图像中获取文本。 问题是文本被噪声掩盖了,处理这些水平线/噪声很复杂。

原图

enter image description here

我处理过的图像:

enter image description here

不确定如何删除那些水平线并获取文本

代码:

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('captcha.jpg',0)

#display image in window
#cv2.imshow('image',img) #@param - windowname, image to be displayed

horizontal_inv = cv2.bitwise_not(img)
#perform bitwise_and to mask the lines with provided mask
masked_img = cv2.bitwise_and(img, img, mask=horizontal_inv)
#reverse the image back to normal
masked_img_inv = cv2.bitwise_not(masked_img)
cv2.imshow("masked img", masked_img_inv)
cv2.imwrite("result2.jpg", masked_img_inv)

cv2.waitKey(0) # time for window to show image in milliseconds - 0 is infinite wait
cv2.destroyAllWindows()

编辑:如果文本为浅色如何处理

enter image description here

enter image description here

最佳答案

import numpy as np
import cv2

# Load an color image in grayscale
img = cv2.imread('captcha.jpg',0)

#display image in window
#cv2.imshow('image',img) #@param - windowname, image to be displayed

horizontal_inv = cv2.bitwise_not(img)
#perform bitwise_and to mask the lines with provided mask
masked_img = cv2.bitwise_and(img, img, mask=horizontal_inv)
#reverse the image back to normal
masked_img_inv = cv2.bitwise_not(masked_img)

kernel = np.ones((5,5),np.uint8)
dilation = cv2.dilate(masked_img_inv,kernel,iterations = 3) # to remove blackline noise
cv2.imwrite("result1.jpg", dilation)

enter image description here

ret,thresh2 = cv2.threshold(dilation,254,255,cv2.THRESH_BINARY_INV) 
thresh2=cv2.bitwise_not(thresh2)
# cv2.imshow("masked img", masked_img_inv)
cv2.imwrite("result2.jpg", thresh2)

enter image description here

cv2.waitKey(0) # time for window to show image in milliseconds - 0 is infinite wait
cv2.destroyAllWindows()

如果您以后有任何疑问,请告诉我。

关于python - 删除验证码文本中不需要的行 - opencv - python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52374197/

相关文章:

python - 使用 Python OpenCV 检测图像中的对象位置

image-processing - 图像交叉检测

二值图像中的环形或空心伪影检测算法

c++ - 坐标图像/离散空间中的几何操作

python - 如何避免 matplotlib.pyplot imshow() 中的自动伪着色

python - Pandas :沿着多索引的第一层切片

xcode - imwrite 在 OpenCV 中抛出编译错误

math - 数组的加权质心

python - 使用 Python 的 Salesforce Bulk API 批量下载

python - 如何从用 numpy 的 tofile() 编写的原始二进制文件中读取 float