去除密集的网格线并转换为清晰的图像 - opencv - python

问题描述 投票:0回答:1

我想从这张图片中提取文本。我是opencv的新手。我已经针对各种问题尝试了各种 opencv 代码,但没有一个对我有用。

如何从中提取文本?或者也许删除网格线和中间的圆形凸起,以便图像变直,然后我可以提取文本。

如有任何帮助,我们将不胜感激。

我尝试过的示例代码之一 - this

python-3.x opencv image-processing text-recognition
1个回答
0
投票

我采取了以下方法:

  • 获取目标通道上不强的像素

  • 按位和这些像素来消除线条

  • 基于网格用邻居的平均值填充像素

    函数写在这里:

     # get weak pixels by comparing each channel
     def getWeak(im, targetChannel, Factor):
         r,g,b = cv2.split(im) # split image
         if targetChannel == 0:
             target = r
             mask = (target<Factor*b)*(target<Factor*g)
         elif targetChannel == 1:
             target = g
             mask = (target<Factor*r)*(target<Factor*b)
         elif targetChannel == 2:
             target = b
             mask = (target<Factor*r)*(target<Factor*g)
         else:
             raise Exception("Invalid channel number!\nInput 0 for red, 1 for green, 2 for blue")
         return np.array(mask, dtype = np.uint8)
     def fill_with_mean(image): # define the fill with mean function for the images
         def mean_filter(arr): # define mean filter
             return np.mean(arr) # return mean of an array
         mean_filled_image = generic_filter(image, mean_filter, size=4, mode='constant', cval=0.0) 
         return mean_filled_image # return the mean filled image
    

实现:(请注意,我通常将图像转换为 RGB,以便更轻松地使用 matplotlib 进行绘图):

im = cv2.cvtColor(cv2.imread("GridCaptacha.png"), cv2.COLOR_BGR2RGB) # read image
Numbers = cv2.bitwise_and(im,im, mask = getWeak(im, 2, 1.1)+getWeak(im, 0, 1.1)) # btwise and, using addition (OR) on the masks
NumbersGray = cv2.cvtColor(Numbers, cv2.COLOR_RGB2GRAY) # convert to gray
# add contrast to image
NumbersGray[NumbersGray<50] = 1 # make numbers positive class
NumbersGray[NumbersGray>=50] = 0 # and the lines negative class
# plotting...
fig, axs = plt.subplots(nrows= 3, ncols = 1, sharex = True, sharey = True)
axs[0].imshow(im)
axs[0].set_title("Raw")
axs[1].imshow(Numbers)
axs[1].set_title("After pixel selection")
axs[2].imshow(fill_with_mean(NumbersGray), cmap = "gray")
axs[2].set_title("After averaging")
for ax in axs:
    ax.axis("off")

结果如下:

图书馆:

import cv2
%matplotlib notebook
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import generic_filter

P.S:到目前为止,我已经回答了其中一些此类问题。我之前提到过,现在再次提到:验证码从来就不是用通常的超立方体来解码的。因此,虽然回答这些问题很有趣,但对于这里的很多人来说,其局限性是非常明显的。

© www.soinside.com 2019 - 2024. All rights reserved.