Radi_tech’s blog

Radiological technologist in Japan / MRI / AI / Deep learning / MATLAB / R / Python

【MATLAB】How to save an image such as JPEG) generated by GAN or other image creation with DICOM information.

f:id:radi_tech:20211017151528p:plain
The generated images are intended to be used in work stations and other software.

Procedure

  • Prepare a file with DICOM information to be added
  • Obtain DICOM metadata.
  • Prepare JPEG file.
  • Convert JPEG to gray scale or single (>> This importan.)
  • Save the file with DICOM metadata.

The key point is the conversion to gray scale or single, because DICOM is a two-dimensional image without color information, while JPEG is a three-dimensional RGB image.
DICOM is a 2D image without color information, whereas JPEG is a 3D RGB image.

If you import the image as RGB, it will be treated as an image (similar to a captured image) which cannot be handled by work station.

It is also necessary to set up a UID so that the DICOM metadata is not confused with the source image.

% pathを指定してDICOMメタデータを取得
dcm_info=dicominfo(dcm_path));

%コピーもと画像と混同しないようにUIDを立てておく
%"dicomuid"は新たにUIDを作成する呪文
uid_1 = dicomuid;
uid_2 = dicomuid;

%メタデータを一部書き換え
%studyとseriesを新たにする
dcm_info.StudyInstanceUID =uid_1;
dcm_info.SeriesInstanceUID=uid_2;


%pathを指定してJPEGを取得
jpg_img =imread(jpg_path);

% gray scaleへ変換する
jpg_img =rgb2gray(jpg_img); 

%saveするpathを指定して(今はsave_pathとする)、dicom_infoを保存
dicomwrite(jpg_img,[],save_path,dcm_info);  

When incorporated into a for statement, the entire series of images can be processed at once.

In this case, the generation of UIDs should be done outside the for statement.

Otherwise, all the images will be inspected separately.

You can create arbitrarily adjusted DICOM images by incorporating imresize and window level settings in the process.