HOW TO CONVERT PATCH INTO dicom

7 ビュー (過去 30 日間)
mohd akmal masud
mohd akmal masud 2022 年 4 月 23 日
編集済み: Walter Roberson 2024 年 1 月 30 日
Dear all,
I have coding below. How to convert p to dicom. so that my image segmentation can convert into 16uint and can view as 3D slice or 4D.
all image have attached. (except dicomCTPhantom can download here = https://drive.google.com/drive/folders/1SKuWT7G-qG9Xs_r_fs50BqZqcS4ZzUwq?usp=sharing )
clc
clear all
dataSetDir = fullfile('C:\Users\Akmal\Desktop\latest U-Nett Model 12.01.2022');
imageDir = fullfile(dataSetDir,'bwaftersegmentationpngI13125610N1untukimportdalam3DSlicer');
imds = imageDatastore(imageDir);
for i = 1:142
subplot(13,11,i)
I = readimage(imds,i);
% binary
Is{i} = logical(I);
imshow3D(I)
end
% For 3D images spect
% C:\Users\Akmal\Desktop\latest U-Nett Model 12.01.2022 is folder you located the dicomI13125610N1 images
myFolder = ('C:\Users\Akmal\Desktop\latest U-Nett Model 12.01.2022\dicomI13125610N1');
filePattern = fullfile(myFolder, '*.dcm'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for K = 1 : length(theFiles)
baseFileName = theFiles(K).name;
fullFileName = fullfile(theFiles(K).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
P(:,:,K) = dicomread(fullFileName);
P(:,:,K) = (P(:,:,K)) .* uint16( Is{K} );
% P(:,:,K) = double(uint8(K).*uint8(Is{K}));
end
% important to know the number after 0
P1 = P(:);
min(P1(P1>0))
size(P)
P = imbinarize(P);
imshow3D(P)
% Get a list of all files in the folder with the desired file name pattern.
myFolder = ('dicomCTPhantom');
filePattern = fullfile(myFolder, '*.dcm'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for L = 1 : length(theFiles)
baseFileName = theFiles(L).name;
fullFileName = fullfile(theFiles(L).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
RZ(:,:,L) = dicomread(fullFileName);
end
figure,
imshow3D(RZ)
RZ=double(RZ);
[N M L]=size(RZ);
rats=size(RZ)./size(P);
[x,y,z]=meshgrid(rats(1):rats(1):M,rats(2):rats(2):N,rats(3):rats(3):L);
RZ=interp3(RZ,x,y,z);
RZ = flip(RZ,3);
e=10000;%isovalue, air at 0 (Faceskin800, air1250)
figure
whitebg('black')
colordef none
axis off
pause
fprintf('\nFull Reconstruction. Please wait...\n');
% for e=min(min(min(RZ))):1000:max(max(max(RZ)))
% plot3(0.5,0.5,0.5);
% hold;
q = patch(isosurface(RZ,e));
axis equal
set(q,'FaceColor','w','EdgeColor','none');
alpha(q,0.2)
material shiny
hold
% q = patch(isosurface(RZ,20000));
% axis equal
% set(q,'FaceColor','g','EdgeColor','none');
% alpha(q,1)
% material shiny
camlight('headlight')
% title([num2str(e)]);
view([-22 -4])
% axis([0 M 0 N 0 L]);
% camlight('left')
% pause(0.01)
% grid
% end
p = patch(isosurface(smooth3(smooth3(P))));
axis equal
set(p,'FaceColor','c','EdgeColor','none');
alpha(p,1)
hold
I TRIED USED THIS BELOW COMMAND BUT GOT ERROR
%% this one for imwrite/dicomwrite from binary subplot(imshow3D) to single image binary
% pastikan outt22(:,:,ii) = imopen(outt2,st2); imshow(outt22(:,:,ii)) buat
% dulu kat atas
for k = 1:142
dicomwrite(p(:,:,k),sprintf('%d.dcm',k));
% imwrite(BW1(:,:,k), sprintf(['bwaftersegmentationpngI13125610N1' ...
% '%03d.png'], k)); %allBW is from gradientweight imshow3D
end
  2 件のコメント
Varun
Varun 2024 年 1 月 30 日
Hi mohd,
Looks like you're trying to use the “dicomwrite” function in MATLAB to write DICOM files from a 3D image stack. The code you provided attempts to save each 2D slice of the 3D image as a separate DICOM file using a loop.
Make sure that the intensity values in your 3D image “p(:,:,k)” are within the valid range for DICOM images. DICOM images typically have specific requirements for pixel values, such as being non-negative integers.
Alternatively, ensure that the directory where you're trying to save the DICOM files exists and you have the necessary permissions to write files in the specified directory. If it doesn't, you can create it using mkdir:
mkdir('your_directory_name');
Please refer to the code with added checks:
for k = 1:142
% Check the size of the 2D slice
disp(size(p(:,:,k)));
% Save DICOM file
dicomwrite(p(:,:,k), sprintf('%s%d.dcm', 'your_directory_name', k));
end
Replace 'your_directory_name' with the actual directory path where you want to save the DICOM files.
Please refer to the following documentation to learn more about using “dicomwrite”:
Hope it helps.
DGM
DGM 2024 年 1 月 30 日
編集済み: DGM 2024 年 1 月 30 日
First, DICOM images regularly do contain signed integer data. DICOM supports a broader range of integer datatypes than many MATLAB image processing tools do, notably uint32, int32, and int8. Attached is an example in int32.
unzip cameraman.dcm.zip % have to do this for the forum
inpict = dicomread('cameraman.dcm'); % read the file
class(inpict) % it's not even a numeric class which MATLAB associates with images
ans = 'int32'
imrange(inpict) % and it's not non-negative
ans = 1×2
1.0e+09 * -2.0296 2.1138
% surprisingly, imshow() actually works on account of getrangefromclass()
% but other tools won't (e.g. imwrite(), im2double(), etc)
imshow(inpict,'border','tight')
Second, the numeric class and range of the image doesn't really matter in this case, since the variable p (lowercase) is a handle to a patch object, not an image. Whether that was supposed to be P instead of p, I don't really know, but it's kind of a moot question now.

サインインしてコメントする。

回答 (0 件)

カテゴリ

Help Center および File ExchangeDICOM Format についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by