How rescale and save image in Matlab?

1 view (last 30 days)
Neda
Neda on 16 May 2019
Commented: Neda on 21 May 2019
I am reading a 'mhd' file which it's giving me an info like below:
Filename: 'patient0001_2CH_ED_gt.mhd'
Format: 'MHA'
CompressedData: 'false'
ObjectType: 'image'
NumberOfDimensions: 3
BinaryData: 'true'
ByteOrder: 'false'
TransformMatrix: [1 0 0 0 1 0 0 0 1]
CenterOfRotation: [0 0 0]
AnatomicalOrientation: 'RAI'
Offset: [0 0 0]
PixelDimensions: [0.3080 0.1540 1.5400]
Dimensions: [549 778 1]
ElementNumberOfChannels: '1'
DataType: 'uchar'
DataFile: 'patient0001_2CH_ED_gt.raw'
BitDepth: 8
HeaderSize: 384
Then by another function I will read the info, then produce the image. Then, I need to rescale image dimension with pixel dimension ( PixelDimensions: [0.3080 0.1540 1.5400]), and I used imagescin order to do this like below code, but it change the unique value of my image. Is there any way to rescale the image without imagesc? alos imagesc produce 3 channels which I don't want, and apparently all channels are same.
rows = 1:info.Dimensions(1);
cols = 1:info.Dimensions(2);
Im2 = imagesc(rows.*info.PixelDimensions(1),cols.*info.PixelDimensions(2),Im(:,:)');
saveas(gcf,'Output_filename.png')
  2 Comments
Jurgen vL
Jurgen vL on 21 May 2019
You are fundamentally misunderstanding what imagesc does. You want spatial resizing, not graylevel rescaling. Consider using imresize3.
Neda
Neda on 21 May 2019
since, I am using 2dimension images, I used imresize., but still same problem. I have done it same as below. Could you please point me to the right direction?
info = SureScan_mhd_read_header('filename.mhd');
Im = SureScan_mhd_read_volume(info);
%rescale image based on info.PixelDimension, image need to be transposed as well
rows = 1:info.Dimensions(1);
cols = 1:info.Dimensions(2);
rows.*info.PixelDimensions(1),cols.*info.PixelDimensions(2),Im(:,:)'
%I did imresize image
im_2 = imresize(ans,[256 256],'nearest');
imshow(im_2)
% it's all black while if I get b = unique(ans(:,:)) is include 0
,1, 2, 3
%then , I did imagsc to see the image
imagesc(im_2,[0 3]);colormap(gray)
saveas(gcf,'test_4.png')
test_4 = imread('test_4.png')
%when I save it like saveas(gcf,'test_4.png'), and read it back again ;
% image size is not 256 *256, and it's 545 672 3 while when I open png file,
% the axis displays 256 * 256

Sign in to comment.

Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!