.dcm格式文件软件读取及python处理详解

yipeiwu_com6年前Python基础

要处理一些.DCM格式的焊接缺陷图像,需要读取和显示.dcm格式的图像。通过搜集资料收集到一些医学影像,并通过pydicom模块查看.dcm格式文件。

若要查看dcm格式文件,可下Echo viewer 进行查看。

若用过pycharm进行处理,可选用如下的代码:

# -*-coding:utf-8-*-
import cv2
import numpy
import dicom
from matplotlib import pyplot as plt

dcm = dicom.read_file("dcm")
dcm.image = dcm.pixel_array * dcm.RescaleSlope + dcm.RescaleIntercept

slices = []
slices.append(dcm)
img = slices[int(len(slices) / 2)].image.copy()
ret, img = cv2.threshold(img, 90, 3071, cv2.THRESH_BINARY)
img = numpy.uint8(img)

im2, contours, _ = cv2.findContours(img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
mask = numpy.zeros(img.shape, numpy.uint8)
for contour in contours:
  cv2.fillPoly(mask, [contour], 255)
img[(mask > 0)] = 255

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2, 2))
img = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel)

img2 = slices[int(len(slices) / 2)].image.copy()
img2[(img == 0)] = -2000

plt.figure(figsize=(12, 12))
plt.subplot(131)
plt.imshow(slices[int(len(slices) / 2)].image, 'gray')
plt.title('Original')
plt.subplot(132)
plt.imshow(img, 'gray')
plt.title('Mask')
plt.subplot(133)
plt.imshow(img2, 'gray')
plt.title('Result')
plt.show()

也可用如下代码:

import pydicom
import os
import numpy
from matplotlib import pyplot, cm
# 用lstFilesDCM作为存放DICOM files的列表
PathDicom = "dicom/2" #与python文件同一个目录下的文件夹
lstFilesDCM = []
for dirName,subdirList,fileList in os.walk(PathDicom):
  for filename in fileList:
    if ".dcm" in filename.lower(): #判断文件是否为dicom文件
      print(filename)
      lstFilesDCM.append(os.path.join(dirName,filename)) # 加入到列表中
## 将第一张图片作为参考图
RefDs = pydicom.read_file(lstFilesDCM[0])  #读取第一张dicom图片
# 建立三维数组
ConstPixelDims = (int(RefDs.Rows),int(RefDs.Columns),len(lstFilesDCM)) # 得到spacing值 (mm为单位)
ConstPixelSpacing = (float(RefDs.PixelSpacing[0]), float(RefDs.PixelSpacing[1]), float(RefDs.SliceThickness))
# 三维数据
x = numpy.arange(0.0, (ConstPixelDims[0]+1)*ConstPixelSpacing[0], ConstPixelSpacing[0]) # 0到(第一个维数加一*像素间的间隔),步长为constpixelSpacing
y = numpy.arange(0.0, (ConstPixelDims[1]+1)*ConstPixelSpacing[1], ConstPixelSpacing[1]) #
z = numpy.arange(0.0, (ConstPixelDims[2]+1)*ConstPixelSpacing[2], ConstPixelSpacing[2]) #
ArrayDicom = numpy.zeros(ConstPixelDims, dtype=RefDs.pixel_array.dtype)
for filenameDCM in lstFilesDCM:
  ds = pydicom.read_file(filenameDCM)
  ArrayDicom[:, :, lstFilesDCM.index(filenameDCM)] = ds.pixel_array # 轴状面显示
  pyplot.figure(dpi=300)
  pyplot.axes().set_aspect('equal', 'datalim')
  pyplot.set_cmap(pyplot.gray())
  pyplot.pcolormesh(x, y, numpy.flipud(ArrayDicom[:, :, 2])) # 第三个维度表示现在展示的是第几层
  pyplot.show()

这两个代码都是可以进行读取的。但是不知道为什么在焊接检测中的dcm图像却无法进行读取。

以上这篇.dcm格式文件软件读取及python处理详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python对字典进行排序实例

本文实例讲述了python对字典进行排序的方法,是非常实用的技巧。分享给大家供大家参考。 具体实现方法如下: import itertools thekeys = ['b','a'...

Python简单获取自身外网IP的方法

本文实例讲述了Python简单获取自身外网IP的方法。分享给大家供大家参考,具体如下: #encoding=utf-8 #author: walker #date: 2016-03-...

对pandas的层次索引与取值的新方法详解

对pandas的层次索引与取值的新方法详解

1、层次索引 1.1 定义 在某一个方向拥有多个(两个及两个以上)索引级别,就叫做层次索引。 通过层次化索引,pandas能够以较低维度形式处理高纬度的数据 通过层次化索引,可以按照层次...

Python实现的多线程同步与互斥锁功能示例

Python实现的多线程同步与互斥锁功能示例

本文实例讲述了Python实现的多线程同步与互斥锁功能。分享给大家供大家参考,具体如下: #! /usr/bin/env python #coding=utf-8 import th...

Python求一批字符串的最长公共前缀算法示例

Python求一批字符串的最长公共前缀算法示例

本文实例讲述了Python求一批字符串的最长公共前缀算法。分享给大家供大家参考,具体如下: 思路一:这个题一拿到手,第一反应就是以第一个字符串strs[0]为标准,如果其他字符串的第一...