Problem converting and displaying Dicom(ultrasonics) image for Matrix

I have a project to read a DICOM file and determine if it is a DICOM file or not. Then read the content of the file and convert it to Matrix and then display it as an image. But unfortunately the picture always appears like this for me
But when I view the image in another program, it appears in its true form
elseif strcmp(Group,'7FE0')
if strcmp(Element,'0010')
if dicom_data{7,5}==16
dcmImage = uint16(fread(fID,[dicom_data{5,5} dicom_data{6,5}],'uint16')');
dicom_data{10,5} = dcmImage;
else
dcmImage = uint16(fread(fID,[dicom_data{5,5} dicom_data{6,5}],'uint8')');
dicom_data{10,5} = dcmImage;
end
else
fseek(fID,ValueLength,0);
end
else
if(ftell(fID)==DateiEnde)
break
end
fseek(fID,ValueLength,0);
end
end
imshow(dicom_data,[0 2^dicom_data{9,5}])
This is the data it reads from the DICOM file
ans =
10×5 cell array
{'TransferSyntax' } {'2' } {'10' } {'UI'} {'1.2.840.10008.1…'}
{'PatientName' } {'10' } {'10' } {'PN'} {'no patient' }
{'SamplesPerPixel'} {'28' } {'2' } {'US'} {[ 3]}
{'NumberOfFrames' } {'28' } {'8' } {'IS'} { 2×1 char }
{'Rows' } {'28' } {'10' } {'US'} {[ 512]}
{'Cols' } {'28' } {'11' } {'US'} {[ 512]}
{'BitsAllocated' } {'28' } {'100'} {'US'} {[ 8]}
{'BitsStored' } {'28' } {'101'} {'US'} {[ 8]}
{'HighBit' } {'28' } {'102'} {'US'} {[ 7]}
{'PixelData' } {'7FE0'} {'10' } {'OB'} {512×512 uint16 }

7 Comments

dcmImage = uint16(fread(fID,[dicom_data{5,5} dicom_data{6,5}],'uint16')');
Is the data stored Big Endian or Little Endian?
The default for fread is "native", and on all systems currently supported, that is Little Endian. In order to get Big Endian you need to specify it at the time you fopen() or the time you fread()
Thank you
I found the problem related to the size of the array, I changed it to another size and the problem was solved
from:
dcmImage = uint8(fread(fID,[dicom_data{5,5} dicom_data{6,5}],'uint8')');
to:
dcmImage = uint8(fread(fID,[1536 512],'uint8')');
but I got a non-rectangular image
Experiment with reversing the size numbers in the fread()

Sign in to comment.

Answers (1)

Hello Khaled,
I understand you want to read a DICOM (ultrasonics) file and extract the values from its metadata. It seems that you are currently using the "fread()" and "fseek()" functions to read the binary file. However, you are encountering issues when reading the pixel data, which could be due to a mismatch in reading the binary data in a particular format or inconsistent data in the file.
To overcome these issues, you can manually try reading the binary file using "fread()" and "fseek()", you can try using the "dicomread()" and "dicominfo()" functions. These functions are specifically designed to read pixel data and metadata from DICOM files, ensuring compatibility and accurate extraction.
To read the pixel data and dicom data, you can refer to the following example code:
pixel_data = dicomread(filename); % read pixel data
imshow(pixel_data);
dicom_data = dicominfo(filename); % To read the DICOM metadata.
Please refer to the following links for more information:
Hope this helps!
Regards,
Aditi

1 Comment

The purpose of the project was to learn how to use low-level facilities to work with files.

Sign in to comment.

Products

Release

R2020b

Asked:

on 29 May 2022

Commented:

on 10 Oct 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!