Pregunta

While trying to write a function to shift an image I came to the problem that the image looks different when copied to a new image. First I create a new image with each side 3 times bigger. Then I copy to it the first image, shifted. But then the image looks different. If I switch to the jet colormap, both images look the same. What is the reason for this?

The code I'm using:

% function newImage = shiftImage(Image,x_shift,y_shift)

[Image, map] = imread('sun.gif'); %availible at http://i.imgur.com/e24QOsX.gif
I = Image(:,:,:,1); %frame 1
I1 = Image(:,:,:,2); %frame 2
I2 = Image(:,:,:,3); %frame 3

x_shift = 100;
y_shift = 150;

h = size(I,1);
w = size(I,2);

newI = zeros(3*size(I));

newI((h+y_shift):(2*h+y_shift-1),(w+x_shift):(2*w+x_shift-1)) = I; % copies I to a region of newI

figure(1); clf;
subplot(2,1,1); imshow(I,map); % shows first frame
subplot(2,1,2); imshow(newI((h+y_shift):(2*h+y_shift-1),(w+x_shift):(2*w+x_shift-1)),map);
% shows region in newI which is equal to I

% colormap(jet) % if I set the colormap to jet, both look the same
% colormap(map) % but if I use colormap from the gif, they look different

all(all( newI((h+y_shift):(2*h+y_shift-1),(w+x_shift):(2*w+x_shift-1)) == I ))  
% compares if all cells from I and region in newI which is supposed to be I
% are identical; they are

figure(2); clf; imshow(newI,map); % how the whole newI looks
¿Fue útil?

Solución

map is a map which is defined on a uint8 space, your image I is uint8 and displayed correctly. newI is of type double, I think the map is repeated 2^24 times to match 32bit color, whatever its "broken".

Convert newI to uint8 and everything is okay: newI=uint8(newI)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top