Showing multiple images in one window in Matlab

3 次查看(过去 30 天)
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 个评论
KSSV
KSSV 2017-7-19
编辑:KSSV 2017-7-19
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! 2017-7-28
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

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2017-7-28
Change
J = zeros(size(I,1), size(I,2), N);
to
J = zeros(size(I,1), size(I,2), N, class(I));
  2 个评论
Walter Roberson
Walter Roberson 2017-7-28
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.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by