Troubles With Image Resizing
Hello! I have a slice of a .nii file (essentially a 3D array) I’d like to print out. However, the dimensions of a voxel (a 3D pixel, so basically a cube) aren’t exactly 1mm x 1mm x 1mm. They’re around 1.2mm x 1.1mm x 1.1mm.
I took a slice of the 3D array, so it’s just a 2D image. So, the dimensions of each pixel are ~1.2mm x 1.1mm. I’d like to resize the image such that one pixel is equal to one element of the array is equal to one mm. In theory, it doesn’t seem too difficult. But I just can’t figure this out.
Here’s my code so far:
clear
clc
close all
% Load NIfTI image
info = niftiinfo(‘index_image.nii’);
brain = niftiread(info);
% Extract a slice from the NIfTI image
slice = brain(:, :, 128);
% Create figure
hFig = figure;
% Display the slice with pixel-to-mm scaling
imagesc(slice);
axis off;
axis equal;
colormap(gray)
% Set figure size so that 1 pixel corresponds to 1mm on the printed figure
set(hFig, ‘Units’, ‘centimeters’, ‘Position’, [0 0 24 17.6]);
movegui(hFig, ‘center’);
% Set properties to control the output size
set(hFig, ‘PaperUnits’, ‘centimeters’);
set(hFig, ‘PaperPosition’, [0 0 24 17.6]);
set(hFig, ‘PaperOrientation’, ‘landscape’);
% Export to PDF and open file
print(hFig, ‘-dpdf’, ‘-r0’, ‘out.pdf’);
open(‘out.pdf’);
The reason why use 24 and 17.6 is because my .nii file is 176 x 240 x 256 elements. I’d honestly like the code to work regardless of the size of the array, but even hard coding it into MATLAB isn’t working. The code is compiling just fine, but the figure in the generated PDF doesn’t have the dimensions I’d like it to have.
Would anyone be able to provide some insights? Thank you!Hello! I have a slice of a .nii file (essentially a 3D array) I’d like to print out. However, the dimensions of a voxel (a 3D pixel, so basically a cube) aren’t exactly 1mm x 1mm x 1mm. They’re around 1.2mm x 1.1mm x 1.1mm.
I took a slice of the 3D array, so it’s just a 2D image. So, the dimensions of each pixel are ~1.2mm x 1.1mm. I’d like to resize the image such that one pixel is equal to one element of the array is equal to one mm. In theory, it doesn’t seem too difficult. But I just can’t figure this out.
Here’s my code so far:
clear
clc
close all
% Load NIfTI image
info = niftiinfo(‘index_image.nii’);
brain = niftiread(info);
% Extract a slice from the NIfTI image
slice = brain(:, :, 128);
% Create figure
hFig = figure;
% Display the slice with pixel-to-mm scaling
imagesc(slice);
axis off;
axis equal;
colormap(gray)
% Set figure size so that 1 pixel corresponds to 1mm on the printed figure
set(hFig, ‘Units’, ‘centimeters’, ‘Position’, [0 0 24 17.6]);
movegui(hFig, ‘center’);
% Set properties to control the output size
set(hFig, ‘PaperUnits’, ‘centimeters’);
set(hFig, ‘PaperPosition’, [0 0 24 17.6]);
set(hFig, ‘PaperOrientation’, ‘landscape’);
% Export to PDF and open file
print(hFig, ‘-dpdf’, ‘-r0’, ‘out.pdf’);
open(‘out.pdf’);
The reason why use 24 and 17.6 is because my .nii file is 176 x 240 x 256 elements. I’d honestly like the code to work regardless of the size of the array, but even hard coding it into MATLAB isn’t working. The code is compiling just fine, but the figure in the generated PDF doesn’t have the dimensions I’d like it to have.
Would anyone be able to provide some insights? Thank you! Hello! I have a slice of a .nii file (essentially a 3D array) I’d like to print out. However, the dimensions of a voxel (a 3D pixel, so basically a cube) aren’t exactly 1mm x 1mm x 1mm. They’re around 1.2mm x 1.1mm x 1.1mm.
I took a slice of the 3D array, so it’s just a 2D image. So, the dimensions of each pixel are ~1.2mm x 1.1mm. I’d like to resize the image such that one pixel is equal to one element of the array is equal to one mm. In theory, it doesn’t seem too difficult. But I just can’t figure this out.
Here’s my code so far:
clear
clc
close all
% Load NIfTI image
info = niftiinfo(‘index_image.nii’);
brain = niftiread(info);
% Extract a slice from the NIfTI image
slice = brain(:, :, 128);
% Create figure
hFig = figure;
% Display the slice with pixel-to-mm scaling
imagesc(slice);
axis off;
axis equal;
colormap(gray)
% Set figure size so that 1 pixel corresponds to 1mm on the printed figure
set(hFig, ‘Units’, ‘centimeters’, ‘Position’, [0 0 24 17.6]);
movegui(hFig, ‘center’);
% Set properties to control the output size
set(hFig, ‘PaperUnits’, ‘centimeters’);
set(hFig, ‘PaperPosition’, [0 0 24 17.6]);
set(hFig, ‘PaperOrientation’, ‘landscape’);
% Export to PDF and open file
print(hFig, ‘-dpdf’, ‘-r0’, ‘out.pdf’);
open(‘out.pdf’);
The reason why use 24 and 17.6 is because my .nii file is 176 x 240 x 256 elements. I’d honestly like the code to work regardless of the size of the array, but even hard coding it into MATLAB isn’t working. The code is compiling just fine, but the figure in the generated PDF doesn’t have the dimensions I’d like it to have.
Would anyone be able to provide some insights? Thank you! image processing MATLAB Answers — New Questions