基于深度学习的病理图像分析
项目地址:https://github.com/BohriumKwong/Deep_learning_in_WSI.git
因为之前已经将项目下载至本地,所以直接上传至云端硬盘即可:
也可以在笔记本中通过git命令上传文件,详情见下文:
git clone 项目地址
在笔记本中,点击“修改”——“笔记本设置”——“选择GPU”
等待“分配”——“连接”——“初始化”后,右上角显示GPU详细信息:
查看当前使用的GPU
!nvidia-smi
连接你的google drive:
from google.colab import drive
drive.mount('/content/drive')
shift+enter运行后,点击链接登录谷歌账号复制密钥并粘贴至笔记本中的密钥框内即可
进入文件夹:
!ls
%cd /content/drive/MyDrive/Colab Notebooks
git clone https://github.com/BohriumKwong/Deep_learning_in_WSI.git
配置:
!sudo apt update &&apt install -y openslide-tools
import openslide
!pip install pyvips
%cd /content/drive/MyDrive/Colab Notebooks/Deep_learning_in_WSI
!ls
安装一些需要的包,要求在requirements.txt内
也可以通过以下命令安装:
(先将requirements文件内的第一行tensorflow-gpu删掉)
!pip install -r requirements.txt
运行demo文件,注意项目路径:
!python '/content/drive/MyDrive/Colab Notebooks/Deep_learning_in_WSI/opencv/opencv_demo.py'
以下代码在opencv/opencv_demo.py 中
import numpy as np
import matplotlib.pyplot as plt
import cv2
svs_img = cv2.imread('/content/drive/MyDrive/Colab Notebooks/Deep_learning_in_WSI/tricks_in_processing_and_training/16558.png')
plt.rcParams['figure.figsize'] = 10, 10
plt.imshow(svs_img)
def get_tissue(im, contour_area_threshold):
"""
Get the tissue contours from image(im)
:param im: numpy 3d-array object, image with RGB mode
:param contour_area_threshold: python integer, contour area threshold, tissue contour is less than it will omit
:return: tissue_cnts: python list, tissue contours that each element is numpy array with shape (n, 2)
"""
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5,5), 0)
binary = cv2.threshold(blurred, 230, 255, cv2.THRESH_BINARY_INV)[1]
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (7, 7))
# morphology = cv2.erode(binary, kernel, iterations = 0)
# erode是opencv中膨胀的操作
# morphology = cv2.dilate(morphology, kernel, iterations = 3)
# dilate是opencv中腐蚀的操作
morphology = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel)
# cv2.morphologyEx指定cv2.MORPH_OPEN参数就是进行开操作
cnts, _ = cv2.findContours(morphology.copy(),cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 对处理后的二值图像进行轮廓提取,并过滤掉轮廓面积小于设定阈值的轮廓
tissue_cnts = []
for each, cnt in enumerate(cnts):
contour_area = cv2.contourArea(cnt)
if contour_area < contour_area_threshold:
# omit the small area contour
del cnts[each]
continue
tissue_cnts.append(np.squeeze(np.asarray(cnt)))
# initialize mask to zero
mask = np.zeros((im.shape[0], im.shape[1])).astype(im.dtype)
color = [1]
mask = cv2.fillPoly(mask, cnts, color)
return mask, cnts
mask, cnts = get_tissue(svs_img, contour_area_threshold=1000)
plt.imshow(mask)
svs_img_new = svs_img.copy()
for i,contour in enumerate(cnts):
if cv2.contourArea(cnts[i]) > 1000:
cv2.drawContours(svs_img_new,cnts,i,(76,177,34),15)
# 轮廓着色的方法
plt.imshow(svs_img_new)
我也没太搞懂这个项目,后续学习会继续补充,也欢迎大佬指导!