Reputation: 13
I am a very new programmer (this is my second exercise), and I have been asked to shift an image (of a Galaxy) by a sinusoidal shift. I have tried to write a programme to do this as follows:
My programme:
GalaxyImage = imread('hs-2013-06-a-web.jpg');
image(GalaxyImage)
GalaxyImage=double(GalaxyImage)/255;
image(Sess2GalaxyIm(GalaxyImage));
My function:
function [GalaxySlanted] = Sess2GalaxyIm(GalaxyImage)
A = 20;
k = 3;
Y = size(GalaxyImage, 1);
X = size(GalaxyImage, 2);
max_shift = ceil(A*2);
GalaxySlanted = zeros(Y, X+max_shift, 3);
for y=1:Y
local_shift = ceil(A*sind(k*y));
local_x = 1:X;
local_x = local_x + local_shift;
GalaxySlanted(y, local_x, :)=GalaxyImage(y, :, :);
end
end
At the moment, when I run it, it seems to just be shifting my image by a constant amount and I don't see why.
Upvotes: 1
Views: 751
Reputation: 7817
There is a possible issue with how you define local_x. You start by adding some padding onto the image to compensate for the fact that the output x could be up to "A" pixels shifted on either side:
max_shift = ceil(A*2);
GalaxySlanted = zeros(Y, X+max_shift, 3);
However, in the loop, you ignore this and only start off with local_x
as 1:X - so if the output from sind
is negative you will be trying to index negative numbers into GalaxySlanted
and get a resulting error. Changing that line to local_x = (1:X)+A;
should work (with oMiD's modification of the shift).
Another quick note - there is actually a function im2double
which will handle the image conversion, and scaling, in one go.
Upvotes: 0
Reputation: 322
Your image's shift is constant because in local_shift = ceil(A*sind(k*Y));
you used constant values for A,K and Y. may be this modification makes your code correct:(y instead of Y)
local_shift = ceil(A*sind(k*y));
Upvotes: 4