0

Tesseract モデルをトレーニングするために、OpenCV を使用して Image の文字を分割したいと考えています。

私はバージョン3.1.0を使用しています (Macports のアップグレードのため - meh.. )。ドキュメント (Python 用) はまだあまり明確ではありません。

これが私がすることです:

  1. 輪郭を見つける準備をするために、イメージを 2 値化します。
  2. 輪郭を見つけます(これは機能します-少なくともゼロ以外の結果が得られます)
  3. 各輪郭について:

    1. マスクを作成します(これはおそらく失敗します - ゼロを取得します)
    2. マスクを使用して元の画像から部分を抽出します (動作するはずですが、マスクが失敗するため、これはまだ動作しません)

OpenCV の新しいバージョンの構文も多少異なるため、これによりさらに扱いにくくなる場合があります。

これが私のコードです:

def characterSplit(img):
    """
    Splits the characters in an image using contours, ready to be labelled and saved for training with Tesseract
    """

    # Apply Thresholding to binarize Image
    img = cv2.GaussianBlur(img, (3,3), 0)
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 75, 10)
    img = cv2.bitwise_not(img)

    # Find Contours
    contours = cv2.findContours(img, cv2.RETR_EXTERNAL , cv2.CHAIN_APPROX_TC89_KCOS, offset=(0,0))[1]

    # Iterate through the contours
    for c in xrange(len(contours)):
        mask = numpy.zeros(img.size)

        cv2.drawContours(mask, contours, c, (0, 255, 0), cv2.FILLED)    # Mask is zeros - It might fail here!

        # Where the result will be stored
        res = numpy.zeros(mask.size)

        # Make a Boolean-type numpy array for the Mask
        amsk = mask != 0

        # I use this to copy a part of the image using the generated mask.
        # The result is zeros because the mask is also zeros
        numpy.copyto(res, img.flatten(), where = amsk)

        ## (... Reshape, crop and save the result ...)

私の知る限り、マスクは元の画像と同じサイズにする必要があります。しかし、それも同じ形にする必要がありますか?たとえば、私のイメージは 640x74 ですが、マスク マトリックスを作成する方法では、マスクは 1x47360 です。たぶんこれが失敗する理由です... (ただし、エラーはスローされません)

どんな助けでも大歓迎です!

4

1 に答える 1

1

私はミキがコメントで提案したことをやった。cv::connectedComponentsキャラ分けをしていました。興味のある人のために、対応するコードを次に示します。

def characterSplit(img, outputFolder=''):
    # Splits the Image (OpenCV Object) into distinct characters and exports it in images withing the specified folder.

    # Blurring the image with Gaussian before thresholding is important
    img = cv2.GaussianBlur(img, (3,3), 0)
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 75, 10)
    img = cv2.bitwise_not(img)

    output = cv2.connectedComponentsWithStats(img, 8, cv2.CV_16S)
    n_labels =  output[0]
    labels = output[1]
    stats = output[2]

    for c in xrange(n_labels):
        # Mask is a boolean-type numpy array with True in the corresponding region
        mask = labels == c

        res = numpy.zeros(img.shape)
        numpy.copyto(res, img, where=mask)

        # The rectangle that bounds the region is stored in:
        # stats[c][0:4] -> [x, y, w, h]

        cv2.imwrite("region_{}.jpg".format(c), res)

お役に立てれば!

于 2016-12-01T18:28:14.840 に答える