當前位置: 首頁>>代碼示例>>Python>>正文


Python pydicom.dcmread方法代碼示例

本文整理匯總了Python中pydicom.dcmread方法的典型用法代碼示例。如果您正苦於以下問題:Python pydicom.dcmread方法的具體用法?Python pydicom.dcmread怎麽用?Python pydicom.dcmread使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在pydicom的用法示例。


在下文中一共展示了pydicom.dcmread方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: is_dicom_image

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def is_dicom_image(file: str) -> bool:
    """Boolean specifying if file is a proper DICOM file with a image

    Parameters
    ----------
    file : str
        The path to the file.

    See Also
    --------
    pydicom.filereader.read_preamble
    pydicom.filereader.read_partial
    """
    result = False
    try:
        img = pydicom.dcmread(file, force=True)
        if 'TransferSyntaxUID' not in img.file_meta:
            img.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian
        img.pixel_array
        result = True
    except (AttributeError, TypeError, KeyError, struct.error):
        pass
    return result 
開發者ID:jrkerns,項目名稱:pylinac,代碼行數:25,代碼來源:io.py

示例2: is_dicomfile

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def is_dicomfile(file: Path) -> bool:
    """
    Checks whether a file is a DICOM-file. It uses the feature that Dicoms have the string DICM hardcoded at offset 0x80.

    :param file:    The full pathname of the file
    :return:        Returns true if a file is a DICOM-file
    """

    if file.is_file():
        if file.stem.startswith('.'):
            logger.warning(f'File is hidden: {file}')
        with file.open('rb') as dcmfile:
            dcmfile.seek(0x80, 1)
            if dcmfile.read(4) == b'DICM':
                return True
            else:
                dicomdict = pydicom.dcmread(str(file), force=True)       # The DICM tag may be missing for anonymized DICOM files
                return 'Modality' in dicomdict
    else:
        return False 
開發者ID:Donders-Institute,項目名稱:bidscoin,代碼行數:22,代碼來源:bids.py

示例3: inspect_sourcefile

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def inspect_sourcefile(self, item):
        """When double clicked, show popup window. """
        if item.column() == 1:
            row  = item.row()
            cell = self.samples_table.item(row, 5)
            sourcefile = Path(cell.text())
            if bids.is_dicomfile(sourcefile):
                sourcedata = pydicom.dcmread(str(sourcefile), force=True)
            elif bids.is_parfile(sourcefile):
                with open(sourcefile, 'r') as parfid:
                    sourcedata = parfid.read()
            else:
                LOGGER.warning(f"Could not read {self.dataformat} file: {sourcefile}")
                return
            self.popup = InspectWindow(sourcefile, sourcedata, self.dataformat)
            self.popup.show()
            self.popup.scrollbar.setValue(0)     # This can only be done after self.popup.show() 
開發者ID:Donders-Institute,項目名稱:bidscoin,代碼行數:19,代碼來源:bidseditor.py

示例4: assign2machine

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def assign2machine(source_file: str, machine_file: str):
    """Assign a DICOM RT Plan file to a specific machine. The source file is overwritten to contain
    the machine of the machine file.

    Parameters
    ----------
    source_file : str
        Path to the DICOM RTPlan file that contains the fields/plan desired
        (e.g. a Winston Lutz set of fields or Varian's default PF files).
    machine_file : str
        Path to a DICOM RTPlan file that has the desired machine. This is easily obtained from pushing a plan from the TPS
        for that specific machine. The file must contain at least one valid field.
    """
    dcm_source = pydicom.dcmread(source_file)
    dcm_machine = pydicom.dcmread(machine_file)
    for beam in dcm_source.BeamSequence:
        beam.TreatmentMachineName = dcm_machine.BeamSequence[0].TreatmentMachineName
    dcm_source.save_as(source_file) 
開發者ID:jrkerns,項目名稱:pylinac,代碼行數:20,代碼來源:utilities.py

示例5: read_image_dicom

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def read_image_dicom(self,path,mode='image'):
        """ Read an image in dicom format.
        Args
            path: Path to the image.
            mode: image|image_sex_view
        """
        dicom_img = pydicom.dcmread(path)
        image = dicom_img.pixel_array
        image = np.stack((image,)*3, -1) #convert grayscale to rgb
        if mode=='image_sex_view':
            if dicom_img.PatientSex == 'F':
                image[:,:,1] = 0
            elif dicom_img.PatientSex == 'M':
                image[:,:,1] = 1
            else:
                raise Exception('Invalid Sex on dicom {}.'.format(path))
            if dicom_img.ViewPosition == 'AP':
                image[:,:,2] = 0
            elif dicom_img.ViewPosition == 'PA':
                image[:,:,2] = 1
            else:
                raise Exception('Invalid View Position on dicom {}. View position is: {}'.format(path,dicom_img.ViewPosition))
        return image[:, :].copy() 
開發者ID:alessonscap,項目名稱:rsna-challenge-2018,代碼行數:25,代碼來源:rsna_generator_mask.py

示例6: imread

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def imread(self, path, grayscale=False, size=None, interpolate="bilinear",
               channel_first=False, as_uint16=False, num_channels=-1, return_palette_indices=False):
        """
        Read image by DICOM module.
        Notice that PIL only supports uint8 for RGB (not uint16).
        So this imread function returns only uint8 array for both RGB and gray-scale.
        (Currently ignore "I" mode for gray-scale (32bit integer).)

        Args:
            path (str or 'file object'): File path or object to read.
            grayscale (bool):
            size (tupple of int):
                (width, height).
                If None, output img shape depends on the files to read.
            channel_first (bool):
                This argument specifies the shape of img is whether (height, width, channel) or (channel, height, width).
                Default value is False, which means the img shape is (height, width, channel).
            interpolate (str):
                must be one of ["nearest", "box", "bilinear", "hamming", "bicubic", "lanczos"].
            as_uint16 (bool):
                If you specify this argument, you can use only False for pil backend.
            num_channels (int):
                channel size of output array.
                Default is -1 which preserves raw image shape.
            return_palette_indices (bool):
                Whether to return a raw palette indices without any conversion or not.
                If this flag is True and read Image has the mode "P",
                then this function returns 2-D array containing the indices into palette.
                We recommend that this flag should be False unless you intend to use the raw palette indices.

        Returns:
            numpy.ndarray
        """
        _imread_before(grayscale, num_channels)
        dicom_dataset = pydicom.dcmread(path)
        img = _apply_gamma_correction(dicom_dataset)
        return _imread_after(img, size, interpolate, channel_first, self.imresize) 
開發者ID:sony,項目名稱:nnabla,代碼行數:39,代碼來源:dicom_backend.py

示例7: on_double_clicked

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def on_double_clicked(self, index: int):
        """Opens the inspect window when a source file in the file-tree tab is double-clicked"""
        sourcefile = Path(self.model.fileInfo(index).absoluteFilePath())
        if bids.is_dicomfile(sourcefile):
            sourcedata = pydicom.dcmread(str(sourcefile), force=True)
        elif bids.is_parfile(sourcefile):
            with open(sourcefile, 'r') as parfid:
                sourcedata = parfid.read()
        else:
            LOGGER.warning(f"Could not read {self.dataformat} file: {sourcefile}")
            return
        self.popup = InspectWindow(sourcefile, sourcedata, self.dataformat)
        self.popup.show()
        self.popup.scrollbar.setValue(0)  # This can only be done after self.popup.show() 
開發者ID:Donders-Institute,項目名稱:bidscoin,代碼行數:16,代碼來源:bidseditor.py

示例8: load_vol

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def load_vol(self, path):
        """
            path : patient data path

            returns numpy array of patient data
        """
        self.patient = pydicom.dcmread(path)

        return self.patient.pixel_array 
開發者ID:koriavinash1,項目名稱:DeepBrainSeg,代碼行數:11,代碼來源:dicom.py

示例9: is_dicom

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def is_dicom(path):
    """Whether the file is a readable DICOM file via pydicom."""
    try:
        ds = pydicom.dcmread(path, force=True)
        ds.pixel_array
        return True
    except:
        return False 
開發者ID:jrkerns,項目名稱:pylinac,代碼行數:10,代碼來源:tools.py

示例10: retrieve_dicom_file

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def retrieve_dicom_file(file: str) -> pydicom.FileDataset:
    """Read and return the DICOM dataset.

    Parameters
    ----------
    file : str
        The path to the file.
    """
    img = pydicom.dcmread(file, force=True)
    if 'TransferSyntaxUID' not in img.file_meta:
        img.file_meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian
    return img 
開發者ID:jrkerns,項目名稱:pylinac,代碼行數:14,代碼來源:io.py

示例11: is_CT_slice

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def is_CT_slice(file: str) -> bool:
        """Test if the file is a CT Image storage DICOM file."""
        try:
            ds = pydicom.dcmread(file, force=True, stop_before_pixels=True)
            return ds.SOPClassUID.name == 'CT Image Storage'
        except (InvalidDicomError, AttributeError, MemoryError):
            return False 
開發者ID:jrkerns,項目名稱:pylinac,代碼行數:9,代碼來源:image.py

示例12: read_image_dicom

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def read_image_dicom(self,path,mode='image'):
        """ Read an image in dicom format.
        Args
            path: Path to the image.
            mode: image|image_sex_view
        """
        dicom_img = pydicom.dcmread(path)
        image = dicom_img.pixel_array
        #convert grayscale to rgb
        image = np.stack((image,)*3, -1) 
        if mode=='image_sex_view':
            #split image in patient sex
            if dicom_img.PatientSex == 'F':
                image[:,:,1] = 0
            elif dicom_img.PatientSex == 'M':
                image[:,:,1] = 1
            else:
                raise Exception('Invalid Sex on dicom {}.'.format(path))
            #split image in view position
            if dicom_img.ViewPosition == 'AP':
                image[:,:,2] = 0
            elif dicom_img.ViewPosition == 'PA':
                image[:,:,2] = 1
            else:
                raise Exception('Invalid View Position on dicom {}. View position is: {}'.format(path,dicom_img.ViewPosition))
        return image[:, :].copy() 
開發者ID:alessonscap,項目名稱:rsna-challenge-2018,代碼行數:28,代碼來源:rsna_generator.py

示例13: _store_instances

# 需要導入模塊: import pydicom [as 別名]
# 或者: from pydicom import dcmread [as 別名]
def _store_instances(client, args):
    '''Loads Instances from files on disk and stores them.'''
    datasets = [pydicom.dcmread(f) for f in args.files]
    client.store_instances(datasets) 
開發者ID:MGHComputationalPathology,項目名稱:dicomweb-client,代碼行數:6,代碼來源:cli.py


注:本文中的pydicom.dcmread方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。