def text_extractor(self, image_obj): temp_image_object = ImageObject() self.__img_name = image_obj.get_image_name() self.__height = image_obj.get_image_height() self.__width = image_obj.get_image_width() self.__img_matrix = image_obj.get_image_matrix() left_limit = self.__width right_limit = 0 top_limit = self.__height bottom_limit = 0 for i in range(self.__width - 1): for j in range(self.__height - 1): variable = self.__img_matrix[j, i] if self.__img_matrix[j, i] < 100: if top_limit > j: top_limit = j if bottom_limit < j: bottom_limit = j a = self.__img_matrix[top_limit - 1:bottom_limit + 1, :] temp_image_object.set_image_matrix(a) temp_image_object.set_image_name('crop{}'.format(self.__img_name)) # self.i_writer.write_image(temp_object) return temp_image_object
def split_image(self, image_object): """Splitting the image into letter wise images""" # i_writer = ImageWriter() # Extract all the parameters from the image object self.__image_matrix = image_object.get_image_matrix() self.__image_name = image_object.get_image_name() self.__image_height = image_object.get_image_height() self.__image_width = image_object.get_image_width() has_black = False pre_col_black = False left_limit = 0 right_limit = 0 no = 0 for i in range(self.__image_width): if has_black: # check previous column whether it has black pre_col_black = True has_black = False for j in range( self.__image_height): # Iterate row by row in each column if self.__image_matrix[j, i] < 100: has_black = True if not pre_col_black: left_limit = i if pre_col_black and not has_black: # true when columns are black to white no += 1 right_limit = i image_width = right_limit - left_limit char_img = self.__image_matrix[:, left_limit - 1:right_limit + 1] image_name = "{}char.bmp".format(no) image_object = ImageObject() image_object.set_image_matrix(char_img) image_object.set_image_name(image_name) image_object.set_image_width(image_width) image_object.set_image_height(self.__image_height) self.__image_list.append(image_object) # i_writer.write_image(image_object) pre_col_black = False
class Enhancement: """Pre-process of the Image""" def __init__(self): self.__grey_image = ImageObject() self.__invert_grey_image = ImageObject() self.__threshold_image = ImageObject() self.__i_writer = ImageWriter() # converting to gray is not needed since image is read in gray scale def im_to_gray(self, image): # Create a grey image object self.__grey_image.set_image_name("g{}".format(image.get_image_name())) self.__i_writer.write_image(self.__grey_image) def invert(self, image=ImageObject()): """Convert image into negative @:parameter imageObject @:return negative image object""" self.__invert_grey_image.set_image_matrix(255 - image.get_image_matrix()) self.__invert_grey_image.set_image_name("i{}".format( image.get_image_name())) self.__invert_grey_image.set_image_height(image.get_image_height()) self.__invert_grey_image.set_image_width(image.get_image_width()) self.__i_writer.write_image(self.__invert_grey_image) return self.__invert_grey_image def binarization(self, image=ImageObject()): """binarize the image using a threshold value @:parameter image object @:return binarized image""" image_val = image.get_image_matrix() retval, img = cv2.threshold(image_val, 127, 255, cv2.THRESH_BINARY) self.__threshold_image.set_image_matrix(img) self.__threshold_image.set_image_name("binarized {}".format( image.get_image_name())) self.__threshold_image.set_image_height(image.get_image_height()) self.__threshold_image.set_image_width(image.get_image_width()) self.__i_writer.write_image(self.__threshold_image) return self.__threshold_image