def main2(): dstroot = 'clusters' listcells = os.listdir(dstroot) cellsinfo = [] for n, i in zip(listcells, tqdm(range(len(listcells)))): cellinfo = {} cellpath = os.path.join(dstroot, n) img = cv2.imread(cellpath) img_gray = cv2.imread(cellpath, 0) img_gray = bf.get_fit_img(img_gray) # cv2.imwrite(cellpath+'abc0.png',img_gray) value1_, value_2 = bf.get_2value(img_gray) value_1, _ = bf.get_2value(img_gray, grien=3) sign_, cell_nuclei_mask1, area1 = get_cell_nuclei_mask( img_gray, img, value_1) #获取细胞核掩码、个数、面积、凸面积、周长、核形规则度、获取细胞>核深染程度 sign_, cell_nuclei_mask2, area2 = get_cell_nuclei_mask( img_gray, img, value1_) #获取细胞核掩码、个数、面积、凸面积、周长、核形规则度、获取细胞核深染 if sign_ == 0: continue # cv2.imwrite(cellpath+'abc1.png',cell_nuclei_mask2) temp = area1 / area2 temp1 = str(temp)[0:5] if area1 / area2 > 0.85: newpath = os.path.join('ab_clusters', n + '_' + temp1 + '.png') shutil.copy(cellpath, newpath)
def main2(): dstroot = 'fovs' list_dst = os.listdir(dstroot) for n, i in zip(list_dst, tqdm(range(len(list_dst)))): imgpath = os.path.join(dstroot, n) # print(imgpath) img_org = cv2.imread(imgpath) img_gray = cv2.imread(imgpath, 0) img_gray_fit = bf.get_fit_img(img_gray) value_1, value_2 = bf.get_2value(img_gray_fit) # 获取细胞核、背景阈值 kernel_1 = np.ones((50, 50), np.uint8) kernel_2 = np.ones((5, 5), np.uint8) cells_xoy = get_cells(img_gray_fit, imgpath, value_2 + 10, kernel_1, 50000, 1600, side=5) value_1, value_2 = bf.get_2value(img_gray_fit, grien=3) clusters_xoy = get_clusters(img_gray_fit, value_1, kernel_2, 20000, 1000000, side=5) crop_from_fov(img_org, n, cells_xoy) crop_from_fov(img_org, n, clusters_xoy, root_='clusters')
def main2(): dstroot = 'crop' listcells = os.listdir(dstroot) cellsinfo = [] for n, i in zip(listcells, tqdm(range(len(listcells)))): cellinfo = {} cellpath = os.path.join(dstroot, n) img = cv2.imread(cellpath) img_gray = cv2.imread(cellpath, 0) img_gray = bf.get_fit_img(img_gray) # cv2.imwrite(cellpath+'abc0.png',img_gray) value_1, value_2 = bf.get_2value(img_gray) sign_nuclei, cell_nuclei_mask, nuclei_cnt, nuclei_area, nuclei_hull_area, nuclei_circ, nuclei_rule, cell_nuclei_value = get_cell_nuclei_mask( img_gray, img, value_1) #获取细胞核掩码、个数、面积、凸面积、周长、核形规则度、获取细胞核深染程度 # cv2.imwrite(cellpath+'abc1.png',cell_nuclei_mask) if sign_nuclei == 0: continue sign_cytoplasm, cell_cytoplasm_mask, cytoplasm_area, cytoplasm_hull_area, cytoplasm_circ, cytoplasm_rule, cell_cytoplasm_value, cytoplasm_var = get_cell_cytoplasm_mask( img_gray, cell_nuclei_mask, nuclei_area, img, value_2) #获取细胞质掩码、面积、凸面积、周长、细胞规则度、获取细胞质情况 # cv2.imwrite(cellpath+'abc2.png',cell_cytoplasm_mask) if sign_cytoplasm == 0 or (cytoplasm_area - nuclei_area) == 0: continue cell_N_C = nuclei_area / (cytoplasm_area - nuclei_area) #计算核质比 cellinfo_keys = [ 'cellpath', 'nuclei_cnt', 'nuclei_area', 'nuclei_hull_area', 'nuclei_circ', 'nuclei_rule', 'cell_nuclei_value', 'cytoplasm_area', 'cytoplasm_hull_area', 'cytoplasm_circ', 'cytoplasm_rule', 'cell_cytoplasm_value', 'cytoplasm_var', 'cell_N_C' ] cellinfo_values = [ cellpath, nuclei_cnt, nuclei_area, nuclei_hull_area, nuclei_circ, nuclei_rule, cell_nuclei_value, cytoplasm_area, cytoplasm_hull_area, cytoplasm_circ, cytoplasm_rule, cell_cytoplasm_value, cytoplasm_var, cell_N_C ] cellinfo = dict(zip(cellinfo_keys, cellinfo_values)) cell_save_sign = get_cell_save_sign(cellinfo) if cell_save_sign == 0: continue cellsinfo.append(cellinfo) newpath = os.path.join('valid', n) shutil.copy(cellpath, newpath) np.save("./cells_info/cells_info.npy", cellsinfo)
1000000, side=5) crop_from_fov(img_org, n, cells_xoy) crop_from_fov(img_org, n, clusters_xoy, root_='clusters') if __name__ == "__main__": dstroot = 'fovs' list_dst = os.listdir(dstroot) for n, i in zip(list_dst, tqdm(range(len(list_dst)))): imgpath = os.path.join(dstroot, n) # print(imgpath) img_org = cv2.imread(imgpath) img_gray = cv2.imread(imgpath, 0) img_gray_fit = bf.get_fit_img(img_gray) value_1, value_2 = bf.get_2value(img_gray_fit) # 获取细胞核、背景阈值 kernel_1 = np.ones((50, 50), np.uint8) kernel_2 = np.ones((5, 5), np.uint8) cells_xoy = get_cells(img_gray_fit, imgpath, value_2 + 10, kernel_1, 50000, 1600, side=5) value_1, value_2 = bf.get_2value(img_gray_fit, grien=3) clusters_xoy = get_clusters(img_gray_fit, value_1, kernel_2, 20000, side=5)
if area1 / area2 > 0.85: newpath = os.path.join('ab_clusters', n + '_' + temp1 + '.png') shutil.copy(cellpath, newpath) if __name__ == "__main__": dstroot = 'clusters' listcells = os.listdir(dstroot) cellsinfo = [] for n, i in zip(listcells, tqdm(range(len(listcells)))): cellinfo = {} cellpath = os.path.join(dstroot, n) img = cv2.imread(cellpath) img_gray = cv2.imread(cellpath, 0) img_gray = bf.get_fit_img(img_gray) # cv2.imwrite(cellpath+'abc0.png',img_gray) value1_, value_2 = bf.get_2value(img_gray) value_1, _ = bf.get_2value(img_gray, grien=3) sign_, cell_nuclei_mask1, area1 = get_cell_nuclei_mask( img_gray, img, value_1) #获取细胞核掩码、个数、面积、凸面积、周长、核形规则度、获取细胞>核深染程度 sign_, cell_nuclei_mask2, area2 = get_cell_nuclei_mask( img_gray, img, value1_) #获取细胞核掩码、个数、面积、凸面积、周长、核形规则度、获取细胞核深染 if sign_ == 0: continue # cv2.imwrite(cellpath+'abc1.png',cell_nuclei_mask2) temp = area1 / area2 temp1 = str(temp)[0:5] if area1 / area2 > 0.85: newpath = os.path.join('ab_clusters', n + '_' + temp1 + '.png') shutil.copy(cellpath, newpath)
np.save("./cells_info/cells_info.npy", cellsinfo) if __name__ == "__main__": dstroot = 'crop' listcells = os.listdir(dstroot) cellsinfo = [] for n, i in zip(listcells, tqdm(range(len(listcells)))): cellinfo = {} cellpath = os.path.join(dstroot, n) # print(cellpath) img = cv2.imread(cellpath) img_gray = cv2.imread(cellpath, 0) img_gray = bf.get_fit_img(img_gray) #cv2.imwrite(cellpath+'abc0.png',img_gray) value_1, value_2 = bf.get_2value(img_gray) sign_nuclei, cell_nuclei_mask, nuclei_cnt, nuclei_area, nuclei_hull_area, nuclei_circ, nuclei_rule, cell_nuclei_value = get_cell_nuclei_mask( img_gray, img, value_1) #获取细胞核掩码、个数、面积、凸面积、周长、核形规则度、获取细胞核深染程度 #cv2.imwrite(cellpath+str(cell_nuclei_value)[0:5]+'abc1.png',cell_nuclei_mask) if sign_nuclei == 0: continue sign_cytoplasm, cell_cytoplasm_mask, cytoplasm_area, cytoplasm_hull_area, cytoplasm_circ, cytoplasm_rule, cell_cytoplasm_value, cytoplasm_var = get_cell_cytoplasm_mask( img_gray, cell_nuclei_mask, nuclei_area, img, value_2) #获取细胞质掩码、面积、凸面积、周长、细胞规则度、获取细胞质情况 #cv2.imwrite(cellpath+'abc2.png',cell_cytoplasm_mask) if sign_cytoplasm == 0 or (cytoplasm_area - nuclei_area) == 0: continue cell_N_C = nuclei_area / (cytoplasm_area - nuclei_area) #计算核质比 cellinfo_keys = [ 'cellpath', 'nuclei_cnt', 'nuclei_area', 'nuclei_hull_area', 'nuclei_circ', 'nuclei_rule', 'cell_nuclei_value',
else: return max(0, (A_max_std - A_std) / (A_max_std - A_min_std)) if __name__ == "__main__": dstroot = 'crop' # find_invalid_cells(dstroot,50) list__ = os.listdir(dstroot) for n in list__: imgpath = os.path.join(dstroot, n) img = cv2.imread(imgpath, 0) value_dhash = get_mindHash(img) img_fit = get_fit_img(img) cnt = get_2value(img_fit) res = get_entropy(img_fit) _, value2 = bf.get_2value(img_fit) ret_, thresh = cv2.threshold(img, value2, 255, cv2.THRESH_BINARY_INV) matrix_01 = thresh / 255 matrix_01 = cv2.resize(matrix_01, (64, 64)) rand_value = get_stand_std(matrix_01) kernel_1 = np.ones((5, 5), np.uint8) thresh = bf.get_img_open(thresh, kernel_1) # thresh = bf.get_img_close(thresh,kernel_1) image_, contours, hierarchy_ = cv2.findContours( thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) rule = 100 if len(contours) == 0: rule = 100 else: area_ = 0 contours_max = []