Conversion matlab code to python code

4 views (last 30 days)
hyounwook jung
hyounwook jung on 3 Apr 2019
Commented: Rik on 3 Apr 2019
I want to convert matalb code to python code.
But I am having difficulty converting the code so, leave a question.
IM = load_nii(uigetfile('*.nii','Select the Analyze file format'), [], [], [], [], [], 1); % Load 3D CT Image
movingVolume = flipud(IM.img);
spineVolume = movingVolume;
[row,col,slice] = size(movingVolume);
for z=1:slice
for i=1:row
for j=1:col
if j < 330
movingVolume(i,j,z) = -1;
end
end
end
end
for z=1:slice
for i=1:row
for j=1:col
if movingVolume(i,j,z) > -800
movingVolume(i,j,z) = 1;
else
movingVolume(i,j,z) = 0;
end
end
end
end
  2 Comments
Guillaume
Guillaume on 3 Apr 2019
It looks like you've already done the conversion and since you don't tell us anything about the original code we can't tell if it's correct or not. You also don't ask a question, so it's unclear what you want.
The code you have written can be greatly simplified:
IM = load_nii(uigetfile('*.nii','Select the Analyze file format'), [], [], [], [], [], 1); % Load 3D CT Image
movingVolume = flipud(IM.img);
spineVolume = movingVolume; %not sure what the purpose of that is. You don't show SpineVolume being used
movingvolume(:, 1:329, :) = -1; %replace your first loop
movingvolume = movingvolume > -800; %replace your 2nd loop
Rik
Rik on 3 Apr 2019
Some small edits for edge cases:
IM = load_nii(uigetfile('*.nii','Select the Analyze file format'), [], [], [], [], [], 1); % Load 3D CT Image
movingVolume = flipud(IM.img);
spineVolume = movingVolume; %not sure what the purpose of that is. You don't show SpineVolume being used
movingvolume(:, 1:min(329,end), :) = -1; %replace your first loop
movingvolume = double(movingvolume > -800); %replace your 2nd loop
%or possibly: movingvolume = cast(movingvolume > -800,'like',movingvolume);

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!