MATLAB Answers

How to paste the coordinate of value 1 binary image into the 3D dicom.

2 views (last 30 days)
Hi all, I have one set of 3D images (dicom format).
Then after did the some method segmentation, it became binary but in 2D images.
anyone know how to paste the coordinate of value 1 binary image into 3D images(dicom format)?
so that the value 1 is foreground, and value 0 is background?
so at the bottom in my coding, just I click at volume that I want, the volume will calculated, and the sum number pixel count also calculated.
My problem is how to mark the coordinate value 1 binary images to the 3D images. so that only voxel on 3D images that have same coordinate with value 1 in binary images will appear.
please anyone can help me?
clear c count init
% for 3D images (dicom format)
clc
clear all
myFolder = ('C:\Users\Akmal\Desktop\I-131 256 28.02.2020\I-131 SPECT NEMA VALIDATION 01112019 256X256 26.09.2021 petang\dcmoriextract');
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);
end
% For binary images (png format, each pixel just have value 1 and 0.)
dataSetDir = fullfile('C:\Users\Akmal\Desktop\I-131 256 28.02.2020\I-131 SPECT NEMA VALIDATION 01112019 256X256 26.09.2021 petang');
imageDir = fullfile(dataSetDir,'bnwaftersegmentation');
imds = imageDatastore(imageDir);
for i = 1:41
% subplot(6,7,i)
I = readimage(imds,i);
end
PVmax = I==0;
PVmin = I==1;
RZ01=P;
RZ01(RZ01<PVmin)=0;
RZ01(RZ01>PVmax)=0;
RZ01(RZ01~=0)=1;
e=10000;%isovalue, air at 0 (Faceskin800, air1250)
clf
whitebg('black')
colordef none
axis off
fprintf('\nFull Reconstruction. Please wait...\n');
camlight('headlight')
% title([num2str(e)]);
view([-22 -4])
p = patch(isosurface(smooth3(P(PVmin ))));
axis equal
set(p,'FaceColor','c','EdgeColor','none');
alpha(p,1)
hold
% assuming that your dicom images and binary images have same page geometry
numframes = 41;
s = [size(P,1) size(P,2)];
maskstack = zeros(s(1),s(2),numframes);
for f = 1:numframes
if f == 1
cast(maskstack,class(readimage(imds,f)));
end
maskstack(:,:,f) = readimage(imds,f);
end
% build location arrays
[X Y Z] = ndgrid(1:s(1),1:s(2),0:2.332:(numframes-1)*2.332);
clc
disp(['-At this point, you can use all the figure tools to zoom, pan'])
disp([' and rotate your object as you wish.'])
disp([' '])
disp(['-To eliminate unnecessary object, please use data cursor tool '])
disp([' to select a point of ROI.'])
disp(['-Data tip of X, Y and Z appeared, right click and select '])
disp([' ''Export Cursor Data to Workspace''.'])
disp(['-Click OK on the pop-up window then click Enter on command '])
disp([' window. '])
ROI=1;
for a=1:ROI
a;
pause
init(a,1:3)=round([cursor_info.Position(2) cursor_info.Position(1) cursor_info.Position(3)]);
end
%init=[round(38/75*ymax) round(36/71*xmax) round(80/200*zmax)] ;%[y x]
[points dim]=size(init);
count=1;
disp([' ']);
disp(['-Wait until volume marching in Figure 1 is finished. '])
% disp(['Exit loop by clicking on the figure and then press Enter.'])
while count<=points
if 1<init(count,1) && init(count,1)<Y && 1<init(count,2) ...
&& init(count,2)<X && 1<init(count,3) && init(count,3)<Z
if RZ01(init(count,1)+1,init(count,2),init(count,3))==1
init(end+1,1)=init(count,1)+1;
init(end,2)=init(count,2);
init(end,3)=init(count,3);
RZ01(init(count,1)+1,init(count,2),init(count,3))=2;
end
if RZ01(init(count,1)-1,init(count,2),init(count,3))==1
init(end+1,1)=init(count,1)-1;
init(end,2)=init(count,2);
init(end,3)=init(count,3);
RZ01(init(count,1)-1,init(count,2),init(count,3))=2;
end
if RZ01(init(count,1),init(count,2)+1,init(count,3))==1
init(end+1,1)=init(count,1);
init(end,2)=init(count,2)+1;
init(end,3)=init(count,3);
RZ01(init(count,1),init(count,2)+1,init(count,3))=2;
end
if RZ01(init(count,1),init(count,2)-1,init(count,3))==1
init(end+1,1)=init(count,1);
init(end,2)=init(count,2)-1;
init(end,3)=init(count,3);
RZ01(init(count,1),init(count,2)-1,init(count,3))=2;
end
if RZ01(init(count,1),init(count,2),init(count,3)+1)==1
init(end+1,1)=init(count,1);
init(end,2)=init(count,2);
init(end,3)=init(count,3)+1;
RZ01(init(count,1),init(count,2),init(count,3)+1)=2;
end
if RZ01(init(count,1),init(count,2),init(count,3)-1)==1
init(end+1,1)=init(count,1);
init(end,2)=init(count,2);
init(end,3)=init(count,3)-1;
RZ01(init(count,1),init(count,2),init(count,3)-1)=2;
end
end
count=count+1;
[points dim]=size(init);
if round(count/20)==count/20
for b=1:points
c(init(b,1),init(b,2),init(b,3))=1;
end
clf
c(end+1,:,:)=0;
c(:,end+1,:)=0;
c(:,:,end+1)=0;
% plot3(0.5,0.5,0.5);
p = patch(isosurface(smooth3(smooth3(c)),0.3));
axis equal
set(p,'FaceColor','m','EdgeColor','none');
alpha(1)
view(3)
axis([min(init(:,2))-3 max(init(:,2))+3 min(init(:,1))-3 max(init(:,1))+3 min(init(:,3))-3 max(init(:,3))+3]);
camlight('right')
grid
drawnow
end
end
PixelCount=size(find(c==1))
pxlsz=info.SpatialResolution;
ROIvolume=gp*pxlsz.^2*PixelCount(1)
TotalCPS=0;
for b=1:points
b
c(init(b,1),init(b,2),init(b,3))=1;
CPSlist(b)=spect(init(b,1),init(b,2),init(b,3));
TotalCPS=TotalCPS+double(CPSlist(b));
end
TotalCPS
CPSlist
Volume=points*pxlsz^2*gp

Accepted Answer

yanqi liu
yanqi liu on 11 Oct 2021
sir,i think may be use logical matrix as mask to filter gray image
  25 Comments
Image Analyst
Image Analyst on 12 Oct 2021
A pixel is just a voxel in 3-D. You take a 2-D logical image of black and white pixels and stuff that as a slice into a 3-D logical image and voila! the pixels are now voxels.
nnz() gives the total number of voxels with a value of "true" in your 3-D binary image. You said you want, quote, "the volume will calculated, and the sum number pixel count also calculated." (Actually those are the same thing). I did not see how @yanqi liu's answer gave you the volume (number) of voxels in your 3-D binary image so that's why I suggested nnz.
The total volume of your 3-D image is numel(image3D) which is just the product of rows times columns times number of slices. It's ALL the pixels, no matter if they are black or white.
bwlabeln() -- not bwlabel() -- gives you the total number of blobs in a 3-D image, which will be less than the total number of true voxels if some of the blobs have more than one voxel in them.
To get the volume of a 3-D binary image, you can do
numTrueVoxels = nnz(image3D); % Count of only the true/white/1 voxels, not the false/black/0 ones.
numVoxels = numel(image3D); % All the voxels in the image, both true ones and false ones.
volumeFraction = numTrueVoxels / numVoxels;
% Label the image and count the blobs:
[labeledImage, numberOfBlobs] = bwlabeln(image3D);

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 9 Oct 2021
It's hard to follow this mostly uncomment program but in general if you have a 3-D image called image3d and a binary image called binaryImage and you want to set slice k of the 3-D image to be true in the same (x,y) locations as the binary image you'd do
image3d(:, :, k) = binaryImage;
Do that for all slices and binary images.

Community Treasure Hunt

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

Start Hunting!