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