## Lab 16 Arnold's Cat Map

#### Background

Read Section 11.15 in the book. I'll provide MATLAB specific details below.

#### MATLAB

##### Images
An image in MATLAB is a M by N or M by N by 3 array, with the 1st version used for grayscale and the 2nd version for RGB images. In the first case the values range from 0 to 255 and in the second they range from 0 to 1. Here's some examples of random images and how to view them:
```cgray = 256*rand(30);    % 30x30 random grayscale image
image(cgray), colormap gray   % display image and set colormap to grayscale
crgb = rand(30,30,3);    % 30x30(x3) random RGB image
image(crgb)              % display (colormap has no effect)
```
You create an image by setting values or you can use the command imread to read an existing image. You could create one using Paint and then read it, or you can even read one off the internet:
```smokey = imread('http://www.math.utk.edu/~ccollins/UTpicts/ut7.jpg');
image(smokey)
```
Now for this lab we need square images, so after you read an image, you'll need to check the size via size, for example size(smokey) returns 127 149 3 so it is not square. I can either trim it to 127x127:
```smokeytrim = smokey(:,1:127,:);
```
You could use other ranges of columns to center the image, but make sure you use exactly 127 (or whatever the smallest dimension is).
##### The Map
The easiest way to work with the Arnold Cat Map is to write it as a function so that each time you pass the function the image it produces the image with the map applied once. From the book pg. 708 formula (2) we have the basic formula. The only part we have to be careful with is that MATLAB starts the numbering at 1 not 0 so we'll have to be shifting back and forth. Here's a function:
```function X = catmap(Y)
% applies Arnold's Cat Map to the image Y to produce image X

p = size(Y,1);      % get the number of pixels on each side
X = zeros(size(Y)); % make space for X (all zeros to start)
for i = 1:p         % loop through all the pixels
for j = 1:p
newi = mod(((i-1) + (j-1)),p) + 1;     % get new i coord (m+n) mod p
newj = mod(((i-1) + 2*(j-1)),p) + 1;   % get new j coord (m+2n) mod p
X(newi,newj,:) = Y(i,j,:);
end
end
X = uint8(X);   % this may have to be adjusted depending on the type of image
```
To use this function, start with an image, say it is called myimage. Since we want to apply this function over and over again, we first copy your image into a temporary variable X to use and then we apply the function over and over again. We can put this in a loop, have it display the image and pause so we can see it, like this:
```X = myimage;
for i = 1:100
X = catmap(X);
image(X)
pause
end
```
For the trimmed smokey image, I found that it took 128 iterations (instead of 100) to get back to the original image.

#### Exercise

Get an image (not Smokey, but something new) of a decent size but not larger than 256x256. Load it into MATLAB and trim it so that it is square. Save the program catmap and then find out how many iterations it takes before the original image is returned. The book gives a graph relating number of iterations to size of image, so you probably will need about as many iterations as the size of your image.

Print out your image, a jumbled version of your image (for some intermediate iteration). Tell me the size of your image and the number of iterations it took for it to return to the original.

Mail: ccollins@math.utk.edu