illan
illan

Reputation: 375

Adding pixelData to pydicom dataset causes VR Error

I am working with a set of dicom images. I would like to create a new image with a header similar to an existing image. However, I already propagate the images in numpy arrays, so to avoid duplication I propagate the headers without PixelData:

metadata = pydicom.filereader.dcmread(image_path[l],stop_before_pixels=True)

In a separate function, I want to attach a different image (an ROI) to the modified metadata:

ds = metadata
ds.PixelData = roi.astype(np.int16).tostring() # A numpy array converted to the same datatype as pixel_array was
ds.save_as(os.path.join(write_dir,'ROI'+str(slice+1))+'.dcm')

This results in an error message below which seems to indicate that the PixelData VR is not set in the dictionary? Thanks for your suggestions.

ValueError: Cannot write ambiguous VR of 'OB or OW' for data element with tag (7fe0, 0010). Set the correct VR before writing, or use an implicit VR transfer syntax

Upvotes: 2

Views: 1571

Answers (1)

scaramallion
scaramallion

Reputation: 1330

The VR for Pixel Data is ambiguous in the DICOM Standard. Depending on the exact nature of your dataset the required VR is either OB or OW. Because you're adding a brand new Pixel Data element to an existing dataset pydicom defaults the VR to 'OB or OW'. Normally this isn't an issue if your dataset is conformant because during write pydicom will automatically fix this so the correct VR is used (using the correct_ambiguous_vr() function). If your dataset isn't conformant then:

  • If your Pixel Data uses a compressed transfer syntax, like JPEG, then it should be OB.
  • Otherwise, it should be OB if Bits Allocated <= 8 and OW if > 8.
# Set the VR manually
ds['PixelData'].VR = 'OW'

Upvotes: 6

Related Questions