【问题标题】:Remove unwanted lines in captcha text - opencv - python删除验证码文本中不需要的行 - opencv - python
【发布时间】:2019-02-21 18:13:33
【问题描述】:

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

原图

我处理后的图像:

不知道如何删除这些水平线并获取文本

代码:

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

编辑:如果文本是浅色的,如何处理

【问题讨论】:

  • 你最好保留这些线并跟随它们获得变形场。拉直后,擦除会更容易,但更重要的是,字符会恢复。

标签: python-3.x opencv image-processing


【解决方案1】:
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()

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

【讨论】:

  • 是否可以将那些波浪形的单词变成直线,就像所有字母的底部对齐一样
  • 它实际上并不取决于图像如何?单词是白色的……还是单词有一些对比色然后是背景,如果它符合上述条件,那么只有我们才能做到这一点
  • 对齐同一行中的单词很困难,因为我们不知道图像中存在哪些字母。如果我们知道那么也很难在直线上对齐。虽然是很好的研究课题。
  • 您好,感谢您的信息,如果字母为浅色如何处理(请检查修改后的问题)
  • 如果您有大量数据,请查看所有数据集。找到字母的主要颜色,例如上面的白色和浅绿色。在找出主要颜色后,您可以尝试 2 件事,第一个尝试将它们转换为灰度并获得足够体面的阈值来区分字母和背景。第二是为特定的 3-4 主色创建自己的内核,即如果将此内核应用于图像,您将轻松区分字母和图像背景。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-01-17
  • 2017-01-27
  • 1970-01-01
  • 2014-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多