删除验证码文本中不需要的行-opencv-python [英] Remove unwanted lines in captcha text - opencv - python

查看:93
本文介绍了删除验证码文本中不需要的行-opencv-python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I trying to get text from captcha image using opencv. Problem is text are masked with noise and it is complex to process with those horizontal line/noise.

原始图片

我处理过的图片:

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

not sure how to remove those horizontal lines and get text

代码:

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()

如何处理文本是否为浅色

Edit : how to handle if text are in light color

推荐答案

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)

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)

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

如果您将来有疑问,请告诉我.

Let me know if you have future queries.

这篇关于删除验证码文本中不需要的行-opencv-python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆