def count_loops(img): global parent parent = [] for i in range(0, 50 * 50): parent.append(i) img = cv2.resize(img, (50, 50), interpolation=cv2.INTER_CUBIC) img = pre.otsu_thresh(img) dir = [[1, 1], [1, 0], [1, -1], [0, 1], [0, -1], [-1, 1], [-1, 0], [-1, -1]] r, c = img.shape for i in range(0, r): for j in range(0, c): if img[i, j] == 0: continue for k in range(0, 8): x = i + dir[k][0] y = j + dir[k][1] if bounds(x, y, r, c) and img[x, y] != 0: parenta = find(i * r + j) parentb = find(x * r + y) if parenta != parentb: union(parenta, parentb) cnt = 0 for i in range(0, r * c): if find(i) == i and img[int(i / r), i % r] != 0: cnt += 1 l = [] l.append(cnt) return l
def histo(img): # img = abs(255 - img) img = cv2.resize(img, (25, 25), interpolation=cv2.INTER_CUBIC) img = pre.otsu_thresh(img) img[img == 255] = 1 # util.display_image(img) vec = [] vsum = np.sum(img, axis=0) hsum = np.sum(img, axis=1) for i in vsum: vec.append(i) for i in hsum: vec.append(i) return vec
def pre_init(img): img = pre.cut_image(img) aspect = img.shape[0] / img.shape[1] img = cv2.resize(img, (50, 50), interpolation=cv2.INTER_CUBIC) img = pre.otsu_thresh(img) new_img = pre.thin_image(img) # changes for end-points counting # img = util.cnvt_bool_to_uint8(img) # kernel = np.ones((3,3),np.uint8) # dilation = cv2.dilate(img, kernel, iterations=1) # dilation[dilation==255]=1 return aspect, img, new_img
def give_me_the_equation(img, model): if len(img) == 0 or len(img[0]) == 0: return "" import preprocess as pre # util.display_image(img, 'do_ittttt') # print(path) # Converting light dark pixels ( <= 30 ) to black ( = 0 ) img = pre.filter_image(img) # Thresholding OTSU img = pre.otsu_thresh(img) ans = ali.align(img, model) print(ans) # util.display_image(img) # characters = seg.split_characters(img) # # for c in characters: # # util.display_image(c) # print("Do itt " + str(len(characters))) # ans = "" # idx = 0 # pre = 0 # for c in characters: # y = util.predict_class(c, model) # if align[idx] == 0: # if pre == 0: # ans = ans + str(y) # else: # ans = ans + "}" + str(y) # elif align[idx] == 1: # if pre == 0: # ans = ans + "^{" + str(y) # else: # ans = ans + str(y) # else: # if pre == 0: # ans = ans + "_{" + str(y) # else: # ans = ans + str(y) # # print(y) # pre = align[idx] # idx = idx + 1 # if pre != 0: # ans = ans + "}" # # print ("ADITYA") # # print(ans) return tx.tolatex(ans)
res = res + recurse(den, model) + "}^{" + recurse( num, model) + "}" else: res = res + "\\sum_{" res = res + recurse(den, model) + "}^{" + recurse( num, model) + "}" previ = i if prev == -1: prev = i if flag == 0: res = res + dt.give_me_the_equation(img[:, prev:previ + 1], model) # print('Recurse exit') return res # input the image path = os.getcwd() + '\\TestEquations\\' + 'eq47.jpg' # print(path) img = pre.input_image(path) util.display_image(img) # align = ali.align(img) # print("Result from CNN") # print(recurse(img, "cnn")) img = pre.filter_image(img) img = pre.otsu_thresh(img) print("Result from ANN") print(recurse(img, "ann")) util.display_image(img)
def run(img): #input image in grayscale # img = pre.input_image('/home/sarthak/ip/text1.png') # util.display_image(img, 'original') #filtering of the input image img = pre.filter_image(img) # util.display_image(img, 'filtered') #smoothening the image # img = preprocess.gaussian_blur(img) # utility.display_image(img, 'Smoothened') #binarization/thresholding of the image img = pre.otsu_thresh(img) # util.display_image(img, 'binarized') #horizontal histogram for line segmentation lines = hist.line_seg(img) # print(len(lines)) #displaying lines as recieved # for sline in lines: # util.display_image(sline) #vertical histogram for words segmentation words = [] for ekline in lines: # util.display_image(ekline) curr = hist.word_seg(ekline) words = words + curr # break #delete it # print(len(words)) # displaying words of lines as recieved # for ekword in words: # util.display_image(ekword) chars = [] #separating characters from words # cnt = 0 for ekword in words: # util.display_image(ekword) clist = hist.char_seg(ekword) chars.append(clist) # displaying characters as recieved # for clist in chars: # for ch in clist: # util.display_image(ch) # break return chars sum = 0 cnt = 0 maxx = -1 for word in chars: for charr in word: cnt += 1 maxx = max(maxx, len(charr[0])) sum += charr.shape[1] sum = sum/cnt print(sum) print(chars[0][2].shape) final = [] for word in chars: new_word = [] for charr in word: if charr.shape[1] > maxx: histo = img.sum(axis=0) minn = 1000 pos = -1 for i in range(0, len(histo)): if histo[i] < minn: minn = histo[i] pos = i new_word.append(charr[:,:pos+1]) new_word.append(charr[:, pos:]) else: new_word.append(charr) final.append(new_word) # for clist in final: # for ch in clist: # util.display_image(ch) # break return final # img = cv2.imread('/home/sarthak/ip/hello.jpg', 0) # run(img)