I want to display images at their original sizes (e.g., new_img is larger than main_img, but when shown, they appear to be the same size).

8 views (last 30 days)
clc;
close all;
clear;
main_img = rgb2gray(imread('flower.jpg'));
[row, col] = size(main_img);
new_img = ones(row*3, col*3);
[n_row, n_col] = size(new_img);
for r_index = 1:n_row
for c_index = 1:n_col
n_r_index = round(r_index / 3);
n_c_index = round(c_index / 3);
if (n_c_index < 1 || n_r_index < 1 || n_c_index > col || n_r_index > row)
continue
end
new_img(r_index, c_index) = main_img(n_r_index, n_c_index);
end
end
subplot(2, 1, 1)
imshow(main_img, [])
title("Original Image")
subplot(2, 1, 2)
imshow(new_img, [])
title("New Image")
imwrite(uint8(new_img), 'new_flower.jpg');
  2 Comments
Manikanta Aditya
Manikanta Aditya on 25 Mar 2024
Try to use the imshow function with InitialMagnification property set to 'fit'. This will display the image at its actual size, unless the image is too big to fit on the screen at that magnification. If the image is too big imshow scales the image to fit mostly.
figure, imshow(main_img, 'InitialMagnification', 'fit'), title("Original Image");
figure, imshow(new_img, 'InitialMagnification', 'fit'), title("New Image");
Hope it helps :)
DGM
DGM on 25 Mar 2024
That still isn't going to fix the fundamental problem that there is probably simply not enough room to display the image on screen at full size.
main_img = imread('peppers.png');
main_img = rgb2gray(main_img);
[row, col, ~] = size(main_img);
new_img = ones(row*3, col*3,class(main_img)); % allocate to the appropriate class
[n_row, n_col] = size(new_img);
for r_index = 1:n_row
for c_index = 1:n_col
n_r_index = round(r_index / 3);
n_c_index = round(c_index / 3);
if (n_c_index < 1 || n_r_index < 1 || n_c_index > col || n_r_index > row)
continue
end
new_img(r_index, c_index) = main_img(n_r_index, n_c_index);
end
end
% this image is 384x512
subplot(2, 1, 1)
imshow(main_img,'initialmagnification','fit')
title("Original Image")
% this image is 1152x1536
subplot(2, 1, 2)
imshow(new_img,'initialmagnification','fit')
title("New Image")
To the question "how can I always display images at 1:1 in a subplot?", the answer is "you can't". At least not always, and enforcing it is problematic and dependent on your display resolution, the image size, figure setup, version, etc. If it's not something that can be consistently achieved, and it's not actually necessary for visualization, then pursuing it is probably not a good use of time.
That said, if you're working with a small image and just want something that only works with that one case, then maybe consider using truesize(), though be aware that it only works if there is only a single image object in the figure, so using subplots isn't an option as far as I can tell.
main_img = imread('sadberry_gray.png');
main_img = im2gray(main_img);
[row, col, ~] = size(main_img);
new_img = ones(row*3, col*3,class(main_img)); % allocate to the appropriate class
[n_row, n_col] = size(new_img);
for r_index = 1:n_row
for c_index = 1:n_col
n_r_index = round(r_index / 3);
n_c_index = round(c_index / 3);
if (n_c_index < 1 || n_r_index < 1 || n_c_index > col || n_r_index > row)
continue
end
new_img(r_index, c_index) = main_img(n_r_index, n_c_index);
end
end
% this image is 19x17
fh1 = figure(2);
imshow(main_img)
title("Original Image")
truesize(fh1,size(main_img,1:2))
% this image is 57x51
fh2 = figure(3);
imshow(new_img)
title("New Image")
truesize(fh2,size(new_img,1:2))
Is there a better option for subplots? I'd have to think about it, but the phone is ringing, so...

Sign in to comment.

Answers (1)

DGM
DGM on 25 Mar 2024
Edited: DGM on 25 Mar 2024
Consider this as a compromise if the goal is simply to illustrate the relative scale.
main_img = imread('peppers.png');
main_img = im2gray(main_img);
[row, col, ~] = size(main_img);
new_img = ones(row*3, col*3,class(main_img)); % allocate to the appropriate class
[n_row, n_col] = size(new_img);
for r_index = 1:n_row
for c_index = 1:n_col
n_r_index = round(r_index / 3);
n_c_index = round(c_index / 3);
if (n_c_index < 1 || n_r_index < 1 || n_c_index > col || n_r_index > row)
continue
end
new_img(r_index, c_index) = main_img(n_r_index, n_c_index);
end
end
ha1 = subplot(2, 1, 1);
imshow(main_img)
title("Original Image")
ha2 = subplot(2, 1, 2);
imshow(new_img)
title("New Image")
% instead of trying to force the displayed images to have their native resolution
% which may not actually be possible given display extents, simply represent the
% relative scale of the images by changing the position of the smaller image
% according to the proportional relationship between the two images.
scalefactor = size(new_img,1:2)./size(main_img,1:2); % [y x]
adjustedsize = ha1.Position(3:4)./fliplr(scalefactor); % [x y]
ha1.Position = [ha1.Position(1:2) + (ha1.Position(3:4)-adjustedsize)/2 adjustedsize];

Products

Community Treasure Hunt

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

Start Hunting!