You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CT图像胸部内轮廓提取:用于胸部畸形程度评估

胸部CT图像内轮廓提取方案

我正在开展一项胸部畸形程度评估的项目,需要从计算机断层扫描(CT)图像中提取胸部的内轮廓,但不清楚如何筛选出目标内轮廓。目前我已完成图像二值化及阈值处理,处理代码如下:

import cv2
import numpy as np
from PIL import Image

import matplotlib.pyplot as plt

def process_and_plot(image_path):
    def process(image_path):
        image = cv2.imread(image_path)
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
        _, binary_image = cv2.threshold(blurred_image, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        return binary_image

    fig, axes = plt.subplots(1, 2, figsize=(10, 5))

    img = Image.open(image_path)
    axes[0].imshow(img, cmap='gray')
    axes[0].set_title('Original Image')

    processed_image = process(image_path)
    axes[1].imshow(processed_image, cmap='gray')  
    axes[1].set_title('Processed Image')

    plt.show()

process_and_plot('test.jpeg')

当前处理输出图像:
处理后CT图像

我期望提取出如图中红色标注的内轮廓:
期望提取的胸部内轮廓


解决方案

要提取目标内轮廓,可通过轮廓检测+层次结构筛选实现,具体步骤如下:

  1. 反转二值图像:原二值化结果中目标区域为黑色,反转后转为白色,适配OpenCV轮廓检测的默认规则(检测白色区域)。
  2. 提取轮廓与层次结构:使用cv2.RETR_CCOMP模式获取轮廓,该模式能区分外层和内层轮廓,层次结构会记录每个轮廓的父轮廓信息。
  3. 筛选内轮廓:通过层次结构判断是否为内层轮廓,同时过滤掉面积过小的噪声轮廓。
  4. 绘制并展示结果:将筛选后的内轮廓绘制在原图上。

修改后的完整代码:

import cv2
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt

def process_and_plot(image_path):
    def process(image_path):
        image = cv2.imread(image_path)
        gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
        _, binary_image = cv2.threshold(blurred_image, 127, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
        # 反转二值图像,目标区域转为白色
        inverted_binary = cv2.bitwise_not(binary_image)
        return image, inverted_binary

    # 获取原图和反转后的二值图像
    original_img, inverted_binary = process(image_path)
    # 提取轮廓及层次结构
    contours, hierarchy = cv2.findContours(inverted_binary, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)
    
    # 筛选内轮廓:层次结构中[3]表示父轮廓索引,不为-1说明是内层轮廓;同时过滤小面积噪声
    inner_contours = []
    for i, cnt in enumerate(contours):
        area = cv2.contourArea(cnt)
        # 根据实际图像调整面积阈值,这里设为5000过滤小噪声
        if hierarchy[0][i][3] != -1 and area > 5000:
            inner_contours.append(cnt)
    
    # 在原图上绘制内轮廓
    result_img = original_img.copy()
    cv2.drawContours(result_img, inner_contours, -1, (0, 0, 255), 2)
    
    # 展示结果
    fig, axes = plt.subplots(1, 3, figsize=(15, 5))
    axes[0].imshow(Image.open(image_path), cmap='gray')
    axes[0].set_title('Original Image')
    
    axes[1].imshow(inverted_binary, cmap='gray')
    axes[1].set_title('Inverted Binary Image')
    
    axes[2].imshow(cv2.cvtColor(result_img, cv2.COLOR_BGR2RGB))
    axes[2].set_title('Inner Contour Extracted')
    
    plt.show()

process_and_plot('test.jpeg')

关键说明

  • cv2.RETR_CCOMP:该轮廓检索模式将轮廓分为外层、内层两个层级,层次结构数组的第4个元素(索引3)表示当前轮廓的父轮廓索引,若不为-1则说明当前是内层轮廓。
  • 面积阈值:需根据你的CT图像分辨率调整,目的是过滤肺部血管、小噪声等不需要的小轮廓。

内容的提问来源于stack exchange,提问作者david-oruna

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 14:35:16