Path: blob/master/Model-3/ocr/helpers.py
426 views
# -*- coding: utf-8 -*-1"""2Helper functions for ocr project3"""4import matplotlib.pyplot as plt5import numpy as np6import cv278SMALL_HEIGHT = 800910def implt(img, cmp=None, t=''):11""" Show image using plt """12plt.imshow(img, cmap=cmp)13plt.title(t)14plt.show()151617def resize(img, height=SMALL_HEIGHT, allways=False):18""" Resize image to given height """19if (img.shape[0] > height or allways):20rat = 1.0*height / img.shape[0]21return cv2.resize(img, (int(rat*img.shape[1]), height), interpolation = cv2.INTER_CUBIC)22return img232425def ratio(img, height=SMALL_HEIGHT):26""" Getting scale ratio """27return img.shape[0] / height282930def extendImg(img, shape):31""" Extend 2D image (numpy array) in vertical and horizontal direction32Shape of result image will match 'shape'33Args:34img: image to be extended35shape: shape (touple) of result image36Returns:37Extended image38"""39x = np.zeros(shape, np.uint8)40x[:img.shape[0], :img.shape[1]] = img41return x424344