Load DICOM image, binarize, and save a new DICOM image

I load a DICOM Image and binarize it.
Then I save a new dicom image. However, the saved image is only black, and not black and white.
Here is the code:
for i = 1:length(d)
na=d(i).name;
TF = startsWith(na,'Tumour');
if TF == 1
info = dicominfo(na);
greyImage = dicomread(info);
BW=imbinarize(greyImage);
fname1 = strcat('bin_',na)
dicomwrite(BW, fname1,info);
else
if TF == 0
fprintf('File do not match')
end
end
end

4 Comments

How are you displaying the binarized image after saving it?
DGM
DGM on 1 May 2024
Edited: DGM on 1 May 2024
I can run this code on a carefully-selected DICOM file, and the binarized result can be read and viewed fine. Your written image is black. Without any information about your image file and how you're viewing it, we cannot know whether this is because:
  • something is going wrong with how you're viewing it
  • something is going wrong with how you're saving it
  • or something is going wrong with how you binarized it to begin with.
I'm going to guess that it's the latter; after all, imbinarize() is not magic. Unless some effort were taken, it might indeed be expected to give garbage results. Did you look at the range of your image data? A lot of DICOM images only use a tiny portion of their available dynamic range. If present, things like annotations, borders, or other decorations may present as features which lie far outside the actual dynamic range of the image data. For example, I have images where:
  • the available dynamic range is [-32768 32767]
  • the actual image data spans only [-59 101]
  • the borders and annotations span [-2048 0]
What do you think will happen when I blindly binarize that?
Again, this is just a guess.
dicomwrite(BW, fname1,info);
That asks to write a dicom image using the attributes of the original image. But the attributes of the original image are potentially unsuitable for writing an image of type logical.
Der both,
I guess it is what Sir Roberson suggests. However, if I save the binarized image without the info when I construct the MedicalVolume it says that PatientPosition is needed.
Then, how can I construct the medical Volume?

Sign in to comment.

 Accepted Answer

Two things that might help are:
  1. Convert the binarized image to type double.
  2. Redefine the minimum and maximum greyscale values in the DICOM info.
I give an example below, based on your code.
% Read and display example DICOM image.
dicom1 = 'CT-MONO2-16-ankle.dcm';
info1 = dicominfo(dicom1);
image1 = dicomread(info1);
imshow(image1, [min(image1(:)),max(image1(:))])
% Binarize image, and write to DICOM.
dicom2 = 'binarized.dcm';
bw = double(imbinarize(image1));
info1.SmallestImagePixelValue = min(bw(:));
info1.LargestImagePixelValue = max(bw(:));
dicomwrite(bw,dicom2,info1);
% Read and display binarized image.
info2 = dicominfo(dicom2);
image2 = dicomread(info2);
imshow(image2)

9 Comments

Hello,
Your code really help.
However, when I have the images binarized, the problem is that when using medicalVolume function the image is completelly white. Why does it happen?
Thank you in advance
I obtain the following warning:
Warning: The scaling from stored values to Hounsfield units resulted in values outside the int16 dynamic range. Use dicomreadVolume to get raw voxel values and manually convert to Hounsfield units.
Could you try converting the binarized image to int16, rather than double? That should at least get rid of the warning. Assuming that this doesn't fix the problem, could you post the result of dicominfo() for your image (omitting any non-anonymised patient information)?
Why would you convert to int32? The original image is (apparently) int16. Use im2int16() with a properly-scaled input image.
Either change the appropriate fields in the metadata, or rescale the image to whatever is appropriate for the reused metadata.
Sorry, typo on my part - now corrected. I agree with what you say.
Dear both, I solved the error by converting the image int16 as Karl suggested.
Although the image is -1024 for black pixels and -1023 for white pixels it works to construct a roi to extract radiomics.
Do you know any form to which the new image can be 0 or 1?
Try checking the value of info.RescaleIntercept for the info struct that you use when saving the binarized image to DICOM. My guess would be that it's -1024. If this is the case, try setting:
info.RescaleIntercept = 0;
before saving.
Try using [] in imshow:
imshow(image2, [])
It works fine!
REALLY THANK YOU FOR YOUR HELP!!!!

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!