Пример #1
0
# -*- coding: utf-8 -*-

import sys
import numpy as np
import cv2
import ocr_mnist

# MNIST 학습 데이터 읽어 들이기
mnist = ocr_mnist.build_model()
mnist.load_weights('mnist.h5')

# 이미지 읽어 들이기
im = cv2.imread('./images/numbers100.PNG')
# 윤곽 추출하기
gray = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2)
cv2.imwrite("numbers100-th.PNG", thresh)
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
                            cv2.CHAIN_APPROX_NONE)[1]

# 추출한 좌표 정렬하기
rects = []
im_w = im.shape[1]
for i, cnt in enumerate(contours):
    x, y, w, h = cv2.boundingRect(cnt)
    if w < 10 or h < 10: continue  # 작은경우 생략
    if w > im_w / 5: continue  # 큰 경우 생략
    y2 = round(y / 10) * 10  # Y 좌표 맞추기
    index = y2 * im_w + x
    rects.append((index, x, y, w, h))
Пример #2
0
import sys
import numpy as np
import cv2
import ocr_mnist

# フォントから生成した学習データを読む --- (※1)
mnist = ocr_mnist.build_model()
mnist.load_weights('font_draw.hdf5')

# 画像の読み込み --- (※2)
im = cv2.imread('numbers100.png')

# 輪郭を抽出 --- (※3)
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) # グレイスケールに
blur = cv2.GaussianBlur(gray, (5, 5), 0) # ぼかす
thresh = cv2.adaptiveThreshold(blur, 255, 1, 1, 11, 2) # 二値化
cv2.imwrite("numbers100-th.png", thresh)
contours = cv2.findContours(thresh, 
    cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)[1]

# 抽出した座標を左上から右下へと並び替える --- (※4)
rects = []
im_w = im.shape[1]
for i, cnt in enumerate(contours):
    x, y, w, h = cv2.boundingRect(cnt)
    if w < 10 or h < 10: continue # 小さすぎるのは飛ばす
    if w > im_w / 5: continue # 大きすぎるのも飛ばす
    y2 = round(y / 10) * 10 # Y座標を揃える
    index = y2 * im_w  + x
    rects.append((index, x, y, w, h))
rects = sorted(rects, key=lambda x:x[0]) # 並び替え