def line_seg(img): img_not = abs(255 - img) #horizontal histogram histo = img_not.sum(axis=1) #smoothening histogram for better detection of lines y_smooth = savgol_filter(histo, 27, 3) #comparing histogram after smoothening # plt.plot(histo) # plt.plot(y_smooth) minimas = argrelextrema(y_smooth, np.less) arr = minimas[0] # print(arr) # plotting minimas in histogram i.e. line breaks # for i in arr: # plt.plot(i, y_smooth[i], 'ro') # plt.show() #creating a list of lines lines = [] prev = 0 for i in arr: if (prev != -1): curr = img_not[prev:i + 1, :] if (np.sum(curr) >= 6 * 255): curr = img[prev:i + 1, :] curr = pre.cut_image(curr) lines.append(curr) prev = i i = len(histo) - 1 curr = img_not[prev:i + 1, :] if (np.sum(curr) >= 6 * 255): curr = img[prev:i + 1, :] curr = pre.cut_image(curr) lines.append(curr) return lines
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 get_granule_count(processed_img: np.array) -> (int, np.array): """ Returns approximate count of circles in image, and distribution of circles' radiuses """ proc = cut_image(processed_img, 30) resized = cv2.resize(proc, (AVG_W, AVG_H)) avg_r, circles = draw_hough(resized) found = count_circles(proc, avg_r) radiuses = np.array([circle[2] for circle in circles]) distros = np.array( [np.where(radiuses == i)[0].shape[0] for i in range(1, 30)]) return found, distros / np.sum(distros)
def get_stats(im: np.array) -> (np.array, np.array): """ Returns stats and areas of connected components of the image Args: im: np.array Returns: stats: np.array - stats of every component label_area:np.array - area of every component """ img = cv2.resize(im, (hough.AVG_W, hough.AVG_H)) img = preprocess.cut_image(img, 30) mask = cv2.threshold( img[:, :, 0], 255, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU )[1] stats = cv2.connectedComponentsWithStats(mask, 4)[2] label_area = stats[1:, cv2.CC_STAT_AREA] return stats, label_area
def cut_images_save(img, if_show_pre=False, if_show=False, img_name='', save_path=''): global image_index print('**********************开始处理图片*************************') print('img_name: ', img_name) wrap = detect_image_counts(img, if_show_pre, if_show, img_name) print('wrap: ', wrap) if wrap == 2: print('-------------此图片有两张发票----------------') imgs_list = cut_image(img) for img in imgs_list: cv2.imwrite(save_path + str(image_index) + '.png', img) image_index += 1 else: cv2.imwrite(save_path + str(image_index) + '.jpg', img) image_index += 1 print( '***************************************处理图片完成*************************************' )