Tuesday, May 27, 2008

assignment 6

1) Let's just say that I worked with Thomas on this first part. God, he's smart.

First, we(?) created two functions called M-files in Matlab's Editor called newrotate and newskew. Newrotate requires inputs of the matrix and an angle. Newskew requires inputs of the matrix and the skew factor. Then after inputting the matrix bigT, assignment 5 question 3 is reduced to 2 lines. Using my values of s=2 and theta = 5*pi/4 here is the A5Q3 image again:


In the editor:
function [B] = newrotate(A,theta)
d = size(A)
B=ones(d);
c =cos(theta);
s=sin(theta);
for x=1:d
for y=1:d;
B(round(mod(x*c-y*s,256)+1),round(mod(x*s+y*c,256)+1))= A(x,y);
end
end


function [B] = newskew( A,s )
d = size(A)
B=ones(d);
for x=1:d
for y=1:d;
B(x,round(mod(x*s+y,256)+1))= A(x,y);
end
end


And in Matlab itself:
R=newrotate(newskew(bigT,2),5*pi/4);
imshow(R)


Next we are to use bilinear interpolation and unskew/unrotate the bigT from assignment 5. Using Mike's commands and Matlab:

newbigT= ones(256);
bigT=255*ones(256);
bigT(30:79,64:191)=zeros(50,128);
bigT(50:199,111:146)=zeros(150,36);
theta=(5*pi/4);
for x=1:256;for y=1:256;
u=x*cos(theta)+y*sin(theta);
v=-x*sin(theta)+y*cos(theta);
up=mod(u,256) + 1;
vp=mod(v,256) + 1;
a=up;
b=mod(vp-2*up,256)+1;
r=floor(a);
s=floor(b);
if (r>0) & (r<256)>0) & (s<256);
M1=[1-a+r, a-r];
M2=[bigT(r,s), bigT(r,s+1); bigT((r+1),s), bigT((r+1),(s+1))];
M3=[1-b+s;b-s];
newbigT(x,y)= M1*M2*M3;
end;
end;
end;
imshow(newbigT)


2) Create a 256x256 matrix with 1's in the (i, i+1) position and zeros elsewhere.

Solution: (done using Matlab 2007)

The command is:
x=(256,1);
diag(x(1:255),1)

Since this is much too big to display here, I'll use a smaller example to illustrate:
x=ones(5,1);
diag(x(1:4),1)

ans =
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
0 0 0 0 0


3) Given a color image determine the 'average' colour. Compute the average colour for the image with:

Note: I came across an interesting website http://www.wisegeek.com/how-can-i-find-the-average-color-in-a-photograph.htm which will find the average colour in any jpeg file image for you. But how will the geek's answers compare with ours?

a) R=ones(100);, G=tril(ones(100));, B=zeros(100);


The image on the top is the original one, the bottom is the average colour using calculations.
The Matlab commands:

X(:,:,1)= ones(100);
X(:,:,2)=tril(ones(100));
X(:,:,3)=zeros(100);
imshow(X)

size(X)
ans =
100 100 3

sum(sum(X))/100/100
ans(:,:,1) = 1
ans(:,:,2) = 0.5050
ans(:,:,3) = 0

Y=ones(100);
Y(:,:,1)=1*ones(100);
Y(:,:,2)=0.5050*ones(100);
Y(:,:,3)=zeros(100);
imshow (Y)


b) The rainbow picture from assignment 4.

The image on the top is the Facebook rainbow and the image on the bottom is the average colour.



I 'imread' it into Matlab and found:

A=imread('rainbow.jpg');
size(A)
ans = 109 200 3

sum(sum(A)) /(109*200)
ans(:,:,1) =
144.6052
ans(:,:,2) =
88.0523
ans(:,:,3) =
76.9043

ans/256
ans(:,:,1) =
0.5649
ans(:,:,2) =
0.3440
ans(:,:,3) =
0.3004

A=ones(256);
A(:,:,1)=0.5649;
A(:,:,2)=0.3440;
A(:,:,3)=0.3004;
imshow(A)


For the rainbow image I created in assignment 4:

size(I)
ans =
256 1024 3

sum(sum(I))/(256*1024)
ans(:,:,1) =
0.5000
ans(:,:,2) =
0.5000
ans(:,:,3) =
0.3750


X=ones(256);
X(:,:,1)=0.5000*ones(256);
X(:,:,2)=0.5000*ones(256);
X(:,:,3)=0.3750*ones(256);
imshow(X)



c) photo #1 (5th row, 4th column). Top is the original, bottom is average colour.

size(A)
ans =
576 750 3

sum(sum(A))/(576*750)
ans(:,:,1) =
109.7425
ans(:,:,2) =
127.9735
ans(:,:,3) =
57.2840

ans/256
ans(:,:,1) =
0.4287
ans(:,:,2) =
0.4999
ans(:,:,3) =
0.2238

D=ones(256);
D(:,:,1)=0.4287*ones(256);
D(:,:,2)=0.4999*ones(256);
D(:,:,3)=0.2238*ones(256);
imshow(D)


photo#2 (5=10th row, 4th column) Top is original, bottom is average colour.

size(A)
ans =
487 660 3

sum(sum(A))/(487*660)
ans(:,:,1) =
14.5507
ans(:,:,2) =
3.6802
ans(:,:,3) =
111.1285


ans/256
ans(:,:,1) =
0.0568
ans(:,:,2) =
0.0144
ans(:,:,3) =
0.4341


B=ones(256);
B(:,:,1)=0.0568*ones(256);
B(:,:,2)=0.0144*ones(256);
B(:,:,3)=0.4341*ones(256);
imshow(B)


In the end, the geek's answers were very similar to ours except for on the red and yellow image at the top. The geek had a pale peach colour for the average colour (which doesn't make nearly as much sense to me as the orange colour Matlab found).
In answer to the question about how the dominant colour relates to the average colour, based on my images it would seem as though if there appears to be a single colour that dominates the image (eg. the green in the grasshopper picture), then the average colour will appear to be close to that dominant colour. On the other hand if there appears to be more than one "co-dominant" colour (eg. in the rainbow), then the average colour does not appear to be close to any one single dominant colour. It depends how "dominant" the dominant colour is in the image.