def handle(dirs, out_dir, clip): start_time = datetime.datetime.now() if not os.path.isdir(out_dir): os.mkdir(out_dir) files = getFiles(dirs) total = len(files) fail, success, skip, count = 0, 0, 0, 0 for f in files: count += 1 print(count, '/', total) img_dirs = os.path.join(out_dir, f.split("\\")[-1]) if os.path.isfile(img_dirs): skip += 1 continue try: # 读取图片 img = cv2.imread(f, 0) # 切边 x, w, y, h = clip img = img[x:w, y:h] # 去除噪点 img = moveNoise(img, 7) # 根据最大熵算法获得最佳阈值 threshed = maxEntrop(img) # 二值化 threshold, thrshed_img = cv2.threshold(img, threshed * 0.4, 255, cv2.THRESH_BINARY) # 使用区域生长法分割 img_segement, thresh_img = regionGrowing(img, thrshed_img) # 保存 saveImage(img_dirs, "_new", img_segement) # 打印信息到输出台 printToConsole(start_time, f, count, total, 5) success += 1 except Exception as e: # 错误情况 saveError(e, out_dir, f) fail += 1 end_time = datetime.datetime.now() expend = end_time - start_time print( "\n\ntotal: %d\nsuccessful: %d\nskip: %d\nfailed: %d\nExpend time: %s" % (total, success, skip, fail, expend)) os.startfile(out_dir)
def handle(dirs, out_dir, clip): start_time = datetime.datetime.now() if not os.path.isdir(out_dir): os.mkdir(out_dir) files = getFiles(dirs) total = len(files) fail, success, skip, count = 0, 0, 0, 0 for f in files: count += 1 print(count, '/', total) img_dirs = os.path.join(out_dir, f.split("\\")[-1]) if os.path.isfile(img_dirs): skip += 1 continue try: # 读取图片 img = cv2.imread(f, 0) # 切边 x, w, y, h = clip img = img[x:w, y:h] # 去除噪点 img = moveNoise(img, 7) # 获得图像像素均值 threshed = getMean(img) # 二值化 threshold, thrshed_img = cv2.threshold(img, threshed * 0.86, 255, cv2.THRESH_BINARY) saveImage(img_dirs, "_threshed_raw", thrshed_img) # 使用轮廓法(速度快)分割 img_segement, thresh_img = maxContour(img, thrshed_img) saveImage(img_dirs, "_threshed", thresh_img) # 去除多余边缘 img_remove_margin = moveMargin(img_segement, thresh_img) saveImage(img_dirs, "_remove_margin", img_remove_margin) # 扩充为正方形并缩小为256x256 img_new = normalization(img_remove_margin) saveImage(img_dirs, "_new", img_new) # 打印信息到输出台 printToConsole(start_time, f, count, total, 5) success += 1 except Exception as e: # 错误情况 saveError(e, out_dir, f) fail += 1 end_time = datetime.datetime.now() expend = end_time - start_time print( "\n\ntotal: %d\nsuccessful: %d\nskip: %d\nfailed: %d\nExpend time: %s" % (total, success, skip, fail, expend)) os.startfile(out_dir)
def handle(dirs, out_dir, clip, w0): start_time = datetime.datetime.now() if not os.path.isdir(out_dir): os.mkdir(out_dir) files = getFiles(dirs) total = len(files) fail, success, skip, count = 0, 0, 0, 0 for f in files: count += 1 print(count, '/', total) img_dirs = os.path.join(out_dir, f.split("\\")[-1]) if os.path.isfile(img_dirs): skip += 1 continue try: # 读取图片 img = cv2.imread(f, 0) # 切边 x,w,y,h = clip img = img[x:w , y:h] # saveImage(img_dirs, "_original", img) # 保存原图 # 获得预测值阈值,二值化 thresh_value = getThreshValuebySoftmax(img, w0) threshold, thrshed_img = cv2.threshold(img, thresh_value + 2, 255, cv2.THRESH_BINARY) saveImage(img_dirs, "_threshed_raw", thrshed_img) # 使用区域生长法分割 img_segement, thresh_img = regionGrowing(img, thrshed_img) saveImage(img_dirs, "_threshed", thresh_img) # 去除多余边缘 img_remove_margin = moveMargin(img_segement, thresh_img) saveImage(img_dirs, "_remove_margin", img_remove_margin) # 扩充为正方形并缩小为256x256 img_new = normalization(img_remove_margin) saveImage(img_dirs, "_new", img_new) # 打印信息到输出台 printToConsole(start_time, f, count, total, 5) success += 1 except Exception as e: # 错误情况 saveError(e, out_dir, f) fail += 1 traceback.print_exc() end_time = datetime.datetime.now() expend = end_time - start_time print("\n\ntotal: %d\nsuccessful: %d\nskip: %d\nfailed: %d\nExpend time: %s" %(total, success, skip, fail, expend)) os.startfile(out_dir)
def handle(dirs, out_dir): start_time = datetime.datetime.now() if not os.path.isdir(out_dir): os.mkdir(out_dir) files = getFiles(dirs) total = len(files) fail, success, skip, count = 0, 0, 0, 0 for f in files: count += 1 print(count, '/', total) img_dirs = os.path.join(out_dir, f.split("\\")[-1]) if os.path.isfile(img_dirs): skip += 1 continue try: img = cv2.imread(f, 0) # img_new = moveMargin(img, img) img_new = normalization(img_new) saveImage(img_dirs, "", img_new) # 打印信息到输出台 printToConsole(start_time, f, count, total, 5) success += 1 except Exception as e: # 错误情况 saveError(e, out_dir, f) fail += 1 end_time = datetime.datetime.now() expend = end_time - start_time print( "\n\ntotal: %d\nsuccessful: %d\nskip: %d\nfailed: %d\nExpend time: %s" % (total, success, skip, fail, expend)) os.startfile(out_dir)
def handle(dirs, out_dir, clip): start_time = datetime.datetime.now() if not os.path.isdir(out_dir): os.mkdir(out_dir) files = getFiles(dirs) total = len(files) fail, success, skip, count = 0, 0, 0, 0 for f in files[:20]: count += 1 print(count, '/', total) img_dirs = os.path.join(out_dir, f.split("\\")[-1]) if os.path.isfile(img_dirs): skip += 1 continue try: # 读取图片 img = cv2.imread(f, 0) # 切边 x,w,y,h = clip img = img[x:w , y:h] # 去除噪点 img = moveNoise(img, 7) # 根据最大熵算法获得最佳阈值 threshed = getDBSCANvalue(img) # 二值化 # thrshed_img = np.zeros(np.shape(img)) # img_m, img_n = np.shape(img) # for i in range(img_m): # for j in range(img_n): # if img[i, j] >= threshed: # thrshed_img[i, j] = 255 threshold, thrshed_img = cv2.threshold(img, threshed, 255, cv2.THRESH_BINARY) # saveImage(img_dirs, "_threshed_raw", thrshed_img) # 使用区域生长法分割 img_segement, thresh_img = regionGrowing(img, thrshed_img) saveImage(img_dirs, "_threshed", thresh_img) # 去除多余边缘 img_remove_margin = moveMargin(img_segement, thresh_img) # saveImage(img_dirs, "_remove_margin", img_remove_margin) # 扩充为正方形并缩小为256x256 img_new = normalization(img_remove_margin) # saveImage(img_dirs, "_new", img_new) # 打印信息到输出台 printToConsole(start_time, f, count, total, 5) success += 1 except Exception as e: # 错误情况 saveError(e, out_dir, f) fail += 1 end_time = datetime.datetime.now() expend = end_time - start_time print("\n\ntotal: %d\nsuccessful: %d\nskip: %d\nfailed: %d\nExpend time: %s" %(total, success, skip, fail, expend)) os.startfile(out_dir)