【问题标题】:Identifying umlauted characters during OCR using opencv使用 opencv 在 OCR 期间识别变音字符
【发布时间】:2018-06-06 03:38:51
【问题描述】:

我正在处理多行图像,如下所示。

我正在使用下面的 opencv 代码按顺序提取每一行和每个瑞典语单词。但是,瑞典变音符号没有被正确识别。像ä 分别标识为.、. 和a。我该如何保存?这也适用于i,它被标识为.,|(我无法正确输入,但它上面没有'点'是i')

下面是我正在使用的python代码,

import cv2
import numpy as np
image = cv2.imread("D:\\Users\\Downloads\\CaptureMultiLines.jpg")
cv2.imshow('orig',image)
# image = cv2.resize(image_original,None,fx=4, fy=4, interpolation = cv2.INTER_CUBIC)

#grayscale
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
# original_resized = cv2.resize(gray, (0,0), fx=.2, fy=.2)
cv2.imshow('gray',gray)
cv2.waitKey(0)

#Remove Salt and pepper noise
saltpep = cv2.fastNlMeansDenoising(gray,None,9,13)
# original_resized = cv2.resize(saltpep, (0,0), fx=.2, fy=.2)
cv2.imshow('Grayscale',saltpep)
cv2.waitKey(0)

#blur
blured = cv2.blur(saltpep,(3,3))
# original_resized = cv2.resize(blured, (0,0), fx=.2, fy=.2)
cv2.imshow('blured',blured)
cv2.waitKey(0)

#binary
ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
# original_resized = cv2.resize(thresh, (0,0), fx=.2, fy=.2)
cv2.imshow('Threshold',thresh)
cv2.waitKey(0)

#dilation
kernel = np.ones((5,100), np.uint8)
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
# original_resized = cv2.resize(img_dilation, (0,0), fx=.2, fy=.2)
cv2.imshow('dilated',img_dilation)
cv2.waitKey(0)

#find contours
im2,ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

#sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[1])

for i, ctr in enumerate(sorted_ctrs):

    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = image[y:y+h, x:x+w]

# #   show ROI
    cv2.imshow('segment no:' +str(i),roi)
    cv2.waitKey(0)


    im = cv2.resize(roi,None,fx=4, fy=4, interpolation = cv2.INTER_CUBIC)
    ret_1,thresh_1 = cv2.threshold(im,127,255,cv2.THRESH_BINARY_INV)
    # original_resized = cv2.resize(thresh, (0,0), fx=.2, fy=.2)
    cv2.imshow('Threshold_1',thresh_1)
    cv2.waitKey(0)
    thresh_1=cv2.cvtColor(thresh_1, cv2.COLOR_BGR2GRAY);

    #find contours
    im,ctrs_1, hier = cv2.findContours(thresh_1, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    #sort contours
    sorted_ctrs_1 = sorted(ctrs_1, key=lambda ctr: cv2.boundingRect(ctr)[0])

    for j, ctr_1 in enumerate(sorted_ctrs_1):

        # Get bounding box
        x_1, y_1, w_1, h_1 = cv2.boundingRect(ctr_1)

        # Getting ROI
        roi_1 = thresh_1[y_1:y_1+h_1, x_1:x_1+w_1]

        # #   show ROI
        cv2.imshow('Line no: ' + str(i) + "Column no : " +str(j),roi_1)
        cv2.waitKey(0)


# original_resized = cv2.resize(image, (0,0), fx=.2, fy=.2)
# cv2.imshow('marked areas',original_resized)
cv2.imshow('marked areas',image)
cv2.waitKey(0)

【问题讨论】:

    标签: python opencv ocr


    【解决方案1】:

    这会给你想要的。

    这是我添加的代码段:

    # dilation
    kernel = np.ones((10, 1), np.uint8)
    joined = cv2.dilate(thresh_1, kernel, iterations=1)
    cv2.imshow('joined', joined)
    cv2.waitKey(0)
    

    这是完整的代码:

    import cv2
    import numpy as np
    image = cv2.imread("D:\\Users\\Downloads\\CaptureMultiLines.jpg")
    cv2.imshow('orig',image)
    # image = cv2.resize(image_original,None,fx=4, fy=4, interpolation = cv2.INTER_CUBIC)
    
    #grayscale
    gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    # original_resized = cv2.resize(gray, (0,0), fx=.2, fy=.2)
    cv2.imshow('gray',gray)
    cv2.waitKey(0)
    
    #Remove Salt and pepper noise
    saltpep = cv2.fastNlMeansDenoising(gray,None,9,13)
    # original_resized = cv2.resize(saltpep, (0,0), fx=.2, fy=.2)
    cv2.imshow('Grayscale',saltpep)
    cv2.waitKey(0)
    
    #blur
    blured = cv2.blur(saltpep,(3,3))
    # original_resized = cv2.resize(blured, (0,0), fx=.2, fy=.2)
    cv2.imshow('blured',blured)
    cv2.waitKey(0)
    
    #binary
    ret,thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
    # original_resized = cv2.resize(thresh, (0,0), fx=.2, fy=.2)
    cv2.imshow('Threshold',thresh)
    cv2.waitKey(0)
    
    #dilation
    kernel = np.ones((5,100), np.uint8)
    img_dilation = cv2.dilate(thresh, kernel, iterations=1)
    # original_resized = cv2.resize(img_dilation, (0,0), fx=.2, fy=.2)
    cv2.imshow('dilated',img_dilation)
    cv2.waitKey(0)
    
    #find contours
    im2,ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    #sort contours
    sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[1])
    
    for i, ctr in enumerate(sorted_ctrs):
    
        # Get bounding box
        x, y, w, h = cv2.boundingRect(ctr)
    
        # Getting ROI
        roi = image[y:y+h, x:x+w]
    
    # #   show ROI
        cv2.imshow('segment no:' +str(i),roi)
        cv2.waitKey(0)
    
    
        im = cv2.resize(roi,None,fx=4, fy=4, interpolation = cv2.INTER_CUBIC)
        ret_1,thresh_1 = cv2.threshold(im,127,255,cv2.THRESH_BINARY_INV)
        # original_resized = cv2.resize(thresh, (0,0), fx=.2, fy=.2)
        cv2.imshow('Threshold_1',thresh_1)
        cv2.waitKey(0)
    
        # dilation
        kernel = np.ones((10, 1), np.uint8)
        joined = cv2.dilate(thresh_1, kernel, iterations=1)
        cv2.imshow('joined', joined)
        cv2.waitKey(0)
    
        joined=cv2.cvtColor(joined, cv2.COLOR_BGR2GRAY);
    
        #find contours
        im,ctrs_1, hier = cv2.findContours(joined, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
        #sort contours
        sorted_ctrs_1 = sorted(ctrs_1, key=lambda ctr: cv2.boundingRect(ctr)[0])
    
        for j, ctr_1 in enumerate(sorted_ctrs_1):
    
            # Get bounding box
            x_1, y_1, w_1, h_1 = cv2.boundingRect(ctr_1)
    
            # Getting ROI
            roi_1 = thresh_1[y_1:y_1+h_1, x_1:x_1+w_1]
    
            # #   show ROI
            cv2.imshow('Line no: ' + str(i) + "Column no : " +str(j),roi_1)
            cv2.waitKey(0)
    
    
    # original_resized = cv2.resize(image, (0,0), fx=.2, fy=.2)
    # cv2.imshow('marked areas',original_resized)
    cv2.imshow('marked areas',image)
    cv2.waitKey(0)
    

    【讨论】:

    • 完美! @Ishara Madhawa。你能添加代码来检测单词之间的空格吗?这将有助于我重建整个文档。
    猜你喜欢
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 2014-04-25
    • 2012-03-13
    • 2013-08-03
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    相关资源
    最近更新 更多