You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lung_segmentation.py 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. # !/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. """
  4. Copyright 2020 Tianshu AI Platform. All Rights Reserved.
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================
  15. """
  16. import sched
  17. import sys
  18. import time
  19. import json
  20. import numpy as np
  21. from abc import ABC
  22. from program.abstract.algorithm import Algorithm
  23. from skimage.morphology import disk, binary_erosion, binary_closing
  24. from skimage.measure import label, regionprops, find_contours
  25. from skimage.filters import roberts
  26. from scipy import ndimage as ndi
  27. from skimage.segmentation import clear_border
  28. import pydicom as dicom
  29. import os
  30. import logging
  31. schedule = sched.scheduler(time.time, time.sleep)
  32. base_path = "/nfs/"
  33. delayId = ""
  34. class Lungsegmentation(Algorithm, ABC):
  35. def __init__(self):
  36. pass
  37. def execute(task):
  38. return Lungsegmentation.process(task)
  39. def process(task_dict):
  40. """Lung segmentation based on dcm task method.
  41. Args:
  42. task_dict: imagenet task details.
  43. key: imagenet task key.
  44. """
  45. global delayId
  46. base_path = task_dict["annotationPath"]
  47. if not os.path.exists(base_path):
  48. logging.info("make annotation path.")
  49. os.makedirs(base_path)
  50. for dcm in task_dict["dcms"]:
  51. image, image_path = Lungsegmentation.preprocesss_dcm_image(dcm)
  52. # segmentation and wirte coutours to result_path
  53. result_path = os.path.join(base_path, image_path)
  54. Lungsegmentation.contour(Lungsegmentation.segmentation(image), result_path)
  55. logging.info("all dcms in one task are processed.")
  56. finish_data = {"reTaskId": task_dict["reTaskId"]}
  57. return finish_data, True
  58. def preprocesss_dcm_image(path):
  59. """Load and preprocesss dcm image.
  60. Args:
  61. path: dcm file path.
  62. """
  63. # result_path = os.path.basename(path).split(".", 1)[0] + ".json"
  64. result_path = ".".join(os.path.basename(path).split(".")[0:-1]) + ".json"
  65. dcm = dicom.dcmread(path)
  66. image = dcm.pixel_array.astype(np.int16)
  67. # Set outside-of-scan pixels to 0.
  68. image[image == -2000] = 0
  69. # Convert to Hounsfield units (HU)
  70. intercept = dcm.RescaleIntercept
  71. slope = dcm.RescaleSlope
  72. if slope != 1:
  73. image = slope * image.astype(np.float64)
  74. image = image.astype(np.int16)
  75. image += np.int16(intercept)
  76. logging.info("preprocesss_dcm_image done.")
  77. return np.array(image, dtype=np.int16), result_path
  78. def segmentation(image):
  79. """Segments the lung from the given 2D slice.
  80. Args:
  81. image: single image in one dcm.
  82. """
  83. # Step 1: Convert into a binary image.
  84. binary = image < -350
  85. # Step 2: Remove the blobs connected to the border of the image.
  86. cleared = clear_border(binary)
  87. # Step 3: Label the image.
  88. label_image = label(cleared)
  89. # Step 4: Keep the labels with 2 largest areas.
  90. areas = [r.area for r in regionprops(label_image)]
  91. areas.sort()
  92. if len(areas) > 2:
  93. for region in regionprops(label_image):
  94. if region.area < areas[-2]:
  95. for coordinates in region.coords:
  96. label_image[coordinates[0], coordinates[1]] = 0
  97. binary = label_image > 0
  98. # Step 5: Erosion operation with a disk of radius 2. This operation is seperate the lung nodules attached to the blood vessels.
  99. selem = disk(1)
  100. binary = binary_erosion(binary, selem)
  101. # Step 6: Closure operation with a disk of radius 10. This operation is to keep nodules attached to the lung wall.
  102. selem = disk(16)
  103. binary = binary_closing(binary, selem)
  104. # Step 7: Fill in the small holes inside the binary mask of lungs.
  105. for _ in range(3):
  106. edges = roberts(binary)
  107. binary = ndi.binary_fill_holes(edges)
  108. logging.info("lung segmentation done.")
  109. return binary
  110. def contour(image, path):
  111. """Get contours of segmentation.
  112. Args:
  113. seg: segmentation of lung.
  114. """
  115. result = []
  116. contours = find_contours(image, 0.5)
  117. if len(contours) > 2:
  118. contours.sort(key=lambda x: int(x.shape[0]))
  119. contours = contours[-2:]
  120. for n, contour in enumerate(contours):
  121. # result.append({"type":n, "annotation":contour.tolist()})
  122. result.append({"type": n, "annotation": np.flip(contour, 1).tolist()})
  123. # write json
  124. with open(path, 'w') as f:
  125. json.dump(result, f)
  126. logging.info("write {} done.".format(path))

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能