Aqua Phoenix
     >>  Lectures >>  Matlab 3  
 

Navigator
   
 
       
   

3.2 Loops - Examples

3.2.1 Color Gradient

For this example, we will build a one dimensional image in which one color fades into another. For a simple color gradient, we will fade black into red, which means in RGB terms (0,0,0) to (255,0,0). This is no different than standing in a dark room and sliding the dimmer on a red light from 0 to 100%.

In order to create this one dimensional image, we require a data structure that can be loaded and displayed by the image function. Since we intend to generate a one dimensional image, we require one row with several columns (each column of which will take a different color), and 3 colors each (Red, Green, and Blue), which will be dimmed selectively.

It is easy to create a new matrix in Matlab - we need not specify the final dimensions:

grad=[];
We now populate the emtpy image in a loop:

for i=1:255
  grad(1,i,1)=i;
  grad(1,i,2)=0;
  grad(1,i,3)=0;
end
The first line defines the range over which the loop iterates: since intensity in images is defined with a maximum of 255, the loop iterates up to 255. The three lines in the body of the loop define each of Red, Green, and Blue. Green and Blue are set to 0, because we are interested in a black to strictly red gradient. Red is set to i because the loop happens to iterate over all possible intensities.

Figure 3.10
Click image to enlarge, or click here to open
Finally, the image can be displayed by converting it to uint8 and calling function image():

image(uint8(grad)); Even though the image appears to have two dimensions, it really only has one. The one row of the image is stretched in the vertical dimension.

Figure 3.11
Click image to enlarge, or click here to open
While the image is in fact a gradient from black to red, it is not 100% correct, even though the human eye would never perceive this subtelty. The lowest intensity as defined in the loop is 1, because the loop iterates from 1 to 255, when in fact the lowest real intensity is 0. However, given the above loop, we cannot define a range from 0 to 255, because i is used to index into the matrix. As there are no 0 indices in Matlab, this would cause an error. One possible correct loop would be:

for i=1:256
  grad(1,i,1)=i-1;
  grad(1,i,2)=0;
  grad(1,i,3)=0;
end
image(uint8(grad));

Figure 3.12
Click image to enlarge, or click here to open
A slightly more complicated one dimensional gradient would, is that of red to green, in RGB: (255,0,0) to (0,255,0). In this case the loop would need to decrement the red value and increment the green value at the same time:

for i=1:256
  grad(1,i,1)=255 - (i-1);
  grad(1,i,2)=i-1;
  grad(1,i,3)=0;
end
image(uint8(grad));

Figure 3.13
Click image to enlarge, or click here to open

3.2.2 Sound Effects

For another example of a one dimensional loop, let us create a constantly rising wave form that resembles the sound of a water drop. We have previously used sin([0:x:y]) for a constant beep, where x denoted the pitch and y the length of the wave form. The full formula to generate a wave form of some pitch is as follows:

Y = sin(2 * pi * f / r * [0:r * l]); where f is the pitch of the tone in Hertz, r is the sample rate of the tone in Hertz, and l is the length of the wave form.

The water drop can be simulated with the following code:

snd=[];
sndTemp=[];
r=8000;
l=0.01;
for f=400:100:2000
  sndTemp=sin(2 * pi * f / r * [0:r * l]);
  snd=[snd, sndTemp];
end
wavplay(snd, 8000);
The loop variable f ranges from frequency 400 to 2000 in increments of 100. In the body of the loop, a sound vector of length 0.01 seconds for the current frequency of f is appended to the final sound vector.

Figure 3.14
Click image to enlarge, or click here to open
Finally, consider writing a loop to reverse 'authoritah.wav':

authoritah=wavread('authoritah.wav');
authoritahRev=[];
l=length(authoritah);
for i=1:l
  authoritahRev(l-i+1)=authoritah(i);
end
wavplay(authoritahRev, 8000);
What language does this come close to?

Figure 3.15
Click image to enlarge, or click here to open

3.2.3 Images - Nested Loops

For an example of a nested loop, we will alter the Red, Green, and Blue color channels in the cartmancop.jpg image. In the first example, consider removing all of the green color:

cartman=imread('cartmancop.jpg');
s=size(cartman);
for i=1:s(1)
  for j=1:s(2)
    cartman(i,j,2) = 0;
  end
end
image(cartman);
The loop iterates over all rows using loop variable i and all columns using loop variable j. In the body of the loop the green channel (2) for each pixel (i,j) is set to 0, while the red and blue color channels are left untouched.

Figure 3.16
Click image to enlarge, or click here to open
In this example we will reproduce the effect of a badly developed photograph which is overexposed on one side and darker on the other. This essentially means that for each row of pixels we will increase the brightness/contrast in the left portion and decrease brightness/contrast in the right portion.

cartman=double(imread('cartmancop.jpg'));
s=size(cartman);
for i=1:s(1)
  for j=1:s(2)
    cartman(i,j,1) = cartman(i,j,1) * 70/j + i;
    cartman(i,j,2) = cartman(i,j,2) * 70/j + i;
    cartman(i,j,3) = cartman(i,j,3) * 70/j + i;
  end
end
image(uint8(cartman));
Since loop variable j iterates over columns, we can use it as a factor of brightness/contrast. Since we use 70/j as a factor for each color channel, the first 69 pixels of each row increase in brightness/contrast (denominator is smaller than numerator), while all pixels > 70 decrease in brightness.

Additionally, we use loop variable i to increase brightness with increasing row number.

Figure 3.17
Click image to enlarge, or click here to open