Showing multiple images in one window in Matlab
1 view (last 30 days)
Show older comments
Ba Ba Black Sheep!
on 19 Jul 2017
Commented: Walter Roberson
on 28 Jul 2017
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
Accepted Answer
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
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!