Tesseract モデルをトレーニングするために、OpenCV を使用して Image の文字を分割したいと考えています。
私はバージョン3.1.0を使用しています (Macports のアップグレードのため - meh.. )。ドキュメント (Python 用) はまだあまり明確ではありません。
これが私がすることです:
- 輪郭を見つける準備をするために、イメージを 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 です。たぶんこれが失敗する理由です... (ただし、エラーはスローされません)
どんな助けでも大歓迎です!