예제 #1
0
    def BuildJsonSC(self, image_array: np.ndarray, metadata_json: Dict[Text,
                                                                       Any],
                    series_uid: Text) -> dicom_json.ObjectWithBulkData:
        """Builds and returns a DICOM Secondary Capture.

    Args:
      image_array: Image array (RGB) to embed in DICOM instance.
      metadata_json: Dict of tags (including study-level information) to add.
      series_uid: UID of the series to create the SC in.

    Returns:
      DICOM JSON Object containing JSON and bulk data of the Secondary Capture.
    """
        # Copy over any study and instance level tags.
        instance_uid = self.GenerateUID()
        metadata_json = metadata_json.copy()
        dicom_json.Insert(metadata_json, tags.SOP_CLASS_UID,
                          tag_values.SECONDARY_CAPTURE_CUID)
        dicom_json.Insert(metadata_json, tags.MODALITY, tag_values.OT_MODALITY)
        dicom_json.Insert(metadata_json, tags.SERIES_INSTANCE_UID, series_uid)
        dicom_json.Insert(metadata_json, tags.SPECIFIC_CHARACTER_SET,
                          _ISO_CHARACTER_SET)
        dicom_json.Insert(metadata_json, tags.SOP_INSTANCE_UID, instance_uid)
        dicom_json.Insert(metadata_json, tags.TRANSFER_SYNTAX_UID,
                          _IMPLICIT_VR_LITTLE_ENDIAN)
        dicom_json.Insert(metadata_json, tags.MEDIA_STORAGE_SOP_CLASS_UID,
                          tag_values.SECONDARY_CAPTURE_CUID)
        dicom_json.Insert(metadata_json, tags.MEDIA_STORAGE_SOP_INSTANCE_UID,
                          instance_uid)
        # Assures URI is unique.
        study_uid = dicom_json.GetValue(metadata_json, tags.STUDY_INSTANCE_UID)
        uri = '{}/{}/{}'.format(study_uid, series_uid, instance_uid)
        metadata_json[tags.PIXEL_DATA.number] = {
            'vr': tags.PIXEL_DATA.vr,
            'BulkDataURI': uri
        }

        dicom_json.Insert(metadata_json, tags.PHOTOMETRIC_INTERPRETATION,
                          'RGB')
        dicom_json.Insert(metadata_json, tags.SAMPLES_PER_PIXEL, 3)
        # Indicates we store pixel data as R1,G1,B1,R2,G2,B2...
        dicom_json.Insert(metadata_json, tags.PLANAR_CONFIGURATION, 0)
        dicom_json.Insert(metadata_json, tags.ROWS, image_array.shape[0])
        dicom_json.Insert(metadata_json, tags.COLUMNS, image_array.shape[1])
        dicom_json.Insert(metadata_json, tags.BITS_ALLOCATED, 8)
        dicom_json.Insert(metadata_json, tags.BITS_STORED, 8)
        dicom_json.Insert(metadata_json, tags.HIGH_BIT, 7)
        dicom_json.Insert(metadata_json, tags.PIXEL_REPRESENTATION, 0)

        bulkdata = dicom_web.DicomBulkData(
            uri=uri,
            data=image_array.tobytes(),
            content_type='application/octet-stream')
        return dicom_json.ObjectWithBulkData(metadata_json, [bulkdata])
예제 #2
0
    def BuildJsonInstanceFromPng(
            self, image: bytes,
            sop_class_uid: Text) -> dicom_json.ObjectWithBulkData:
        """Builds and returns a DICOM instance from a PNG.

    This function will create a new DICOM study and series. Converts all
    incoming
    images to grayscale.

    Args:
      image: Image bytes of DICOM instance.
      sop_class_uid: UID of the SOP class for DICOM instance.

    Returns:
      DICOM JSON Object containing JSON and bulk data of the Secondary Capture.
    """
        study_uid = self.GenerateUID()
        series_uid = self.GenerateUID()
        instance_uid = self.GenerateUID()
        metadata_json = {}
        dicom_json.Insert(metadata_json, tags.PLANAR_CONFIGURATION, 0)
        # Converts colored images to grayscale.
        dicom_json.Insert(metadata_json, tags.PHOTOMETRIC_INTERPRETATION,
                          'MONOCHROME2')
        dicom_json.Insert(metadata_json, tags.SOP_CLASS_UID, sop_class_uid)
        dicom_json.Insert(metadata_json, tags.STUDY_INSTANCE_UID, study_uid)
        dicom_json.Insert(metadata_json, tags.SERIES_INSTANCE_UID, series_uid)
        dicom_json.Insert(metadata_json, tags.SPECIFIC_CHARACTER_SET,
                          _ISO_CHARACTER_SET)
        dicom_json.Insert(metadata_json, tags.SOP_INSTANCE_UID, instance_uid)
        dicom_json.Insert(metadata_json, tags.TRANSFER_SYNTAX_UID,
                          _EXPLICIT_VR_LITTLE_ENDIAN)
        dicom_json.Insert(metadata_json, tags.MEDIA_STORAGE_SOP_CLASS_UID,
                          sop_class_uid)
        dicom_json.Insert(metadata_json, tags.MEDIA_STORAGE_SOP_INSTANCE_UID,
                          instance_uid)

        # Assures URI is unique.
        uri = '{}/{}/{}'.format(study_uid, series_uid, instance_uid)
        metadata_json[tags.PIXEL_DATA.number] = {
            'vr': tags.PIXEL_DATA.vr,
            'BulkDataURI': uri
        }

        bulkdata = dicom_web.DicomBulkData(
            uri=uri, data=image, content_type='image/png; transfer-syntax=""')
        return dicom_json.ObjectWithBulkData(metadata_json, [bulkdata])
예제 #3
0
    def BuildJsonSR(
            self, report_text: Text,
            metadata_json: Dict[Text, Any]) -> dicom_json.ObjectWithBulkData:
        """Builds and returns a Basic Text DICOM JSON Structured Report instance.

    This function will create a new DICOM series.

    Args:
      report_text: Text string to use for the Basic Text DICOM SR.
      metadata_json: Dict of tags (including study-level information) to add.

    Returns:
      DICOM JSON Object containing the Structured Report.
    """
        # Dicom StowJsonRs expects a list with DICOM JSON as elements.
        # Add study level tags to the SR.
        dataset = metadata_json.copy()
        series_uid = self.GenerateUID()
        instance_uid = self.GenerateUID()
        dicom_json.Insert(dataset, tags.SOP_CLASS_UID,
                          tag_values.BASIC_TEXT_SR_CUID)
        dicom_json.Insert(dataset, tags.MODALITY, tag_values.SR_MODALITY)
        dicom_json.Insert(dataset, tags.SERIES_INSTANCE_UID, series_uid)
        dicom_json.Insert(dataset, tags.SPECIFIC_CHARACTER_SET,
                          _ISO_CHARACTER_SET)

        logging.log(
            logging.INFO,
            'Creating DICOM JSON SR with Series UID: %s and Instance UID: %s',
            series_uid, instance_uid)
        dicom_json.Insert(dataset, tags.SOP_INSTANCE_UID, instance_uid)

        content_dataset = {}
        dicom_json.Insert(content_dataset, tags.RELATIONSHIP_TYPE, 'CONTAINS')
        dicom_json.Insert(content_dataset, tags.VALUE_TYPE, 'TEXT')
        dicom_json.Insert(content_dataset, tags.TEXT_VALUE, report_text)
        dicom_json.Insert(dataset, tags.CONTENT_SEQUENCE, content_dataset)

        dicom_json.Insert(dataset, tags.TRANSFER_SYNTAX_UID,
                          _IMPLICIT_VR_LITTLE_ENDIAN)
        dicom_json.Insert(dataset, tags.MEDIA_STORAGE_SOP_CLASS_UID,
                          tag_values.BASIC_TEXT_SR_CUID)
        dicom_json.Insert(dataset, tags.MEDIA_STORAGE_SOP_INSTANCE_UID,
                          instance_uid)

        return dicom_json.ObjectWithBulkData(dataset)
예제 #4
0
  def testObjectWithBulkData(self):
    """Tests methods of ObjectWithBulkData class."""
    # Construct dicom_json.ObjectWithBulkData object
    dicom_dict = dict()
    instance_uid = 'instance_uid'
    study_uid = 'study_uid'
    series_uid = 'series_uid'
    dicom_json.Insert(dicom_dict, tags.SOP_INSTANCE_UID, instance_uid)
    dicom_json.Insert(dicom_dict, tags.STUDY_INSTANCE_UID, study_uid)
    dicom_json.Insert(dicom_dict, tags.SERIES_INSTANCE_UID, series_uid)
    bulkdata = dicom_web.DicomBulkData(
        uri='uri',
        data=bytearray('image_array', encoding='utf8'),
        content_type='type')
    bulkdata_list = [bulkdata]
    dicom_object = dicom_json.ObjectWithBulkData(dicom_dict, bulkdata_list)

    # Test properties
    self.assertEqual(dicom_object.instance_uid, instance_uid)
    self.assertEqual(dicom_object.series_uid, series_uid)
    self.assertEqual(dicom_object.study_uid, study_uid)