Sunday, June 15, 2008

Assignment #9

1. Take images from Ass#6 and convert them to an image with 64 colours with the following Octave commands:

A=imread("inpic.jpg");
B=floor(double(A)/64)*85;
imwrite("outpic.jpg",B(:,:,1),B(:,:,2),B(:,:,3))





2. Convert an image to one with only 27 colours using the following Octave command:

A=imread("girl.jpg"); #for colour set {42, 128, 214}
B=floor(double(A)/86)*86+42;
imwrite("girl27.jpg",B(:,:,1),B(:,:,2),B(:,:,3))


The images ares completely washed out (white).


Using the colour set {0, 128, 255}, there are two methods.

Method 1
A=imread("girl.jpg");
B=floor((A+64)/128)*128;
imwrite("girl27.jpg",B(:,:,1),B(:,:,2),B(:,:,3))


Result - Washed out images.

Method 2
A=imread("girl.jpg");
B=floor(A/85)*128;
imwrite("girl27.jpg",B(:,:,1),B(:,:,2),B(:,:,3))



Result - Much better.

3. Turn the images to greyscale images.

A=imread("girl.jpg");
B=(A(:,:,1)+A(:,:,2)+A(:,:,3))/3;
imwrite("girlgrey.jpg",B)






4. Convert images to greyscale with only 64 intensity values.

A=imread("girl.jpg");
B=(A(:,:,1)+A(:,:,2)+A(:,:,3))/3;
C=floor(double(B)/4)*4;
imwrite("girlgrey64.jpg",C)

Here are the two images. As you can see, very faint and difficult to see.




5. Convert images to greyscale with 16 intensity values.

A=imread("girl.jpg");
B=(A(:,:,1)+A(:,:,2)+A(:,:,3))/3;
C=floor(double(B)/16)*16;
imwrite("girlgrey16.jpg",C)

Here are the images.

Saturday, June 14, 2008

Assignment #8 Q5

Using the nearest neighbour approach (Ass#7 Shrink2) , I scaled down four of the images by factors of 0.8, 0.6, 0.4, and 0.2 like so.

A=imread("Q5.jpg");
B=shrink2(A,.8); #size(B) = 408 613 3
C=shrink2(A,.6); #size(C) = 306 459 3
D=shrink2(A,.4); #size(D) = 203 306 3
E=shrink2(A,.2); #size(E) = 101 152 3

Next, I placed each of the scaled images in an appropriate region so to replicate (or at least try to) the given final image.

A(52:459,78:690,:)=B;
A(103:408,155:613,:)=C;
A(155:357,231:536,:)=D;
A(206:306,308:459,:)=E;
F=double(A)/255;
imwrite("Q51.jpg",F(:,:,1),F(:,:,2),F(:,:,3))

Friday, June 13, 2008

Assignment #8

1. For question 1, we were asked to modify the pictures by scaling the x and y directions by different factors. By using the Average Over All Pixels method, this Octave command (like the one in Assign#7) does the job.

function [smallimage]=modify(girl,fx,fy);
girl=double(imread("girl.jpg"));
Mp = floor(size(girl,1)*fx);
Np = floor(size(girl,2)*fy);
smallimage(:,:,1) = zeros(Mp,Np);
smallimage(:,:,2) = zeros(Mp,Np);
smallimage(:,:,3) = zeros(Mp,Np);
for i=0:(Mp–1);
for j=0:(Np–1);
for x =floor(i/fx):ceil((i+1)/fx)-1;
for y =floor(j/fy):ceil((j+1)/fy)-1;
ival = girl(x+1,y+1,:);
if (x lessthan i/fx);
ival=ival*(1+x–(i/fx));
end;
if ((x+1)>((i +1)/fx));
ival=ival*(1–(x+1)+((i+1)/fx));
end;
if (y < (j/fy)); ival=ival*(1+y–(j/fy)); end; if ((y+1)>((j+1)/fy));
ival=ival*(1–(y+1)+((j+1)/fy));
end;
smallimage(i+1,j+1,:)=smallimage(i+1,j+1,:)+ival;
end;
end;
smallimage(i+1,j+1,:)=smallimage(i+1,j+1,:)/(1/(fx*fy))^2;
end;
end;
endfunction;

to show image:
A=imread("girl.jpg");
B=modify(A,fx,fy);
C=double(B)/255;
imwrite("girl1a.jpg",C(:,:,1),C(:,:,2),C(:,:,3))

Comparing the original and Scaled image (fx=1 and fy=0.5).




fx=0.5 and fy=1



fx=0.2 and fy=0.8




2. To enlarge colour images by the 'nearest neighbour' method, we use the same commands as those in Assign#7. However, to enlarge images, the f factor for my commands must be greater than 1.

function [smallimage]=enlarge(pic,f);
Mp = floor(size(pic,1)*f);
Np = floor(size(pic,2)*f);
smallimage(:,:,1) = zeros(Mp-1,Np-1);
smallimage(:,:,2) = zeros(Mp-1,Np-1);
smallimage(:,:,3) = zeros(Mp-1,Np-1);
for i=0:(Mp-1);
for j=0:(Np-1);
a=floor((i/f)+1);
b=floor((j/f)+1);
if (a>0)&(a<(size(pic,1)))&(b>0)&(b<(size(pic,2)));
smallimage(i+1,j+1,:)=pic(a,b,:);
end;
end;
end;
endfunction;

To show enlarged image, follow these commands.
A=imread("girl.jpg");
B=enlarge(A,f);
C=double(B)/255;
imwrite("girl2.jpg",C(:,:,1),C(:,:,2),C(:,:,3))

3. To enlarge an image using bilinear interpolation, I found that I could use the same command as in Assign#7 but keeping in mind to enlarge an image, my f factor must be greater than one.

function [smallimage]=enlarge(girl,f);
girl=double(imread("girl.jpg"));
Mp = floor(size(girl,1)*f);
Np = floor(size(girl,2)*f);
smallimage(:,:,1) = zeros(Mp-1,Np-1);
smallimage(:,:,2) = zeros(Mp-1,Np-1);
smallimage(:,:,3) = zeros(Mp-1,Np-1);
for i = 0:(Mp-1);
for j = 0:(Np-1);
a = i/f;
b = j/f;
r = floor(a);
s = floor(b);
if (r>0)&(r<256)&(s>0)&(s<256); k="1:3;" a="imread(" b="enlarge(A,f);" c="double(B)/255;" f ="2.5." onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNKsdwrWa8UvZnGTjdKy76bdxB40fVu4qE3YYDKQPp1TSYGfFqL5qboRSWFRdJvX3FoxU2fIYOufQUsNy_mcIz7awiPDTZMuJY0Tb07lBFZdjBo-6RzDovDxoXojxa_yijiEgT1kSp3qA/s1600-h/girl.jpg">

Assignment #7

Method 2 - Nearest Neighbour

function [smallimage]=shrink2(girl,f);
Mp = floor(size(girl,1)*f);
Np = floor(size(girl,2)*f);
smallimage(:,:,1) = zeros(Mp-1,Np-1);
smallimage(:,:,2) = zeros(Mp-1,Np-1);
smallimage(:,:,3) = zeros(Mp-1,Np-1);
for i=0:(Mp-1);
for j=0:(Np-1);
a=floor((i/f)+1);
b=floor((j/f)+1);
if (a>0)&(a<(size(girl,1)))&(b>0)&(b<(size(girl,2)));
smallimage(i+1,j+1,:)=girl(a,b,:);
end;
end;
end;
endfunction;

To show the image on Octave, I used these commands:
A=imread("girl.jpg");
B=shrink2(A,f);
C=double(B)/255;
imwrite("girl75.jpg",C(:,:,1),C(:,:,2),C(:,:,3))

Using this command, you can differ the f factor and receive these scale reductions.
f=0.75 f=0.25 f=0.1


The same was done for the other two pictures.





Method 3 - Bilinear Interpolation
function [smallimage]=shrink3(girl,f);
girl=double(imread("girl.jpg"));
Mp = floor(size(girl,1)*f);
Np = floor(size(girl,2)*f);
smallimage(:,:,1) = zeros(Mp-1,Np-1);
smallimage(:,:,2) = zeros(Mp-1,Np-1);
smallimage(:,:,3) = zeros(Mp-1,Np-1);
for i = 0:(Mp-1);
for j = 0:(Np-1);
a = i/f;
b = j/f;
r = floor(a);
s = floor(b);
if (r greaterthan 0)&(r lessthan256)&(s greaterthan 0)&(s lessthan256);
for k=1:3;
smallimage(i,j,k) = [1–a+r,a–r]*[girl(r,s,k), girl(r,s+1,k); girl(r+1,s,k),girl(r+1,s+1,k)]*[1–b+s;b–s];
end;
end;
end;
end;
endfunction;



The next two pics and their f factor shrink.





Method 1 : Average over all pixels.

function [smallimage]=shrink1(girl,f);
girl=double(imread("girl.jpg"));
Mp = floor(size(girl,1)*f);
Np = floor(size(girl,2)*f);
smallimage(:,:,1) = zeros(Mp,Np);
smallimage(:,:,2) = zeros(Mp,Np);
smallimage(:,:,3) = zeros(Mp,Np);
for i = 0 : (Mp – 1);
for j = 0 : (Np – 1);
for x =floor(i/f): ceil((i + 1)/f)-1;
for y =floor(j/f): ceil((j + 1)/f)-1;
ival=girl(x+1,y+1,:);
if (x lessthan (i/f));
ival=ival*(1+x–(i/f));
end;
if ((x + 1) > ((i + 1)/f));
ival = ival * (1 – (x + 1)+((i + 1)/f));
end;
if (y < (j/f)); ival = ival * (1 + y – (j/f)); end; if ((y + 1) > ((j + 1)/f));
ival = ival * (1 – (y + 1)+((j + 1)/f));
end;
smallimage(i+1,j+1,:) = smallimage(i+1,j+1,:) + ival;
end;
end;
smallimage(i+1,j+1,:)=smallimage(i+1,j+1,:)/(1/f)^2;
end;
end;
endfunction;

to show image:
A=imread('picturename.jpg');
B=shrink1(A,f);
C=double(B)/255;
imwrite("end.jpg",C(:,:,1),C(:,:,2),C(:,:,3))

These are the pics of "girl.jpg" with f factors 0.75, 0.25, and 0.10 respectively.



Here are the two other pics with their shrink factors using the average method.



Wednesday, June 11, 2008

Assignment #5 Q3

3.a) bigT=255*ones(256);
bigT(30:79,64:191)=zeros(50,128);
bigT(50:199,111:146)=zeros(150, 36);
for x = 1:256;
for y = 1:256 ;
newy=rem(2*x+y,256)+1;
bigTskew(x,y)=bigT(x,newy);
end;
end;
imshow(bigTskew);



b) rotate image by 3pi/4 degrees.
bigT=255*ones(256);
bigT(30:79,64:191)=zeros(50,128);
bigT(50:199,111:146)=zeros(150, 36);
for x = 1:256
for y = 1:256
newy=rem(2*x+y,256)+1;
bigTskew(x,y)=bigT(x,newy);
end;
end;
for x=1:256;
for y=1:256;
bigTskewrotate(x,y)=bigTskew(mod(round(x*cos(3*pi/4)-y*sin(3*pi/4)),256)+1, mod(round(x*sin(3*pi/4)+y*cos(3*pi/4)),256)+1);
end;
end;
imshow(bigTskewrotate);

Monday, June 9, 2008

Assignment #6

2. Create a 256x256 matrix with 1s in the (i, i+1) position and zeros elsewhere using the functions tril(M,k) and triu (M,k).

Octave command:
tril(ones(9),1) - tril(ones(9),0)
ans =



or...

triu(ones(9),1) - triu(ones(9),2)
ans =





3.a) A(:,:,1)=255*ones(100);
A(:,:,2)=255*tril(ones(100));
A(:,:,3)=zeros(100);
imshow(A);



B=double(A)/255;
size(B)

{answer: ans = 100 100 3}

sum(sum(B))/(100*100)

{answer: ans(:,:,1)=1}
ans(:,:,2)=0.505
ans(:,:,3)=0

B=ones(100);
B(:,:,1)=ones(100);
B(:,:,2)=0.505*ones(100);
B(:,:,3)=zeros(100);
imshow(B);
As a result, this is the average colour.






c)Pic 1

A=imread("cocochanel.jpg");
B=double(A)/255;
size(B)
{ans. 74 150 3}

sum(sum(B))/(74*150)

{ans. ans(:,:,1)=0.64490}
ans(:,:,2)=0.54085
ans(:,:,3)=0.63316

C=ones(256);
C(:,:,1)=0.64490*ones(256);
C(:,:,2)=0.54085*ones(256);
C(:,:,3)=0.63316*ones(256);
imshow(C);

This picture's average colour is...



Pic 2


A=imread("recycle.jpg");
B=double(A)/255;
size(B)
{ans. 153 104 3}

sum(sum(B))/(153*104)

{ans. ans(:,:,1)=0.052669}
ans(:,:,2)=0.32379
ans(:,:,3)=0.28915

C=ones(256);
C(:,:,1)=0.052669*ones(256);
C(:,:,2)=0.32379*ones(256);
C(:,:,3)=0.28915*ones(256);
imshow(C);

This picture's average colour is ...