def separate_words(line): line_hist = cv2.reduce(line, 0, cv2.REDUCE_AVG).reshape(-1) new_line = cv2.cvtColor(line, cv2.COLOR_GRAY2BGR) line_peaks = peakdetect(line_hist, lookahead=50) Hl, Wl = new_line.shape[:2] words = [] for y in line_peaks[0]: if y[1] == 255: words.append(y[0]) # plt.plot(y[0], y[1], "r*") if y[1] == 255: cv2.line(new_line, (y[0], 0), (y[0], Hl), (255, 0, 0), 3) # for y in line_peaks[1]: # plt.plot(y[0], y[1], "g*") # if y[1] == 255: # words.append(y[0]) # cv2.line(new_line, (y[0], 0), (y[0], Hl), (0, 255, 0), 3) words.insert(0, 0) words.append(Wl) # plt.imshow(new_line, cmap=plt.cm.gray) # plt.show() return words
def separate_cha(line): line_hist = cv2.reduce(line, 0, cv2.REDUCE_AVG).reshape(-1) new_line = cv2.cvtColor(line, cv2.COLOR_GRAY2BGR) line_peaks = peakdetect(line_hist, lookahead=25) Hl, Wl = new_line.shape[:2] cha = [] # for y in line_peaks[0]: # plt.plot(y[0], y[1], "r*") # cv2.line(new_line, (y[0], 0), (y[0], Hl), (255, 0, 0), 3) for y in line_peaks[0]: if y[1] >= 235: cha.append(y[0]) # plt.plot(y[0], y[1], "g*") cv2.line(new_line, (y[0], 0), (y[0], Hl), (0, 255, 0), 3) cha.insert(0, 0) cha.append(Wl) # plt.plot(line_hist) # plt.show() # plt.imshow(new_line, cmap=plt.cm.gray) # plt.show() return cha
def find_degree(image): min_score = 999999 degree = 0 for d in range(-6, 7): rotated_image = rotate_max_area(image, d) ri_hist = cv2.reduce(rotated_image, 1, cv2.REDUCE_AVG).reshape(-1) line_peaks = peakdetect(ri_hist, lookahead=30) score_ne = num_ne = 0 score_po = num_po = 0 for y in line_peaks[0]: score_ne -= (y[1] * 1) num_ne += 1 for y in line_peaks[1]: score_po += (y[1] * 1) num_po += 1 score = score_ne / num_ne + score_po / num_po print("score: ", score, " degree: ", d) print(": ", score_ne / num_ne, " : ", score_po / num_po) if score < min_score: degree = d min_score = score print(degree) rotated_image = rotate_max_area(image, degree) plt.imshow(rotated_image, cmap=plt.cm.gray) plt.show()
def separate_words(line): line_hist = cv2.reduce(line, 0, cv2.REDUCE_AVG).reshape(-1) new_line = cv2.cvtColor(line, cv2.COLOR_GRAY2BGR) line_peaks = peakdetect(line_hist, lookahead=20) Hl, Wl = new_line.shape[:2] for y in line_peaks[0]: plt.plot(y[0], y[1], "r*") cv2.line(new_line, (y[0], 0), (y[0], Hl), (255, 0, 0), 3) for y in line_peaks[1]: plt.plot(y[0], y[1], "g*") # cv2.line(new_line, (y[0], 0), (y[0], Hl), (0, 255, 0), 3) # print(y) plt.plot(line_hist) plt.show() return new_line
def find_degree(image): min_score = 999999 degree = 0 for d in range(-6, 7): rotated_image = rotate_max_area(image, d) # cv2.imwrite('./tr_' + str(d) + '.jpg', rotated_image) ri_hist = cv2.reduce(rotated_image, 1, cv2.REDUCE_AVG).reshape(-1) # plt.plot(ri_hist) # plt.savefig('./tr_' + str(d) + '_h.jpg') # plt.clf() # plt.show() line_peaks = peakdetect(ri_hist, lookahead=30) score_ne = num_ne = 0 score_po = num_po = 0 for y in line_peaks[0]: score_ne -= (y[1] * 1) num_ne += 1 for y in line_peaks[1]: score_po += (y[1] * 1) num_po += 1 score = score_ne / num_ne + score_po / num_po # print("score: ", score, " degree: ", d) # print(": ", score_ne / num_ne, " : ", score_po / num_po) if score < min_score: degree = d min_score = score # print('Degree: ', degree) rotated_image = rotate_max_area(image, degree) # plt.imshow(rotated_image, cmap=plt.cm.gray) # plt.show() return rotated_image
s_img_2 = img_as_ubyte(binary_sauvola) # cv2.imwrite('./t1_2.jpg', s_img_2) s_img_2[img3 == 255] = 255 # cv2.imwrite('./t4.jpg', s_img_2) new_img = cv2.cvtColor(s_img_2, cv2.COLOR_GRAY2BGR) rotated = find_degree(new_img) rotated = cv2.cvtColor(rotated, cv2.COLOR_RGB2GRAY) hist = cv2.reduce(rotated, 1, cv2.REDUCE_AVG).reshape(-1) H, W = rotated.shape[:2] peaks = peakdetect(hist, lookahead=40) rotated2 = cv2.cvtColor(rotated, cv2.COLOR_GRAY2BGR) peak = [] for y in peaks[0]: peak.append(y[0]) # plt.plot(y[0], y[1], "r*") cv2.line(rotated2, (0, y[0]), (W, y[0]), (255, 0, 0), 3) # for y in peaks[1]: # peak.append(y[0]) # plt.plot(y[0], y[1], "g*") # cv2.line(rotated, (0, y[0]), (W, y[0]), (0, 255, 0), 3) # plt.plot(hist) # plt.savefig('hist.jpg') # plt.clf()