使用 OpenCV 和 Python 从图像中识别和裁剪文本时出现问题



我正在使用从这个答案中获取的代码:使用 Opencv 检测图像中的文本区域

我正在使用的代码是:

import cv2
def captch_ex(file_name ):
    img  = cv2.imread(file_name)
    img_final = cv2.imread(file_name)
    img2gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    ret, mask = cv2.threshold(img2gray, 180, 255, cv2.THRESH_BINARY)
    image_final = cv2.bitwise_and(img2gray , img2gray , mask =  mask)
    ret, new_img = cv2.threshold(image_final, 180 , 255, cv2.THRESH_BINARY)  # for black text , cv.THRESH_BINARY_INV
    '''
            line  8 to 12  : Remove noisy portion
    '''
    kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3 , 3)) # to manipulate the orientation of dilution , large x means horizonatally dilating  more, large y means vertically dilating more
    dilated = cv2.dilate(new_img,kernel,iterations = 9) # dilate , more the iteration more the dilation
    contours = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[0] # get contours
    index = 0
    for contour in contours:
        # get rectangle bounding contour
        [x,y,w,h] = cv2.boundingRect(contour)
        #Don't plot small false positives that aren't text
        if w < 35 and h<35:
            continue
        # draw rectangle around contour on original image
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,255),2)
        #you can crop image and send to OCR  , false detected will return no text :)
        cropped = img_final[y :y +  h , x : x + w]
        s = file_name + 'crop_' + str(index) + '.png'
        cv2.imwrite(s , cropped)
        index = index + 1
    # write original image with added contours to disk
file_name ='rec_5.png'
captch_ex(file_name)

要指出的最大区别如下: contours = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)[0]添加了[0],因为我在没有它的情况下不断收到此错误

Traceback (most recent call last):
  File "test2.py", line 38, in <module>
    captch_ex(file_name)
  File "test2.py", line 20, in captch_ex
    [x,y,w,h] = cv2.boundingRect(contour)
TypeError: points is not a numpy array, neither a scalar

不幸的是,我找不到来源,但我在某处读到,版本 3 的此方法发生了变化,现在是必需的。

我的问题是,当我向该函数提供图像时,我会收到数百个1 px.宽度的裁剪图像,这些图像并没有完成该函数在引用答案中显然解决的问题。

截至目前,我猜测上面提到的其他[0]可能是导致错误的原因,但没有它,我可以让脚本完成。

问题在于 cv2.findContours() 方法,实际上它对 Opencv 2 和 Opencv 3 有不同的返回参数,您必须检查您正在使用的 Opencv 版本的文档, 一般而言:

对于 Opencv 2:

contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

对于 Opencv 3:

image, contours, hierarchy = cv2.findContours(dilated,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

现在您不需要[0]黑客来访问轮廓,您可以进一步继续:

for contour in contours:
    # get rectangle bounding contour
    [x,y,w,h] = cv2.boundingRect(contour)

相关内容

  • 没有找到相关文章

最新更新