Showing multiple images in one window in Matlab

3 views (last 30 days)
Why doesn't the following code show the images?
clear all;
image_name = 'woman.png';
I = gray_imread(image_name);
N = 12;
J = zeros(size(I,1), size(I,2), N);
for i=1:N
J(:,:,i) = I;
end
sqrtt = ceil(sqrt(N));
m = sqrtt;
n = sqrtt;
for k=1:N
K = J(:,:,k);
subplot(m,n,k);
imshow(K);
set(gca,'xtick',[],'ytick',[])
end
How can I solve the issue?
  2 Comments
KSSV
KSSV on 19 Jul 2017
Edited: KSSV on 19 Jul 2017
What is size of I? What is this function gray_imread?? We cannot help you unless the function is known.
Ba Ba Black Sheep!
Ba Ba Black Sheep! on 28 Jul 2017
function img = gray_imread( image_name )
I = imread(image_name);
if(is_rgb(I))
img = rgb2gray(I);
elseif (is_gray(I))
img = I;
end
function ret = is_gray( yourImage )
[~, ~, numberOfColorChannels] = size(yourImage);
if(numberOfColorChannels == 1)
ret = 1;
else
ret = 0;
end
function ret = is_rgb( a )
if size(a,3)==3
ret = 1;
else
ret = 0;
end

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 28 Jul 2017
Change
J = zeros(size(I,1), size(I,2), N);
to
J = zeros(size(I,1), size(I,2), N, class(I));
  2 Comments
Walter Roberson
Walter Roberson on 28 Jul 2017
You are accidentally converting uint8 values to double with the same integer values. However MATLAB looks at the data type to determine the expected range of values. For the integer data types like uint8 the smallest integer of the type is mapped to the first color in the color map and the highest valid integer for the type is mapped to the last color in the map. For the floating point types the rule is different: 0 is mapped to the first colour and 1 is mapped to the last colour. So where uint8(128) would map halfway between into the colour map, double(128) is way outside the expected 0 to 1 range and so would get mapped to the last colour. You end up with an image that is black where the original was exactly 0 and bright white everywhere else.
The change to the code that I showed does not initialize J as double (the default when you use zeros), and instead copies the data type such as uint8 from the original matrix to J so that the original and J will be treated the same way by the graphics engine.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!