Problem converting and displaying Dicom(ultrasonics) image for Matrix
Show older comments
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
Walter Roberson
on 29 May 2022
dcmImage = uint16(fread(fID,[dicom_data{5,5} dicom_data{6,5}],'uint16')');
Is the data stored Big Endian or Little Endian?
Khaled Al Hamidi
on 29 May 2022
Walter Roberson
on 29 May 2022
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()
Khaled Al Hamidi
on 29 May 2022
Walter Roberson
on 29 May 2022
As an experiment, try
imshow(swapbytes(dcmImage))
Khaled Al Hamidi
on 29 May 2022
Walter Roberson
on 29 May 2022
Experiment with reversing the size numbers in the fread()
Answers (1)
aditi bagora
on 10 Oct 2023
0 votes
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:
- Read metadata from DICOM message - MATLAB dicominfo - MathWorks India
- Read DICOM image - MATLAB dicomread - MathWorks India
- Accessing data in DICOM files - MATLAB & Simulink (mathworks.com)
- How to check if file is a dicom file or not
Hope this helps!
Regards,
Aditi
1 Comment
Walter Roberson
on 10 Oct 2023
The purpose of the project was to learn how to use low-level facilities to work with files.
Categories
Find more on Read and Write Image Data from Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!