基于深度学习的CT图像肺结节自动检测技术六—模型预测

本文介绍了一种利用深度学习技术进行肺部CT影像中结节检测的方法。通过使用UNet和3D-CNN模型,实现了从二维图像到三维体积数据的结节检测,包括预处理、模型训练与预测流程。关键步骤包括图像阈值化、形态学操作、中心点定位及模型预测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#模型预测的相关功能

from chapter4 import get_unet
from chapter5 import get_3dnnnet, stack_2dcube_to_3darray, prepare_image_for_net3D, MEAN_PIXEL_VALUE
import glob
import cv2
import numpy as np
from scipy import ndimage
from scipy.ndimage.measurements import center_of_mass
from skimage import morphology

CHANNEL_COUNT = 1
_3DCNN_WEIGHTS = './model/3dcnn.hd5'
UNET_WEIGHTS = './model/unet.hd5'
THRESHOLD = 2
BATCH_SIZE = 1


# 获取unet预测结果的中心点坐标(x,y)
def unet_candidate_dicom(unet_result_path):
    centers = []
    image_t = cv2.imread(unet_result_path, cv2.IMREAD_GRAYSCALE)
    # Thresholding(阈值化)
    image_t[image_t < THRESHOLD] = 0
    image_t[image_t > 0] = 1
    # dilation(扩张)
    selem = morphology.disk(1)
    image_eroded = morphology.binary_dilation(image_t, selem=selem)
    label_im, nb_labels = ndimage.label(image_eroded)

    for i in range(1, nb_labels + 1):
        blob_i = np.where(label_im == i, 1, 0)
        mass = center_of_mass(blob_i)
        y_px = int(round(mass[0]))
        x_px = int(round(mass[1]))
        centers.append([y_px, x_px])
    return centers


# 数据输入网络之前先进行预处理
def prepare_image_for_net(img):
    img = img.astype(np.float)
    img /= 255.
    if len(img.shape) == 3:
        img = img.reshape(img.shape[-3], img.shape[-2], img.shape[-1])
    else:
        img = img.reshape(1, img.shape[-2], img.shape[-1], 1)
    return img


# unet模型的预测代码
def unet_predict(imagepath):
    model = get_unet()
    model.load_weights(UNET_WEIGHTS)
    # read png and ready for predict
    images = []
    img = cv2.imread(imagepath, cv2.IMREAD_GRAYSCALE)
    images.append(img)
    for index, img in enumerate(images):
        img = prepare_image_for_net(img)
        images[index] = img
    images3d = np.vstack(images)
    y_pred = model.predict(images3d, batch_size=BATCH_SIZE)
    print(len(y_pred))
    count = 0
    for y in y_pred:
        y *= 255.
        y = y.reshape((y.shape[0], y.shape[1])).astype(np.uint8)
        cv2.imwrite('./temp_dir/chapter6/unet_result.png', y)
        count += 1


# 3dcnn模型的预测代码
def _3dcnn_predict(imagepath):
    cube_image = stack_2dcube_to_3darray(imagepath, 4, 8, 32)
    img3d = prepare_image_for_net3D(cube_image, MEAN_PIXEL_VALUE)
    model = get_3dnnnet(load_weight_path='./model/3dcnn.hd5')
    result = model.predict(img3d, batch_size=BATCH_SIZE, verbose=1)
    print('3dcnn result: ', result)


if __name__ == "__main__":
    unet_predict('./data/chapter6/unet_input_img.png')
    centers = unet_candidate_dicom('./temp_dir/chapter6/unet_result.png')
    print('y, x', centers)
    _3dcnn_predict('./data/chapter6/true_positive_nodules.png')
    _3dcnn_predict('./data/chapter6/false_positive_nodules.png')
### 3D 医学图像数据预处理的方法和技术 #### 窗宽窗位调整 对于医学影像,尤其是CT和MRI扫描得到的数据,在查看特定组织结构时需要根据文件中的窗宽窗位参数进行相应调整。这一步骤能够帮助突出显示不同类型的软组织、骨骼或其他解剖特征[^1]。 #### 图像重采样 当原始采集到的三维体素间距不均匀或者与其他研究之间存在差异时,则需执行重采样的操作来统一坐标系下的分辨率。此过程可以改善后续算法性能并确保多源数据的一致性。 ```python import numpy as np from scipy.ndimage import zoom def resample_3d(image, original_spacing, target_spacing=[1., 1., 1.]): resize_factor = np.array(original_spacing) / target_spacing new_real_shape = image.shape * resize_factor new_shape = np.round(new_real_shape) real_resize_factor = new_shape / image.shape image_resampled = zoom(image, real_resize_factor, mode='nearest') return image_resampled ``` #### 去噪处理 由于成像设备本身的局限性和环境因素的影响,获取到的医疗图片往往含有不同程度上的随机干扰成分。应用诸如高斯滤波器这样的平滑技术可以在保留边缘细节的同时有效减少高频噪声污染。 ```python from skimage.filters import gaussian def denoise_image(img_volume, sigma=0.8): img_denoised = gaussian(img_volume, sigma=sigma, multichannel=False) return img_denoised ``` #### 归一化处理 为了使来自不同模态或机构之间的样本具有可比性,并加速模型收敛速度,有必要实施标准化变换使得整个批次内的像素强度分布趋于一致。 ```python def normalize_intensity(volume_data): min_val = volume_data.min() max_val = volume_data.max() normalized_vol = (volume_data - min_val) / (max_val - min_val) return normalized_vol ``` 除了上述基本步骤外,还可以利用专门设计用于生物医学领域的大规模开源工具包如MONAI来进行更复杂的任务,比如自动裁剪感兴趣区域(ROI),基于统计特性的异常检测等;亦或是借助TorchIO实现高效的批量化在线增强策略以扩充有限资源条件下所能获得的有效实例数量[^2][^3]。
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SongpingWang

你的鼓励是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值