I am trying to create a new dicom file from sratch using pydicom. I have built a file with help from the following links from stackoverflow and the pydicom github examples.THis is my function:
def generate_dicom(pixel_array,filename):
sop_uid = pydicom.uid.generate_uid()
sop_class_uid = pydicom._storage_sopclass_uids.ComputedRadiographyImageStorage
#create metadata
file_meta = Dataset()
file_meta.MediaStorageSOPClassUID = sop_class_uid
file_meta.MediaStorageSOPInstanceUID = sop_uid
file_meta.ImplementationClassUID = '1.2.276.0.7230010.3.0.3.6.2'
file_meta.FileMetaInformationGroupLength = 218
file_meta.FileMetaInformationVersion = b'\x00\x01'
file_meta.ImplementationVersionName = 'OFFIS_DCMTK_362'
file_meta.SourcApplicationEntitTitle = 'AZMED'
ds = FileDataset(filename, {},file_meta = file_meta,preamble=b"\x00" * 128)
# Set the transfer syntax implicitly, if not set explicitly above
ds.is_little_endian = True
ds.is_implicit_VR = True
#change modality
ds.Modality = 'CR'
# Set creation date/time
dt = datetime.datetime.now()
ds.ContentDate = dt.strftime('%Y%m%d')
timeStr = dt.strftime('%H%M%S.%f') # long format with micro seconds
ds.ContentTime = timeStr
ds.StudyInstanceUID = pydicom.uid.generate_uid()
ds.SeriesInstanceUID = pydicom.uid.generate_uid()
ds.SOPInstanceUID = sop_uid
ds.SOPClassUID = sop_class_uid
ds.SecondaryCaptureDeviceManufctur = 'AZMed_SAS'
ds.FrameOfReferenceUID = pydicom.uid.generate_uid()
# set patient name and ID
ds.PatientName = "Test^Firstname"
ds.PatientID = "123456"
## These are the necessary imaging components of the FileDataset object.
# we create 16 bit pixel arrays here
ds.SamplesPerPixel = 1
ds.PhotometricInterpretation = "MONOCHROME2"
ds.PixelRepresentation = 0
ds.HighBit = 15
ds.BitsStored = 16
ds.BitsAllocated = 16
#ds.SmallestImagePixelValue = b"\x00\x00"
#ds.LargestImagePixelValue = b"\xff\xff"
ds.Columns = pixel_array.shape[0]
ds.Rows = pixel_array.shape[1]
if pixel_array.dtype != np.uint16:
pixel_array = pixel_array.astype(np.uint16)
ds.PixelData = pixel_array.tostring()
ds.ImagesInAcquisition = "1"
# reutrn the dataset of dicom
return ds
For this function I pass an image using cv2:
new_pixel_array = cv2.imread('/home/toing/image_01.png', -1)
When I pass new_pixel_array
with shape (1500, 200)
, with 12 bit values and type uint16
to my function, I get a very blurry image when I try to read the generated dicom.
ds = generate_dicom(new_pixel_array, 'toing.dcm')
ds.pixel_array
x = np.arange(16).reshape(16,1)
new_pixel_array = (x + x.T) * 32
new_pixel_array = np.tile(new_pixel_array,(16,16))
As suggested in this answer, I get the desired tiled image shown correctly.
I also tried converting new_pixel_array to 8 bit values before passing to my function, but I still get the same result. On the other hand the generated tiled image works fine when converted. What am I doing wrong here?
It seems to me that the dimensions are set incorrectly.
If I understand you correctly your image should be (1500, 200)
i.e. 1500px as height and 200px as width.
ds.Columns = pixel_array.shape[0] # 1500
ds.Rows = pixel_array.shape[1] # 200
In the snippet you provided you set the width or columns to the first value of your shape.
Try changing those values