Adding effects to imported pictures
Show older comments
The goal of what I am trying to do it import an image with inputs of amplitude and frequency and produce a wave over the picture. I am lost somewhere in the loops and it would be very helpful if someone could fix it. I need to also make sure that the x and y stay inside the domain this is the code I have now.
img = imread(img.jpg);
img = im2double(img);
S = size(img)
amplitude = 50;
frequency = 1/30;
max = max(size(img));
for x = 1:max
for y = 1:max
x1 = x + amplitude*cos(frequency*y);
y1 = y;
imagechange(x,y,:) = img(x,y,:);
end
end
imshow(imagechange)
Answers (1)
Guillaume
on 16 Mar 2017
Let's start with the problems with your code
max = max(size(img));
I don't know where you got the idea to name a variable the same as the function that's used to generate it, but it's an extremely bad idea. From this point on, you can't use the max function since matlab will understand it as indexing into your max variable instead. Never ever name a variable the same as matlab's commonly used functions.
for x = 1:max
for y = 1:max
Unless the image is square, one of x and y will go over the boundary of the image. What is the point of it when you could just make them stop at the boundary. So,
for x = 1:size(img, 2)
for y = 1:size(img, 1)
This also ties with:
imagechange(x, y, :) = img(x,y,:)
Conventionally, x is the horizontal direction and y is the vertical one. 1st index of a matrix is row (vertical), 2nd is the column (horizontal). To avoid confusing everybody, use either
imagechange(y, x, :) = img(y, x, :);
or
imagechange(row, col, :) = img(row, col, :);
And of course, one of these y, x should be y1, x1, otherwise you're just copying the image.
Now to answer your question, there are many ways you can constrain your x1, y1 to the image boundary, you could simply
x1 = min(max(x1, 0), size(img, 2));
Or you could wrap around:
x1 = mod(x1-1, size(img, 2)) + 1;
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!