how to develop 2D images into 3D viewing.

hi all, I have code below. Anyone know how to develop it into viewing as 3D, let say I want add thickness of that images 2.332mm. So that I can develop it into 3D images viewing.
Because if still 2D images, it cannot develop as 3D viewing.
% For binary images (png format, each pixel just have value 1 and 0.)
clc
clear all
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
% For origional images 2D (png format, each pixel have pixel count.)
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,'Image');
imds = imageDatastore(imageDir);
for k = 1:41
% subplot(6,7,i)
J = readimage(imds,k);
end
%for viewing as 3D images
p = patch(isosurface(J, I==1));
axis equal
set(p,'FaceColor','c','EdgeColor','none');
alpha(p,1)
hold
but have error
Error using isosurface (line 72)
Isovalue must be a scalar.
sorry I ask many times, because I'm newer in Matlab Language.

 Accepted Answer

yanqi liu
yanqi liu on 9 Oct 2021
Edited: yanqi liu on 9 Oct 2021
sir,please check the follow code to get some information
% For binary images (png format, each pixel just have value 1 and 0.)
clc
clear all
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);
% binary
Is{i} = logical(I);
end
% For origional images 2D (png format, each pixel have pixel count.)
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,'Image');
imds = imageDatastore(imageDir);
Js = [];
for k = 1:41
% subplot(6,7,i)
J = readimage(imds,k);
if ndims(J) == 3
J = rgb2gray(J);
end
% gray image
Js(:,:,k) = double(uint8(J).*uint8(Is{k}));
end
%for viewing as 3D images
Ds = smooth3(Js);
figure
hiso = patch(isosurface(Ds,5),...
'FaceColor',[1,.75,.65],...
'EdgeColor','none');
hcap = patch(isocaps(Js,5),...
'FaceColor','interp',...
'EdgeColor','none');
colormap copper
view(45,30)
axis tight
daspect([1,1,.8])
lightangle(45,30);
set(gcf,'Renderer','zbuffer'); lighting phong
isonormals(Ds,hiso)
set(hcap,'AmbientStrength',.6)
set(hiso,'SpecularColorReflectance',0,'SpecularExponent',50)

4 Comments

its work liu!!! Thank you so much.
But how to calculate the total volume and count each volume??
I have the code below to calculate the total volume each and total count each volume. just one click at the volume that we want..but how to apply it. Can help me?
[ymax xmax zmax]=size(Js);
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)<ymax && 1<init(count,2) ...
&& init(count,2)<xmax && 1<init(count,3) && init(count,3)<zmax
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
sir,may be use
>> Vertices=get(hiso, 'Vertices');
>> shp = alphaShape(Vertices);
>> totalvol = volume(shp)
Hi sir, this is last once. I promise this is last. If I used 3D images , is it correct my coding?
% For binary images (png format, each pixel just have value 1 and 0.)
clc
clear all
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:40
% subplot(6,7,i)
I = readimage(imds,i);
% binary
Is{i} = logical(I);
end
% For 3D images spect
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
P = (Is{K});
I think should use isosurface to get 3D structure

Sign in to comment.

More Answers (1)

yanqi liu
yanqi liu on 9 Oct 2021
Edited: yanqi liu on 9 Oct 2021
use matrix to smooth data

3 Comments

Tq liu, but still have error
Error using smooth3 (line 51)
V must be a 3D array.
Hi liu,
If i change my 2D images to 3D images, how to write the coordinate binary into 3D?
% For binary images (png format, each pixel just have value 1 and 0.)
clc
clear all
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);
% binary
Is{i} = logical(I);
end
% For 3D images spect
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
Is it like this?
Js(:,:,k) = double(uint8(P).*uint8(Is{K}));
end
%for viewing as 3D images
Ds = smooth3(Js);
yes,use logical image to filter target

Sign in to comment.

Categories

Products

Release

R2021a

Community Treasure Hunt

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

Start Hunting!