. DCM format file software reading and python processing


In order to process some welding defect images in. DCM format, it is necessary to read and display the images in. DCM format. Some medical images were collected by collecting data, and the. DCM file was viewed through pydicom module.

To view the DCM format file, click echo viewer to view it.

If pychar is used for processing, the following code can be selected:

# -*-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 = []
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.imshow(slices[int(len(slices) / 2)].image, 'gray')
plt.imshow(img, 'gray')
plt.imshow(img2, 'gray')

The following codes are also available:

import pydicom
import os
import numpy
from matplotlib import pyplot, cm
#Using lstfiles DCM as the list of DICOM files
Pathdicom = "DICOM / 2" ා a folder in the same directory as the python file
lstFilesDCM = []
for dirName,subdirList,fileList in os.walk(PathDicom):
  for filename in fileList:
    if ".dcm" in  filename.lower (): ා judge whether the file is a DICOM file
      lstFilesDCM.append ( os.path.join (dirname, file name)) # added to the list
##Use the first picture as a reference
RefDs =  pydicom.read_ File (lstfiles DCM [0]) to read the first DICOM image
#Building a 3D array
ConstPixelDims = (int( RefDs.Rows ),int( RefDs.Columns ), len (lstfiles DCM)) ා get spacing value (mm)
ConstPixelSpacing = (float(RefDs.PixelSpacing[0]), float(RefDs.PixelSpacing[1]), float(RefDs.SliceThickness))
#3D data
x =  numpy.arange (0.0, (constpixeldims [0] + 1) * constpixelspacing [0], constpixelspating [0]) # 0 to (the first dimension plus one * the interval between pixels), in steps of 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 ා axial surface display
  pyplot.axes().set_aspect('equal', 'datalim')
  pyplot.pcolormesh (x, y,  numpy.flipud (arraydicom [:,:, 2])) ා the third dimension indicates which layer is displayed now

Both codes are readable. But we don’t know why the DCM image in welding inspection can’t be read.

The above. DCM format file software reading and python processing is the small editor to share all the content, I hope to give you a reference, also hope that you can support developeppaer.