def get_micrcode(file_name, img_for_extraction_path, dir_path, img): # Read the image # og_img = cv2.imread(file_name, 0) # height, width = og_img.shape[:2] # og_img = cv2.resize(og_img,(width//2, height//2), interpolation = cv2.INTER_AREA) height, width = img.shape[:2] og_image_contours = img #utils.display_image('display_image', og_img) # Thresholding the image (thresh, img_bin) = cv2.threshold(img, 127, 255,cv2.THRESH_BINARY) # Invert the image # utils.display_image('display_image', img_bin) img_bin_inv = 255-img_bin # utils.display_image('display_image', img_bin_inv) crop_img = img_bin_inv[height - height//6 : , : ] # utils.display_image('display_image', crop_img) print("MICR Code") utils.store_img(dir_path, file_name, crop_img, "micrcode")
def extract_boxes(file_name, img_for_extraction_path, dir_path, img): folder_name = filename.split(".")[0] store_loc = dir_path + "date_segmented2/" print(store_loc) height, width = img.shape[:2] print(width) delta = (width // 100) digit_width = (width // 8) temp = None for i in range(0, 8): if i is 0: output_image = img[height // 20:height - (height // 20), delta:((i + 1) * digit_width) - delta] h, w = output_image.shape[:2] # utils.display_image("image", output_image) gap_x = w // 6 gap_y = h // 6 vis = np.zeros((h + (2 * gap_y), w + (2 * gap_x)), np.uint8) vis[gap_y:gap_y + h, gap_x:gap_x + w] = output_image output_image = vis # utils.display_image("image", output_image) kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3)) output_image = cv2.dilate(output_image, kernel, iterations=2) # utils.display_image("image", output_image) output_image = cv2.resize(output_image, (28, 28), interpolation=cv2.INTER_AREA) # utils.display_image("image", output_image) amount_image = output_image utils.store_img(store_loc, "position_" + str(i) + ".tif", output_image, folder_name) elif i is 7: output_image = img[height // 20:height - (height // 20), (i * digit_width) + delta:width - delta] h, w = output_image.shape[:2] # utils.display_image("image", output_image) gap_x = w // 6 gap_y = h // 6 vis = np.zeros((h + (2 * gap_y), w + (2 * gap_x)), np.uint8) vis[gap_y:gap_y + h, gap_x:gap_x + w] = output_image output_image = vis # utils.display_image("image", output_image) kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3)) output_image = cv2.dilate(output_image, kernel, iterations=2) # utils.display_image("image", output_image) output_image = cv2.resize(output_image, (28, 28), interpolation=cv2.INTER_AREA) # utils.display_image("image", output_image) crop_img = output_image h1, w1 = amount_image.shape[:2] h2, w2 = crop_img.shape[:2] vis = np.zeros((max(h1, h2), w1 + w2), np.uint8) vis[:h1, :w1] = amount_image vis[:h2, w1:w1 + w2] = crop_img amount_image = vis utils.store_img(store_loc, "position_" + str(i) + ".tif", output_image, folder_name) else: output_image = img[height // 20:height - (height // 20), (i * digit_width) + delta:((i + 1) * digit_width) - delta] h, w = output_image.shape[:2] gap_x = w // 6 gap_y = h // 6 vis = np.zeros((h + (2 * gap_y), w + (2 * gap_x)), np.uint8) vis[gap_y:gap_y + h, gap_x:gap_x + w] = output_image output_image = vis kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (3, 3)) # output_image = cv2.dilate(output_image, kernel, iterations=2) output_image = cv2.resize(output_image, (28, 28), interpolation=cv2.INTER_AREA) # utils.display_image("image", output_image) utils.store_img(store_loc, "position_" + str(i) + ".tif", output_image, folder_name) crop_img = output_image h1, w1 = amount_image.shape[:2] h2, w2 = crop_img.shape[:2] vis = np.zeros((max(h1, h2), w1 + w2), np.uint8) vis[:h1, :w1] = amount_image vis[:h2, w1:w1 + w2] = crop_img amount_image = vis
def find_boxes(file_name, img_for_box_extraction_path, dir_path, img): # Read the image # path = file_name # print(path) # img = cv2.imread(path , 0) # height, width = img.shape[:2] # img = cv2.resize(img,(width//2, height//2), interpolation = cv2.INTER_AREA) height, width = img.shape[:2] og_img = img # Thresholding the image (thresh, img_bin) = cv2.threshold(img, 160, 255, cv2.THRESH_BINARY) # Invert the image img_bin = 255 - img_bin #utils.display_image('display_image', img_bin) extraction_img = img_bin kernel = cv2.getStructuringElement( cv2.MORPH_CROSS, (3, 3) ) # to manipulate the orientation of dilution , large x means horizonatally dilating more, large y means vertically dilating more dilated = cv2.dilate( img_bin, kernel, iterations=2) # dilate , more the iteration more the dilation # utils.display_image('display_image', dilated) # utils.store_img(dir_path, "1" + file_name, dilated, "boxes_temp") img_bin = dilated img_bin = cv2.GaussianBlur(img_bin, (5, 5), 0) # utils.display_image('display_image', img_bin) # utils.store_img(dir_path, "2" + file_name, img_bin, "boxes_temp") # Defining a kernel length kernel_length = np.array(img).shape[1] // 80 # A verticle kernel of (1 X kernel_length), which will detect all the verticle lines from the image. verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length)) # A horizontal kernel of (kernel_length X 1), which will help to detect all the horizontal line from the image. hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1)) # A kernel of (3 X 3) ones. kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3)) # Morphological operation to detect vertical lines from an image img_temp1 = cv2.erode(img_bin, verticle_kernel, iterations=2) # utils.display_image('display_image', img_temp1) verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=2) (thresh, verticle_lines_img) = cv2.threshold(verticle_lines_img, 127, 255, cv2.THRESH_BINARY) # utils.display_image('display_image', verticle_lines_img) # utils.store_img(dir_path, "3" + file_name, verticle_lines_img, "boxes_temp") # Morphological operation to detect horizontal lines from an image img_temp2 = cv2.erode(img_bin, hori_kernel, iterations=2) # utils.display_image('display_image', img_temp2) horizontal_lines_img = cv2.dilate(img_temp2, hori_kernel, iterations=2) (thresh, horizontal_lines_img) = cv2.threshold(horizontal_lines_img, 127, 255, cv2.THRESH_BINARY) # utils.display_image('display_image', horizontal_lines_img) # utils.store_img(dir_path, "4" + file_name, horizontal_lines_img, "boxes_temp") # Weighting parameters, this will decide the quantity of an image to be added to make a new image. alpha = 0.5 beta = 1.0 - alpha # This function helps to add two image with specific weight parameter to get a third image as summation of two image. img_final_bin = cv2.addWeighted(verticle_lines_img, alpha, horizontal_lines_img, beta, 0.0) # utils.display_image('display_image', img_final_bin) img_final_bin = cv2.erode(~img_final_bin, kernel, iterations=2) # utils.display_image('display_image', img_final_bin) (thresh, img_final_bin) = cv2.threshold(img_final_bin, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU) # utils.display_image('display_image', img_final_bin) # utils.store_img(dir_path, "5" + file_name, img_final_bin, "boxes_temp") # Find contours for image, which will detect all the boxes im2, contours, hierarchy = cv2.findContours(img_final_bin, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #utils.display_image('display_image', im2) # Sort all the contours by top to bottom. (contours, boundingBoxes) = utils.sort_contours(contours, method="top-to-bottom") idx = 0 account_no_flag = False date_flag = False amount_flag = False account_number_list = [] amount_list = [] for c in contours: # Returns the location and width,height for every contour x, y, w, h = cv2.boundingRect(c) if w < width // 20 or h < height // 20: continue if (isAccountNumber(width, height, x, y, w, h)): account_number_list.append(c) # print("Account Number") new_img = extraction_img[y:y + h, x:x + w] if (isAmount(width, height, x, y, w, h)): amount_list.append(c) new_img = extraction_img[y:y + h, x:x + w] if (isDate(width, height, x, y, w, h) and not date_flag): print("Date") date_flag = True new_img = og_img[y:y + h, x:x + w] # new_img = extraction_img[y:y+h, x:x+w] # utils.display_image('display_image', new_img) utils.store_img(dir_path, file_name, new_img, "date1") account_number_list = utils.sort_contours_area(account_number_list) amount_list = utils.sort_contours_area(amount_list) if len(account_number_list) != 0: for i in range(len(account_number_list)): x, y, w, h = cv2.boundingRect(account_number_list[0]) new_img = extraction_img[y:y + h, x:x + w] # utils.display_image('display_image', new_img) # utils.store_img(dir_path, file_name, new_img, "accNum") if len(amount_list) != 0: x, y, w, h = cv2.boundingRect(amount_list[0]) new_img = extraction_img[y:y + h, x:x + w]
def refine_date(file_name, img_for_box_extraction_path, dir_path, img): height, width = img.shape[:2] img = cv2.resize(img, (width * 2, height * 2), interpolation=cv2.INTER_LINEAR) height, width = img.shape[:2] (thresh, img) = cv2.threshold(img, 160, 255, cv2.THRESH_BINARY_INV) # utils.display_image('display_image', img) og_img = img # Invert the image img_bin_inv = 255 - img # Defining a kernel length kernel_length = np.array(img).shape[1] // 20 kernel_length_vertical = height // 2 kernel_length_horizontal = width // 4 # A verticle kernel of (1 X kernel_length), which will detect all the verticle lines from the image. verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length_vertical)) # A horizontal kernel of (kernel_length X 1), which will help to detect all the horizontal line from the image. hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length_horizontal, 1)) # A kernel of (3 X 3) ones. kernel = cv2.getStructuringElement(cv2.MORPH_CROSS, (2, 2)) img_temp = cv2.dilate(img, kernel, iterations=1) # utils.display_image('display_image', img_temp) # Morphological operation to detect horizontal lines from an image img_temp2 = cv2.erode(img_temp, hori_kernel, iterations=1) # utils.display_image('display_image', img_temp2) horizontal_lines_img = cv2.dilate(img_temp2, hori_kernel, iterations=1) (thresh, horizontal_lines_img) = cv2.threshold(horizontal_lines_img, 160, 255, cv2.THRESH_BINARY) # utils.display_image('display_image', horizontal_lines_img) im2, contours, hierarchy = cv2.findContours(horizontal_lines_img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) # Sort all the contours by top to bottom. (contours, boundingBoxes) = utils.sort_contours(contours, method="top-to-bottom") if len(contours) > 1: x1, y1, w1, h1 = cv2.boundingRect(contours[0]) x2, y2, w2, h2 = cv2.boundingRect(contours[len(contours) - 1]) print(cv2.boundingRect(contours[0])) print(cv2.boundingRect(contours[len(contours) - 1])) new_img = img_temp[y1 + h1:y2, x2:x1 + w2] # utils.display_image('display_image', new_img) # utils.store_img(dir_path, file_name, new_img, "date") # cv2.imwrite(dir_path + "/date/" + img_for_box_extraction_path + '.tif', new_img) # (thresh, new_img) = cv2.threshold(new_img, 32, 255,cv2.THRESH_BINARY) # utils.display_image('display_image', new_img) final_img = new_img # utils.display_image('display_image', final_img) # (thresh, final_img) = cv2.threshold(final_img, 32, 255,cv2.THRESH_BINARY) # utils.display_image('display_image', final_img) #final_image = remove_lines(final_img) # utils.store_img(dir_path, file_name, final_img, "date8") utils.store_img(dir_path, file_name, final_img, "date2") img_temp = final_img
def get_underline(img_for_extraction_path, file_name, dir_path, img): # height, width = og_img.shape[:2] # og_img = cv2.resize(og_img,(width//2, height//2), interpolation = cv2.INTER_AREA) height, width = img.shape[:2] og_img = slicing.slice_image_get_remaining(img) og_image_contours = og_img utils.display_image('display_image', og_img) # Thresholding the image (thresh, img_bin) = cv2.threshold(og_img, 160, 255, cv2.THRESH_BINARY) # Invert the image utils.display_image('display_image', img_bin) img_bin_inv = 255 - img_bin img_bin_inv_final = 255 - img_bin utils.display_image('display_image', img_bin_inv) # utils.store_img(dir_path, str(1) + "_" + img_for_extraction_path, img_bin_inv, "images_for_paper") img_bin_inv_blur = cv2.GaussianBlur(img_bin_inv, (3, 3), 0) utils.display_image('display_image', img_bin_inv_blur) kernel = cv2.getStructuringElement( cv2.MORPH_CROSS, (5, 2) ) # to manipulate the orientation of dilution , large x means horizonatally dilating more, large y means vertically dilating more img_bin_inv_blur_dilated = cv2.dilate( img_bin_inv_blur, kernel, iterations=3) # dilate , more the iteration more the dilation utils.display_image('display_image', img_bin_inv_blur_dilated) # utils.store_img(dir_path, str(2) + "_" + img_for_extraction_path, img_bin_inv_blur_dilated, "images_for_paper") kernel_length = np.array(img_bin).shape[1] // 5 hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1)) img_temp_horizontal = cv2.erode(img_bin_inv_blur_dilated, hori_kernel, iterations=1) utils.display_image('display_image', img_temp_horizontal) # utils.store_img(dir_path, str(3) + "_" + img_for_extraction_path, img_temp_horizontal, "images_for_paper") horizontal_lines_img = cv2.dilate(img_temp_horizontal, hori_kernel, iterations=1) utils.display_image('display_image', horizontal_lines_img) # utils.store_img(dir_path, img_for_extraction_path, horizontal_lines_img, "handwritten") (thresh, horizontal_lines_img) = cv2.threshold(horizontal_lines_img, 32, 255, cv2.THRESH_BINARY) utils.display_image('display_image', horizontal_lines_img) utils.store_img(dir_path, str("3a") + "_" + img_for_extraction_path, horizontal_lines_img, "images_for_paper") horizontal_lines_img = cv2.GaussianBlur(horizontal_lines_img, (5, 5), 0) utils.display_image('display_image', horizontal_lines_img) edges = cv2.Canny(horizontal_lines_img, 127, 255) lines = cv2.HoughLinesP(edges, 1, np.pi / 180, 30, minLineLength=height // 1.5) img_bin_inv_contours = img_bin_inv # if lines is None: # return # for line in lines: # x1, y1, x2, y2 = line[0] # cv2.line(img_bin_inv_contours, (x1, y1), (x2, y2), (0, 255, 0), 3) image, contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) utils.display_image('display_image', image) # utils.store_img(dir_path, str(4) + "_" + img_for_extraction_path, image, "images_for_paper") cv2.drawContours(img_bin_inv_contours, contours, -1, (255, 255, 255), 3) utils.display_image('display_image', img_bin_inv_contours) # Sort all the contours by top to bottom. (contours, boundingBoxes) = utils.sort_contours(contours, method="top-to-bottom") idx = 0 underline = "underline" flag = False amount_image = None previous = None # utils.display_image1(img_bin_inv_final) for contour in contours: # get rectangle bounding contour [x, y, w, h] = cv2.boundingRect(contour) # Don't plot small false positives that aren't text if w < width / (2): continue if ((y - (height // 8)) > 0): utils.display_image( 'captcha_result', img_bin_inv_final[y - (height // 10):y + h + (height // 100), width // 10:width - width // 5]) # utils.display_image('captcha_result', crop_img) # utils.store_img(dir_path, underline + "_" + str(idx) + "_" + img_for_extraction_path, crop_img, "handwritten_payee") else: # crop_img = img_bin_inv_final[0 : y + h + (height // 100), x : x + w] utils.display_image( 'captcha_result', img_bin_inv_final[0:y + h + (height // 100), width // 10:width - width // 5]) utils.display_image( 'captcha_result', img_bin_inv_final[y - (height // 10):y + h + (height // 100), width // 10:width - width // 5]) # y_act = y - (height // 12) # h_act = y_act + (height // 12) + (h + (height // 100)) if flag: if previous is None: # print ("Previous is None") previous = [0, y, width, h] else: # print (previous) # print ([0, y, width, h]) overlap = utils.find_overlap(img_bin_inv_final, previous, [0, y, width, h]) # previous = [0, y, width, h] if overlap > 0.1: continue else: previous = [0, y, width, h] if not flag: # draw rectangle around contour on original image # cv2.rectangle(og_image_contours, (x, y , (x + w, y + h + (height // 100), (0, 0, 0), 2) flag = True # delta = (height // 100) if ((y - (height // 8)) > 0): crop_img = img_bin_inv_final[y - (height // 10):y + h + (height // 100), width // 10:width - width // 5] # utils.display_image('captcha_result', crop_img) # utils.store_img(dir_path, underline + "_" + str(idx) + "_" + img_for_extraction_path, crop_img, "handwritten_payee") else: # crop_img = img_bin_inv_final[0 : y + h + (height // 100), x : x + w] crop_img = img_bin_inv_final[0:y + h + (height // 100), width // 10:width - width // 5] # utils.display_image('captcha_result', crop_img) # utils.store_img(dir_path, underline + "_" + str(idx) + "_" + img_for_extraction_path, crop_img, "handwritten_payee") else: if ((y - (height // 8)) > 0): crop_img = img_bin_inv_final[y - (height // 12):y + h + (height // 100), :] else: # crop_img = img_bin_inv_final[0 : y + h + (height // 100), x : x + w] crop_img = img_bin_inv_final[0:y + h + (height // 100), :] # utils.display_image('captcha_result', crop_img) # print("Underline") # utils.store_img(dir_path, underline + "_" + str(idx) + "_" + img_for_extraction_path, crop_img, "handwritten") if amount_image is None: amount_image = crop_img else: h1, w1 = amount_image.shape[:2] h2, w2 = crop_img.shape[:2] vis = np.zeros((max(h1, h2), w1 + w2), np.uint8) vis[:h1, :w1] = amount_image vis[:h2, w1:w1 + w2] = crop_img amount_image = vis # utils.display_image('captcha_result', amount_image) # print("Underline") # utils.store_img(dir_path, underline + "_" + str(idx) + "_" + img_for_extraction_path, crop_img, "handwritten") idx = idx + 1 height, width = amount_image.shape[:2] # utils.display_image('captcha_result', amount_image) amount_image[:, width - ((width) // 5):] = 0 # utils.display_image('captcha_result', amount_image) # width//2 - (width//2)//20 amount_image[:, width // 2 - (width // 2) // 15:width // 2 + (width // 2) // 15] = 0 # utils.display_image('captcha_result', amount_image) # utils.store_img(dir_path, underline + "_" + str(idx) + "_" + img_for_extraction_path, amount_image, "handwritten_concatenated") if idx == 4: print(file_name) amount_image = amount_image[:, :width // 2]
def remove_underline_amount(img_for_extraction_path, file_name, dir_path, img): # utils.store_img(dir_path, img_for_extraction_path, final_img, "amount") height, width = img.shape[:2] # img = img[ height//20 : , width//20 : width - width//5] final_img = img temp_image = img utils.display_image('display_image', img) kernel = cv2.getStructuringElement( cv2.MORPH_CROSS, (5, 1) ) # to manipulate the orientation of dilution , large x means horizonatally dilating more, large y means vertically dilating more img_dilated = cv2.dilate( img, kernel, iterations=2) # dilate , more the iteration more the dilation utils.display_image('display_image', img_dilated) kernel_length = np.array(img_dilated).shape[1] // 8 hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1)) img_temp_horizontal = cv2.erode(img_dilated, hori_kernel, iterations=1) utils.display_image('display_image', img_temp_horizontal) horizontal_lines_img = cv2.dilate(img_temp_horizontal, hori_kernel, iterations=1) utils.display_image('display_image', horizontal_lines_img) new_img = img - horizontal_lines_img utils.display_image('display_image', new_img) # final_img = new_img[height//20 : , width//20 : width] final_img = new_img[height // 20:, :] kernel = cv2.getStructuringElement( cv2.MORPH_RECT, (5, 5) ) # to manipulate the orientation of dilution , large x means horizonatally dilating more, large y means vertically dilating more dilated_img = cv2.dilate(new_img, kernel, iterations=2) utils.display_image('display_image', dilated_img) new_img = dilated_img # im2, contours, hierarchy = cv2.findContours(new_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) # # utils.display_image('display_image', im2) # (contours, boundingBoxes) = utils.sort_contours(contours, method="left_to_right") # # contours = utils.sort_contours_area(contours) # length = len(contours) # trim_size = length // 2 # print ("Length : " + str(length) + " -- trim_size : " + str(trim_size)) # for i in range(0, length): # # and i < length - 2*trim_size # if(i >= trim_size): # continue # contour = contours[i] # # get rectangle bounding contour # [x, y, w, h] = cv2.boundingRect(contour) # if w > width//50 and h > height//2: # continue # # Don't plot small false positives that aren't text # # if w < width//50 or h < height//3: # # continue # # draw rectangle around contour on original image # cv2.rectangle(temp_image, (x , y), (x + w , y + h), (255, 255, 255), 2) # crop_img = new_img[y : y + h, x : x + w] # # utils.display_image('captcha_result', new_img[y : y + h, x : x + w]) # # new_img[y : y + h, x : x + w] = 0 # final_img[y : y + h, x : x + w] = 0 # # utils.display_image('captcha_result', final_img) # print("contour") # utils.display_image('captcha_result', final_img) # utils.store_img(dir_path, img_for_extraction_path, final_img, "name") utils.store_img(dir_path, str(6) + "_" + img_for_extraction_path, final_img, "images_for_paper")
np.random.shuffle(data) n_data = len(data) n_test = int(np.ceil(n_data * args.split)) n_train = n_data - n_test print("Created {} new image mask pairs".format(n_data)) print("#Train: ", n_train) print("#Test: ", n_test) print("split: ", args.split) print("train: ", d_train) print("test: ", d_test) print("Saving test images to disk ...") for i in range(n_test): X, Y = data[i] f_x = os.path.join(d_test, file_form.format(i, "x")) f_y = os.path.join(d_test, file_form.format(i, "y")) utils.store_img(X, f_x) utils.store_img(Y, f_y) print("Saving train images to disk ...") for i in range(n_test, n_data): X, Y = data[i] f_x = os.path.join(d_train, file_form.format(i, "x")) f_y = os.path.join(d_train, file_form.format(i, "y")) utils.store_img(X, f_x) utils.store_img(Y, f_y) print("Done")