Thursday, June 19, 2008

Assign 9, Q5 - And Photomosaic start...

Here we had to change spidey to a greyscale image with only 16 intensity values.

I thought that the image would be even more grainy...however the new 16 intensity values greyscale image had more detail. Perhaps because there are now less intensity values to choose from and so not much shading could take place but more detail in certain areas can now be seen.



The lessening of intensity values with grey scale images and the 'apparent increasing' of detail seems to work "in reverse" for colour images as can be noted in my comments of Assignment9 Question 1 (below).

Here is the start of my photomosaic...

I tried to use the spiderman image but when I shrunk it, reduced the colours, and enlarged it...the image was too distorted.



So I used a different picture...the ATARI icon.

Step 1:




step 2:




Step 3:



Step 4 & 5:

I used Photoshop to adjust the image size via pixels because I had to 'crop'/grap the 'small pictures' I wanted from a screen grab. I want to use the pac-man ghosts and ms. pac-man as my small pictures.












Here is part of my octave code:

# step 1 - shrink image - the size of the original is 200 by 200 pixels...

function [smallimage]=shrink2(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 image which was scaled down by factor f = 0.1, the resulting image will be 20 by 20 pixels.
A=imread("logo.png");
B=shrink2(A,.1);
C=double(B)/255;
imwrite("logo1.png",C(:,:,1),C(:,:,2),C(:,:,3))

# step 2 - reduce colour to 27...

A=imread("logo1.png");
B = floor(double(A)/86)*86+42;
imwrite("logo2.png",double(B)(:,:,1)/255, double(B)(:,:,2)/255, double(B)(:,:,3)/255);

# step 3 - enlarge...

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;

A=imread("logo2.png");
B=enlarge(A,20);
C=double(B)/255;
imwrite("logo3.png",C(:,:,1),C(:,:,2),C(:,:,3));


# Put pixels in...

a=imread("logo3.png");
p=imread("pink.png");
r=imread("red.png");
y=imread("mspac.png");

# Pink...(column, row, :) pixel coords...

a(140:159,300:319,:)=p;
a(160:179,300:319,:)=p;
a(180:199,300:319,:)=p;
a(200:219,300:319,:)=p;

a(140:159,320:339,:)=p;
a(160:179,320:339,:)=p;
a(180:199,320:339,:)=p;
a(200:219,320:339,:)=p;



a(140:159,380:399,:)=p;
a(160:179,380:399,:)=p;
a(180:199,380:399,:)=p;
a(200:219,380:399,:)=p;

a(140:159,400:419,:)=p;
a(160:179,400:419,:)=p;
a(180:199,400:419,:)=p;
a(200:219,400:419,:)=p;

a(140:159,420:439,:)=p;
a(160:179,420:439,:)=p;
a(180:199,420:439,:)=p;
a(200:219,420:439,:)=p;



a(140:159,480:499,:)=p;
a(160:179,480:499,:)=p;
a(180:199,480:499,:)=p;
a(200:219,480:499,:)=p;

a(140:159,500:519,:)=p;
a(160:179,500:519,:)=p;
a(180:199,500:519,:)=p;
a(200:219,500:519,:)=p;


# red pixels coords...

a(80:99,300:319,:)=r;
a(80:99,320:339,:)=r;

a(80:99,380:399,:)=r;
a(80:99,400:419,:)=r;
a(80:99,420:439,:)=r;

a(80:99,480:499,:)=r;
a(80:99,500:519,:)=r;


# yellow pixels coords...

a(600:619,160:179,:)=y;
a(600:619,180:199,:)=y;
a(600:619,200:219,:)=y;
a(600:619,220:239,:)=y;
a(600:619,240:259,:)=y;

a(620:639,120:139,:)=y;
a(620:639,140:159,:)=y;
a(620:639,160:179,:)=y;
a(620:639,180:199,:)=y;
a(620:639,200:219,:)=y;
a(620:639,220:239,:)=y;

a(640:659,80:99,:)=y;
a(640:659,100:119,:)=y;
a(640:659,120:139,:)=y;
a(640:659,140:159,:)=y;
a(640:659,160:179,:)=y;
a(640:659,180:199,:)=y;
a(640:659,200:219,:)=y;


a(660:679,80:99,:)=y;
a(660:679,100:119,:)=y;
a(660:679,120:139,:)=y;
a(660:679,140:159,:)=y;
a(660:679,160:179,:)=y;
a(660:679,180:199,:)=y;


a(600:619,380:399,:)=y;
a(620:639,380:399,:)=y;
a(640:659,380:399,:)=y;
a(660:679,380:399,:)=y;

a(600:619,400:419,:)=y;
a(620:639,400:419,:)=y;
a(640:659,400:419,:)=y;
a(660:679,400:419,:)=y;

a(600:619,420:439,:)=y;
a(620:639,420:439,:)=y;
a(640:659,420:439,:)=y;
a(660:679,420:439,:)=y;


f=double(a)/255;
imwrite("atari5.png",f(:,:,1),f(:,:,2),f(:,:,3));

here is the resulting mosaic to date:

Assign 9, Q4

Here we had to change the spidey image into a greyscale of only 64 intensity values...here is where the image ceases to look cool...

Assign 9, Q3

Change spidey into a greyscale image...

Assign 9, Q2

Will now convert the spidey image to contain only 27 colours.

Here are the original, 64, and 27 colour images.






As you can guess...with fewer colours to represent the image, the less detail and shading is apparent in comparison with the original image.

Assign 9, Q1

Really excited to try the photomosaic so will be using new images for this assignment...just want to see how they turn out.

Here is the original image of our friendly neighbourhood spider-man (okay he doesn't look THAT friendly).



Here is the resulting image after we have converted the image to only 64 colours.



way cool!

Assign 8, Q5

Here I used the nearest neighbour approximation function that I used in assignment #7 and applied it 4 times (f=0.8, 0.6, 0.4, 0.2) the large image just remains the same at f=1 or the original sized image.

Then layered the pics one on top of the other.

octave:10> a=imread("start.jpg");
octave:11> b=shrinkimg(a,0.8);
octave:12> c=shrinkimg(a,0.6);
octave:13> d=shrinkimg(a,0.4);
octave:14> e=shrinkimg(a,0.2);
octave:15> a1=double(a)/255;
octave:16> b1=double(b)/255;
octave:17> c1=double(c)/255;
octave:18> d1=double(d)/255;
octave:19> e1=double(e)/255;
octave:20> a1(52:459,78:690,:)=b1;
octave:21> a1(103:408,155:613,:)=c1;
octave:22> a1(155:357,231:536,:)=d1;
octave:23> a1(206:306,308:459,:)=e1;
octave:24> f=double(a1)/255;
octave:25> imwrite("nstart.jpg", f(:,:,1),f(:,:,2),f(:,:,3));
octave:26> imwrite("n1start.jpg",a1(:,:,1),a1(:,:,2),a1(:,:,3));

In a new terminal you can imshow:

a=imread("n1start.jpg");
b=double(a)/255;
imshow(b);

Here is the image:

Wednesday, June 18, 2008

Assign 8, Q2

Use nearest neighbour approximation (n.a.) to 'enlarge' a colour image using f=1/f.

Note that I could change my n.a. function from f's to 1/f's or I could just take an 'f' and plug it into 1/f.

Ex: f=0.5=1/2 so 1/f=1/(1/2)=2.

Therefore I can use my same n.a. function used in assignment #7, Q2, but use values of f>1.

Here is how f=1.2 looks like:

Assign 8, Q1c

Similarly for fx=0.2 and fy=0.8...we use the function written before and enter our new fx and fy values as follows:

c=shrinkimg(b,0.2,0.8);

# note that we don't have to imwrite and double our image again as we have already done this.

The resulting image is:



and is very dark and grainy.

Assign 8, Q1b

Using the function written already and fx=0.5, fy=1...

I get the following modified image by typing:

a=imread("ppic1.jpg");
b=double(a)/255;
c=shrinkimg(b,0.5,1);

Then usual imwrite and imshow process gives you:

Assign 8, Q1

Here we had to try and shrink the image along the 'x' and 'y' axis using different values of 'f' (the shrink factor). In this case I did fx=1 and fy=0.5.

Here are my octave commands:

# note that "lt"= less than and "gt"=greater than...

function s=shrinkimg(img,fx,fy);
xp=floor(size(img,1)*fx);
yp=floor(size(img,2)*fy);
s(:,:,1)=zeros(xp,yp);
s(:,:,2)=zeros(xp,yp);
s(:,:,3)=zeros(xp,yp);
for i=0:(xp-1);
for j=0:(yp-1);
for x=floor(i/fx):ceil((i+1)/fx)-1;
for y=floor(j/fy):ceil((j+1)/fy)-1;
ival=img(x+1,y+1,:)
if(x"lt"i/fx);
ival=ival*(1-i/fx+x);
end;
if (x+1"gt"(i+1)/fx);
ival=ival*(1-(i+1)/fx+x+1);
end;
if (y"lt"i/fy);
ival=ival*(1-j/fy+y);
end;
if (y+1"gt"(j+1)/fy);
ival=ival*(1-(y+1)+(j+1)/fy);
end;
s(i+1,j+1,:)=s(i+1,j+1,:)+ival;
end;
end;
s(i+1,j+1,:)=s(i+1,j+1,:)/((1/(fx*fy))^2);
end;
end;
endfunction;


Now since I am using a function...all I need to enter on the following lines of octave is:

a=imread("imagename.jpg");
b=double(a)/255;
c=shrinkimg(b,1,0.5);
imwrite("out.jpg", c(:,:,1),c(:,:,2),c(:,:,3));

open new terminal:

a=imread("out.jpg");
b=double(a)/255;
imshow(b);

The resulting image is the following:

Assign 7, Q1-method1

Finally got method 1 to work. Please see octave code below and resulting images...