user2192778
user2192778

Reputation: 455

How to do a horizontal shift of an image in MATLAB?

I have an image that I have converted into a double matrix. I want to shift it horizontally, but I am not sure how to do this. I try to manipulate the position, but it turns out that I am only manipulating the color.

Is there a way that I can reference the pixel position instead and then add a constant to perform a shift?

Upvotes: 6

Views: 16086

Answers (4)

Gelliant
Gelliant

Reputation: 1845

If you do not have the toolbox and still want to do subpixel shifting you can use this function. It makes a filter that is a single pixel shifted in x and y and applies the filter on the image.

function [IM] = shiftIM(IM,shift)
S = max(ceil(abs(shift(:))));
S=S*2+3;
filt = zeros(S);
shift = shift + (S+1)/2;
filt(floor(shift(1)),floor(shift(2)))=(1-rem(shift(1),1))*(1-rem(shift(2),1));
filt(floor(shift(1))+1,floor(shift(2)))=rem(shift(1),1)*(1-rem(shift(2),1));
filt(floor(shift(1)),floor(shift(2))+1)=(1-rem(shift(1),1))*rem(shift(2),1);
filt(floor(shift(1))+1,floor(shift(2)+1))=rem(shift(1),1)*rem(shift(2),1);
IM=conv2(IM, filt, 'same');
end

%to test
IM = imread('peppers.png');IM = mean(double(IM),3);
for ct = 0:0.2:10
    imagesc(shiftIM(IM,[ct/3,ct]))
    pause(0.2)
end
%it should shift to the right in x and y

Upvotes: 0

Amro
Amro

Reputation: 124543

Using the Image Processing Toolbox, you can apply a spatial transformation:

img = imread('pout.tif');
T = maketform('affine', [1 0 0; 0 1 0; 50 100 1]);   %# represents translation
img2 = imtransform(img, T, ...
    'XData',[1 size(img,2)], 'YData',[1 size(img,1)]);
subplot(121), imshow(img), axis on
subplot(122), imshow(img2), axis on

affine translation

Upvotes: 8

erikced
erikced

Reputation: 732

You could use circshift to perform a circular shift, im = circshift(im, [vShift hShift]).

Upvotes: 5

ypnos
ypnos

Reputation: 52317

Say your image is matrix A and you want to wrap x columns to the left:

A = [A(x+1:end,:) A(1:x,:)];

Upvotes: 2

Related Questions