Shifting an image using matrix manipulation.

4 views (last 30 days)
Hi, I have a codeline that shifts an image 240 pixels horizontally. I used the code from an example in my class, however they do it in grayscale and I want to keep the shifted image in the original color. What do i have to modify in order to keep the same color as the original image.
Here's the code:
if true
% code
function [] = hshift
X=imread('photo1.jpg');
X_double=double(X);
red_channel_X= X_double(:,:,1);
green_channel_X=X_double(:,:,2);
blue_channel_X=X_double(:,:,3);
X1=.3*red_channel_X+.3*green_channel_X+.4*blue_channel_X;
colormap('default')
[m,n]=size(X1);
r=240;
E=eye(n);
T=zeros(n,n);
T(:,1:r)=E(:,n-(r-1):n);
T(:,r+1:n)=E(:,1:n-r);
X_shift=X1*T;
imagesc(uint8(X_shift));
colormap('default');
end

Accepted Answer

Image Analyst
Image Analyst on 30 Jun 2016
Simply use imtranslate().

More Answers (0)

Community Treasure Hunt

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

Start Hunting!