Step 1
c:\Users\admin\pictures
A= imread("baby.jpg");
size(A)
124 124 3
Step 2
Reduce 12 x 12 (dividing by 10)
by using program
Step3
reduce to 27 colors
Sunday, July 6, 2008
Friday, July 4, 2008
Rose_ Mosaic
Step(1)
Read the image and find the size
>> cd c:\Users\admin\pictures
>> A=imread('rose.jpg');
>> image(A), axis off
size(A)
ans =
500 500 3
Step(2)
Reduce the image to 10 X 10
Dividing by 50
Shrink it down to a 34 by 34 image(dividing the original image by 10)
Octave code as follows:
function Z=zoom2(limg,scale)
rows=floor(size(limg,1)*scale);
cols=floor(size(limg,2)*scale);
for i=1:rows;
for j=1:cols;
a=round(i/scale);
b=round(j/scale);
Z(i,j,:)=limg(a,b,:);
end;
end;
endfunction
cd c:\users\admin\pictures
B=imread("dog.jpg");
B=double(B)/255;
B=zoom2(B,1/10);
imwrite("1/10dog.jpg",B(:,:,1),B(:,:,2),B(:,:,3));
size(B);
10 x 10
Step(3)
Reduce the colors to 27 of the reduced image
function B=draw27(A)
A=double(A);
for i=1:size(A,1);
for j=1:size(A,2);
for k=1:3
B(i,j,k)=floor(A(i,j,k)/86)*86+42;
endfor;
endfor;
endfor;
endfunction
cd c:\users\admin\pictures
A=imread("reduceddog.jpg");
B=draw27(A);
C=double(B)/255;
imwrite("dog27.jpg",C(:,:,1),C(:,:,2),C(:,:,3));
Step(4)
Stretch the 27 color image 50 times
function Z=stretch2(simg,scale)
original_rows=size(simg,1);
original_cols=size(simg,2);
rows=floor(size(simg,1)*scale);
cols=floor(size(simg,2)*scale);
for i=0:(rows-1);
for j=0:(cols-1);
a=floor((i/scale)+1);
b=floor((j/scale)+1);
if (a>0) & (a<=original_rows) & (b>0) & (b<=original_cols) Z(i+1,j+1,:)= simg(a,b,:); end; end; end; endfunction cd c:\Users\admin\pictures A=imread("rose27.jpg"); B=stretch2(A,50); size(B) 500 500 3 C=double(B)/255; imwrite("strechedrose.jpg",C(:,:,1),C(:,:,2),C(:,:,3))
Step(5)
Read the image and find the size
>> cd c:\Users\admin\pictures
>> A=imread('rose.jpg');
>> image(A), axis off
size(A)
ans =
500 500 3
Step(2)
Reduce the image to 10 X 10
Dividing by 50
Shrink it down to a 34 by 34 image(dividing the original image by 10)
Octave code as follows:
function Z=zoom2(limg,scale)
rows=floor(size(limg,1)*scale);
cols=floor(size(limg,2)*scale);
for i=1:rows;
for j=1:cols;
a=round(i/scale);
b=round(j/scale);
Z(i,j,:)=limg(a,b,:);
end;
end;
endfunction
cd c:\users\admin\pictures
B=imread("dog.jpg");
B=double(B)/255;
B=zoom2(B,1/10);
imwrite("1/10dog.jpg",B(:,:,1),B(:,:,2),B(:,:,3));
size(B);
10 x 10
Step(3)
Reduce the colors to 27 of the reduced image
function B=draw27(A)
A=double(A);
for i=1:size(A,1);
for j=1:size(A,2);
for k=1:3
B(i,j,k)=floor(A(i,j,k)/86)*86+42;
endfor;
endfor;
endfor;
endfunction
cd c:\users\admin\pictures
A=imread("reduceddog.jpg");
B=draw27(A);
C=double(B)/255;
imwrite("dog27.jpg",C(:,:,1),C(:,:,2),C(:,:,3));
Step(4)
Stretch the 27 color image 50 times
function Z=stretch2(simg,scale)
original_rows=size(simg,1);
original_cols=size(simg,2);
rows=floor(size(simg,1)*scale);
cols=floor(size(simg,2)*scale);
for i=0:(rows-1);
for j=0:(cols-1);
a=floor((i/scale)+1);
b=floor((j/scale)+1);
if (a>0) & (a<=original_rows) & (b>0) & (b<=original_cols) Z(i+1,j+1,:)= simg(a,b,:); end; end; end; endfunction cd c:\Users\admin\pictures A=imread("rose27.jpg"); B=stretch2(A,50); size(B) 500 500 3 C=double(B)/255; imwrite("strechedrose.jpg",C(:,:,1),C(:,:,2),C(:,:,3))
Step(5)
Photo mosaic project (3)
Step (1)Read the picture and find the size
>> cd c:\Users\admin\pictures
>> A=imread('dog.jpg');
>> image(A),axis off
size(A)
ans =
340 340 3
Step(2)
Shrink it down to a 34 by 34 image(dividing the original image by 10)
Octave code as follows:
function Z=zoom2(limg,scale)
rows=floor(size(limg,1)*scale);
cols=floor(size(limg,2)*scale);
for i=1:rows;
for j=1:cols;
a=round(i/scale);
b=round(j/scale);
Z(i,j,:)=limg(a,b,:);
end;
end;
endfunction
cd c:\users\admin\pictures
B=imread("dog.jpg");
B=double(B)/255;
B=zoom2(B,1/10);
imwrite("1/10dog.jpg",B(:,:,1),B(:,:,2),B(:,:,3));
size(B);
34 34 3
Step(3)
Reduce the image in step 2 to 27 colors
The octave commands are as follows:
function B=draw27(A)
A=double(A);
for i=1:size(A,1);
for j=1:size(A,2);
for k=1:3
B(i,j,k)=floor(A(i,j,k)/86)*86+42;
endfor;
endfor;
endfor;
endfunction
cd c:\users\admin\pictures
A=imread("reduceddog.jpg");
B=draw27(A);
C=double(B)/255;
imwrite("dog27.jpg",C(:,:,1),C(:,:,2),C(:,:,3));
Step(4)
function Z=stretch2(simg,scale)
original_rows=size(simg,1);
original_cols=size(simg,2);
rows=floor(size(simg,1)*scale);
cols=floor(size(simg,2)*scale);
for i=0:(rows-1);
for j=0:(cols-1);
a=floor((i/scale)+1);
b=floor((j/scale)+1);
if (a>0) & (a<=original_rows) & (b>0) & (b<=original_cols) Z(i+1,j+1,:)= simg(a,b,:); end; end; end; endfunction cd c:\Users\admin\pictures A=imread("dog27.jpg"); B=stretch2(A,10); size(B) 340 340 3 C=double(B)/255; imwrite("strecheddog.jpg",C(:,:,1),C(:,:,2),C(:,:,3))
Starting over: First reducing the original image into 300 x 300 size The new picture is like this:
---------------------------------------------------------------------------------------
Step(1)
cd c:\Users\admin\pictures
imread("newdog.jpg");
Step(2)
function Z=zoom2(limg,scale)
rows=floor(size(limg,1)*scale);
cols=floor(size(limg,2)*scale);
for i=1:rows;
for j=1:cols;
a=round(i/scale);
b=round(j/scale);
Z(i,j,:)=limg(a,b,:);
end;
end;
endfunctionoctave-3.0.1.exe:10> cd c:\users\admin\pictures
octave-3.0.1.exe:11> B=imread("newdog.jpg");
octave-3.0.1.exe:12> B=double(B)/255;
octave-3.0.1.exe:13> B=zoom2(B,1/10);
octave-3.0.1.exe:14> imwrite("1/reducednewdog.jpg",B(:,:,1),B(
octave-3.0.1.exe:15> size(B);
octave-3.0.1.exe:16> size(B)
ans =
30 30 3
Step (3)
27 color reduction of the above 30 x 30 image
function B=draw27(A)
A=double(A);
for i=1:size(A,1);
for j=1:size(A,2);
for k=1:3
B(i,j,k)=floor(A(i,j,k)/86)*86+42;
endfor;
endfor;
endfor;
endfunction
cd c:\users\admin\pictures
A=imread("reducednewdog.jpg");
B=draw27(A);
C=double(B)/255;
imwrite("newdog27.jpg",C(:,:,1),C(:,:,2),C(:,:,3));
Step(4)
Stretched the image in step (3) 10 times:
function Z=stretch2(simg,scale)
original_rows=size(simg,1);
original_cols=size(simg,2);
rows=floor(size(simg,1)*scale);
cols=floor(size(simg,2)*scale);
for i=0:(rows-1);
for j=0:(cols-1);
a=floor((i/scale)+1);
b=floor((j/scale)+1);
if (a>0) & (a<=original_rows) & (b>0) & (b<=original_cols) Z(i+1,j+1,:)= simg(a,b,:); end; end; end; endfunction cd c:\Users\admin\pictures A=imread("newdog27.jpg"); B=stretch2(A,10); size(B) 300 300 3 C=double(B)/255; imwrite("strechednewdog.jpg",C(:,:,1),C(:,:,2),C(:,:,3)) Pictures and their average colors
My original picture is 300x300 and now after shrinking to 30x30, changing to 27 colours, and then enlarging it back to 300x300, there are 30 square "pixels" along the top of the 300x300 picture. Therefore each 'square' is 10x10. So, I will need to resize all my 'small average colour pictures' to a 10x10 size.
If you want to put the rose over the dogs 'right eye', you will need to enter the following code.
# Put pixels into octave...
a=imread("strechednewdog.jpg");
r=imread("pixelrose.png");
# You will need to 'imread' each 10x10 picture you want to use in your mosaic above...
# Now, let's move the rose icon over the dog's right eye...
# Note that the coordinates are (y-axis, x-axis,:)...
a(110:119,90:99,:)=r
# How do you get these numbers????
# start at the dog's eye and start counting all the way to the top of the picture...you should get 11...so each pixel square is 10x10...therefore your starting coordinate for the coloumn would be 11x10=110. Your finishing coordinate for the column would be x+9...always...in this case it is 110+9=119.
# similarly, for the rows, I start at the dogs eye. Start counting to the left of the picture and you should get 9 squares. Therefore 9x10=90 is your starting coordinate for the rows and your finishing coordinate is 90+9=99...
# to see the image...
f=double(a)/255;
imwrite("dogtest.png",f(:,:,1),f(:,:,2),f(:,:,3));
Here is how the image test looks like:
cd c:\Users\admin\pictures
a=imread("newdogstretched");
ctave-3.0.1.exe:19> a=imread("newdogstretched.jpg");
ctave-3.0.1.exe:20> r2=imread("p2new.jpg");
ctave-3.0.1.exe:21> a(110:119,90:99,:)=r2;
ctave-3.0.1.exe:22> a(110:119,140:149,:)=r2;
ctave-3.0.1.exe:23> a(110:119,130:139,:)=r2;
ctave-3.0.1.exe:24> a(150:159, 100:109,:)=r2;
ctave-3.0.1.exe:25> a(150:159, 110:119,:)=r2;
ctave-3.0.1.exe:26> a(150:159, 120:129,:)=r2;
ctave-3.0.1.exe:27> a(100:110, 140:149,:)=r2;
ctave-3.0.1.exe:27> a(100:109, 140:149,:)=r2;
ctave-3.0.1.exe:28> a(100:109, 150:159,:)=r2;
ctave-3.0.1.exe:29> a(50:59, 0:9,:)=r2;
ctave-3.0.1.exe:29> a(50:59, 00:09,:)=r2;
ctave-3.0.1.exe:29> a(20:29,30:39,:)=r2;
ctave-3.0.1.exe:30> a(60:69,20:29,:)=r2;
ctave-3.0.1.exe:33> a(50:59,180:189,:)=r2;
ctave-3.0.1.exe:34> a(50:59,170:179,:)=r2;
ctave-3.0.1.exe:35> a(50:59, 90:99,:)=r2;
ctave-3.0.1.exe:36> a(60:69,170:179,:)=r2;
ctave-3.0.1.exe:37> a(200:209,10:19,:)=r2;
ctave-3.0.1.exe:38> a(30:39,50:59,:)=r2;
ctave-3.0.1.exe:39> a(40:49,40:49,:)=r2;
ctave-3.0.1.exe:31> f=double(a)/255;
ctave-3.0.1.exe:32> imwrite("dogtest.jpg",f(:,:,1),f(:,:,2),f(:,:,3));
>> cd c:\Users\admin\pictures
>> A=imread('dog.jpg');
>> image(A),axis off
size(A)
ans =
340 340 3
Step(2)
Shrink it down to a 34 by 34 image(dividing the original image by 10)
Octave code as follows:
function Z=zoom2(limg,scale)
rows=floor(size(limg,1)*scale);
cols=floor(size(limg,2)*scale);
for i=1:rows;
for j=1:cols;
a=round(i/scale);
b=round(j/scale);
Z(i,j,:)=limg(a,b,:);
end;
end;
endfunction
cd c:\users\admin\pictures
B=imread("dog.jpg");
B=double(B)/255;
B=zoom2(B,1/10);
imwrite("1/10dog.jpg",B(:,:,1),B(:,:,2),B(:,:,3));
size(B);
34 34 3
Step(3)
Reduce the image in step 2 to 27 colors
The octave commands are as follows:
function B=draw27(A)
A=double(A);
for i=1:size(A,1);
for j=1:size(A,2);
for k=1:3
B(i,j,k)=floor(A(i,j,k)/86)*86+42;
endfor;
endfor;
endfor;
endfunction
cd c:\users\admin\pictures
A=imread("reduceddog.jpg");
B=draw27(A);
C=double(B)/255;
imwrite("dog27.jpg",C(:,:,1),C(:,:,2),C(:,:,3));
Step(4)
function Z=stretch2(simg,scale)
original_rows=size(simg,1);
original_cols=size(simg,2);
rows=floor(size(simg,1)*scale);
cols=floor(size(simg,2)*scale);
for i=0:(rows-1);
for j=0:(cols-1);
a=floor((i/scale)+1);
b=floor((j/scale)+1);
if (a>0) & (a<=original_rows) & (b>0) & (b<=original_cols) Z(i+1,j+1,:)= simg(a,b,:); end; end; end; endfunction cd c:\Users\admin\pictures A=imread("dog27.jpg"); B=stretch2(A,10); size(B) 340 340 3 C=double(B)/255; imwrite("strecheddog.jpg",C(:,:,1),C(:,:,2),C(:,:,3))
Starting over: First reducing the original image into 300 x 300 size The new picture is like this:
---------------------------------------------------------------------------------------
Step(1)
cd c:\Users\admin\pictures
imread("newdog.jpg");
Step(2)
function Z=zoom2(limg,scale)
rows=floor(size(limg,1)*scale);
cols=floor(size(limg,2)*scale);
for i=1:rows;
for j=1:cols;
a=round(i/scale);
b=round(j/scale);
Z(i,j,:)=limg(a,b,:);
end;
end;
endfunctionoctave-3.0.1.exe:10> cd c:\users\admin\pictures
octave-3.0.1.exe:11> B=imread("newdog.jpg");
octave-3.0.1.exe:12> B=double(B)/255;
octave-3.0.1.exe:13> B=zoom2(B,1/10);
octave-3.0.1.exe:14> imwrite("1/reducednewdog.jpg",B(:,:,1),B(
octave-3.0.1.exe:15> size(B);
octave-3.0.1.exe:16> size(B)
ans =
30 30 3
Step (3)
27 color reduction of the above 30 x 30 image
function B=draw27(A)
A=double(A);
for i=1:size(A,1);
for j=1:size(A,2);
for k=1:3
B(i,j,k)=floor(A(i,j,k)/86)*86+42;
endfor;
endfor;
endfor;
endfunction
cd c:\users\admin\pictures
A=imread("reducednewdog.jpg");
B=draw27(A);
C=double(B)/255;
imwrite("newdog27.jpg",C(:,:,1),C(:,:,2),C(:,:,3));
Step(4)
Stretched the image in step (3) 10 times:
function Z=stretch2(simg,scale)
original_rows=size(simg,1);
original_cols=size(simg,2);
rows=floor(size(simg,1)*scale);
cols=floor(size(simg,2)*scale);
for i=0:(rows-1);
for j=0:(cols-1);
a=floor((i/scale)+1);
b=floor((j/scale)+1);
if (a>0) & (a<=original_rows) & (b>0) & (b<=original_cols) Z(i+1,j+1,:)= simg(a,b,:); end; end; end; endfunction cd c:\Users\admin\pictures A=imread("newdog27.jpg"); B=stretch2(A,10); size(B) 300 300 3 C=double(B)/255; imwrite("strechednewdog.jpg",C(:,:,1),C(:,:,2),C(:,:,3)) Pictures and their average colors
My original picture is 300x300 and now after shrinking to 30x30, changing to 27 colours, and then enlarging it back to 300x300, there are 30 square "pixels" along the top of the 300x300 picture. Therefore each 'square' is 10x10. So, I will need to resize all my 'small average colour pictures' to a 10x10 size.
If you want to put the rose over the dogs 'right eye', you will need to enter the following code.
# Put pixels into octave...
a=imread("strechednewdog.jpg");
r=imread("pixelrose.png");
# You will need to 'imread' each 10x10 picture you want to use in your mosaic above...
# Now, let's move the rose icon over the dog's right eye...
# Note that the coordinates are (y-axis, x-axis,:)...
a(110:119,90:99,:)=r
# How do you get these numbers????
# start at the dog's eye and start counting all the way to the top of the picture...you should get 11...so each pixel square is 10x10...therefore your starting coordinate for the coloumn would be 11x10=110. Your finishing coordinate for the column would be x+9...always...in this case it is 110+9=119.
# similarly, for the rows, I start at the dogs eye. Start counting to the left of the picture and you should get 9 squares. Therefore 9x10=90 is your starting coordinate for the rows and your finishing coordinate is 90+9=99...
# to see the image...
f=double(a)/255;
imwrite("dogtest.png",f(:,:,1),f(:,:,2),f(:,:,3));
Here is how the image test looks like:
cd c:\Users\admin\pictures
a=imread("newdogstretched");
ctave-3.0.1.exe:19> a=imread("newdogstretched.jpg");
ctave-3.0.1.exe:20> r2=imread("p2new.jpg");
ctave-3.0.1.exe:21> a(110:119,90:99,:)=r2;
ctave-3.0.1.exe:22> a(110:119,140:149,:)=r2;
ctave-3.0.1.exe:23> a(110:119,130:139,:)=r2;
ctave-3.0.1.exe:24> a(150:159, 100:109,:)=r2;
ctave-3.0.1.exe:25> a(150:159, 110:119,:)=r2;
ctave-3.0.1.exe:26> a(150:159, 120:129,:)=r2;
ctave-3.0.1.exe:27> a(100:110, 140:149,:)=r2;
ctave-3.0.1.exe:27> a(100:109, 140:149,:)=r2;
ctave-3.0.1.exe:28> a(100:109, 150:159,:)=r2;
ctave-3.0.1.exe:29> a(50:59, 0:9,:)=r2;
ctave-3.0.1.exe:29> a(50:59, 00:09,:)=r2;
ctave-3.0.1.exe:29> a(20:29,30:39,:)=r2;
ctave-3.0.1.exe:30> a(60:69,20:29,:)=r2;
ctave-3.0.1.exe:33> a(50:59,180:189,:)=r2;
ctave-3.0.1.exe:34> a(50:59,170:179,:)=r2;
ctave-3.0.1.exe:35> a(50:59, 90:99,:)=r2;
ctave-3.0.1.exe:36> a(60:69,170:179,:)=r2;
ctave-3.0.1.exe:37> a(200:209,10:19,:)=r2;
ctave-3.0.1.exe:38> a(30:39,50:59,:)=r2;
ctave-3.0.1.exe:39> a(40:49,40:49,:)=r2;
ctave-3.0.1.exe:31> f=double(a)/255;
ctave-3.0.1.exe:32> imwrite("dogtest.jpg",f(:,:,1),f(:,:,2),f(:,:,3));
Thursday, July 3, 2008
Another Photomosaic
Step (1)
Picture(from Google search), saved on the disk and read with the following code.
Code in MATLAB
cd c:\users\admin\pictures
lion=imread('lionphotomosaic.jpg');
lion=double(lion)/255;
imshow(lion)
size (lion)
ans =
350 350 3
Step 2
Shrink it down to a 35 by 35 image(dividing the original image by 10)
function Z=zoom2(limg,scale)
rows=floor(size(limg,1)*scale);
cols=floor(size(limg,2)*scale);
for i=1:rows;
for j=1:cols;
a=round(i/scale);
b=round(j/scale);
Z(i,j,:)=limg(a,b,:);
end;
end;
endfunction
cd c:\users\admin\pictures
B=imread("lionphotomosaic.jpg");
B=double(B)/255;
B=zoom2(B,1/10);
imwrite("lion2mosaic.jpg",B(:,:,1),B(:,:,2),B(:,:,3));
size(B);
ans =
35 35 3
Original with all colors but reduced image of size 35 x 35
Step 3
Reduce it with only 27 color values.
Octave code is as follows( I will try to write in MATLAB too)
function B=draw27(A)
A=double(A);
for i=1:size(A,1);
for j=1:size(A,2);
for k=1:3
B(i,j,k)=floor(A(i,j,k)/86)*86+42;
endfor;
endfor;
endfor;
endfunction
cd c:\users\admin\pictures
A=imread("lion2mosaic.jpg")
lion27=draw27(A);
B=double(lion27)/255;
imwrite("lion3mosaic.jpg",B(:,:,1),B(:,:,2),B(:,:,3));
27 color and reduced image (of size 35 X 35)
Original size(not reduced) but 27 colors
Step 4
Enlarge(10 times) the image(reduced 27 colors) using the nearest neighbor approach.
Size of the image will be 350 x 350
Octave code is like this:
function Z=stretch2(simg,scale)
original_rows=size(simg,1);
original_cols=size(simg,2);
rows=floor(size(simg,1)*scale);
cols=floor(size(simg,2)*scale);
for i=0:(rows-1);
for j=0:(cols-1);
a=floor((i/scale)+1);
b=floor((j/scale)+1);
if (a>0) & (a<=original_rows) & (b>0) & (b<=original_cols) Z(i+1,j+1,:)= simg(a,b,:); end; end; end; endfunction cd c:\Users\admin\pictures A=imread("lion3mosaic.jpg"); B=stretch2(A,10); size(B) 350 350 3 C=double(B)/255; imwrite("strechedlionlion4.jpg",C(:,:,1),C(:,:,2),C(:,:,3))
Step 5
Find at least one picture whose average color is one of the 27 possible colors that appear in the picture.
Friday, June 27, 2008
Photo mosaic project
Following link may give you a little bit help
http://www.andreaplanet.com/andreamosaic/
It is a free software download and use
What we actually have to do to create a photo mosaic is as follows:
(1)Select a picture first(as a background picture)
(2)shrink it down to a 31 by 31 (or suitable size)image
(3) Reduce it with only 27 color values,
(4)Enlarge the image using the nearest neighbor approach
(5)Find at least one picture whose average color is one of the 27 possible colors
that appear in the picture
(6)Shrink those images so that they are the size of the 'pixels' in the enlarged picture
(7)Calculate the average colors of the smaller images,
(8) Sort them in an array so that for each color there is one image,
(9)Replace the 'pixels' in the big image with the smaller images.
Step (1)
Picture(from Google search), saved on the disk and read with the following code.
Code in MATLAB
cd c:\users\admin\pictures
pic=imread('catmosaic.jpg');
pic=double(pic)/255;
imshow(pic)
size (pic)
ans =
124 124 3
Step2
shrink it down to a 31 by 31 image
Octave Code(I am trying to write it in MATLAB too)
octave-3.0.1.exe:1>
octave-3.0.1.exe:1> function Z=zoom2(limg,scale)
> rows=floor(size(limg,1)*scale);
> cols=floor(size(limg,2)*scale);
> for i=1:rows;
> for j=1:cols;
> a=round(i/scale);
> b=round(j/scale);
> Z(i,j,:)=limg(a,b,:);
> end;
> end;
> endfunction
octave-3.0.1.exe:2> cd c:\users\admin\pictures
octave-3.0.1.exe:3> pic=imread('catmosaic.jpg');
octave-3.0.1.exe:4> pic=double(pic)/255;
octave-3.0.1.exe:5> pic=zoom2(pic,1/4);
octave-3.0.1.exe:6> imshow(pic)
octave-3.0.1.exe:7> size(pic)
ans =
31 31 3
Step 3
Reduce it with only 27 color values.
Octave code is as follows( I will try to write in MATLAB too)
octave-3.0.1.exe:8> function B=draw27(A)
> A=double(A);
> for i=1:size(A,1)
> for j=1:size(A,2)
> for k=1:3
> B(i,j,k)=floor(A(i,j,k)/86)*86+42;
> endfor
> endfor
> endfor
> endfunction
octave-3.0.1.exe:9> cd c:\users\admin\pictures
octave-3.0.1.exe:10> pic=imread('catmosaic.jpg')
octave-3.0.1.exe:11> pic27=draw27(pic);
octave-3.0.1.exe:12> imshow(double(pic27)/255)
Step 4
Enlarge(20 times) the image using the nearest neighbor approach.
Size of the image will be 620 x 620
Octave code is like this:
cols=floor(size(simg,2)*scale);
for i=0:(rows-1);
for j=0:(cols-1);
a=floor((i/scale)+1);
b=floor((j/scale)+1);
if (a>0) & (a<=original_rows) & (b>0) & (b<=original_cols)
Z(i+1,j+1,:)=simg(a,b,:);
end;
end;
end;
endfunction
cd c:\Users\admin\pictures
A=imread("pic27.jpg");
picstep3=stretch2(A,20);
size(picstep3);
B=double(picstep3)/255;
imwrite("strechedpic.jpg",B(:,:,1),B(:,:,2),B(:,:,3))
Step 5
Find at least one picture whose average color is one of the 27 possible colors that appear in the picture.
http://www.andreaplanet.com/andreamosaic/
It is a free software download and use
What we actually have to do to create a photo mosaic is as follows:
(1)Select a picture first(as a background picture)
(2)shrink it down to a 31 by 31 (or suitable size)image
(3) Reduce it with only 27 color values,
(4)Enlarge the image using the nearest neighbor approach
(5)Find at least one picture whose average color is one of the 27 possible colors
that appear in the picture
(6)Shrink those images so that they are the size of the 'pixels' in the enlarged picture
(7)Calculate the average colors of the smaller images,
(8) Sort them in an array so that for each color there is one image,
(9)Replace the 'pixels' in the big image with the smaller images.
Step (1)
Picture(from Google search), saved on the disk and read with the following code.
Code in MATLAB
cd c:\users\admin\pictures
pic=imread('catmosaic.jpg');
pic=double(pic)/255;
imshow(pic)
size (pic)
ans =
124 124 3
Step2
shrink it down to a 31 by 31 image
Octave Code(I am trying to write it in MATLAB too)
octave-3.0.1.exe:1>
octave-3.0.1.exe:1> function Z=zoom2(limg,scale)
> rows=floor(size(limg,1)*scale);
> cols=floor(size(limg,2)*scale);
> for i=1:rows;
> for j=1:cols;
> a=round(i/scale);
> b=round(j/scale);
> Z(i,j,:)=limg(a,b,:);
> end;
> end;
> endfunction
octave-3.0.1.exe:2> cd c:\users\admin\pictures
octave-3.0.1.exe:3> pic=imread('catmosaic.jpg');
octave-3.0.1.exe:4> pic=double(pic)/255;
octave-3.0.1.exe:5> pic=zoom2(pic,1/4);
octave-3.0.1.exe:6> imshow(pic)
octave-3.0.1.exe:7> size(pic)
ans =
31 31 3
Step 3
Reduce it with only 27 color values.
Octave code is as follows( I will try to write in MATLAB too)
octave-3.0.1.exe:8> function B=draw27(A)
> A=double(A);
> for i=1:size(A,1)
> for j=1:size(A,2)
> for k=1:3
> B(i,j,k)=floor(A(i,j,k)/86)*86+42;
> endfor
> endfor
> endfor
> endfunction
octave-3.0.1.exe:9> cd c:\users\admin\pictures
octave-3.0.1.exe:10> pic=imread('catmosaic.jpg')
octave-3.0.1.exe:11> pic27=draw27(pic);
octave-3.0.1.exe:12> imshow(double(pic27)/255)
Step 4
Enlarge(20 times) the image using the nearest neighbor approach.
Size of the image will be 620 x 620
Octave code is like this:
cols=floor(size(simg,2)*scale);
for i=0:(rows-1);
for j=0:(cols-1);
a=floor((i/scale)+1);
b=floor((j/scale)+1);
if (a>0) & (a<=original_rows) & (b>0) & (b<=original_cols)
Z(i+1,j+1,:)=simg(a,b,:);
end;
end;
end;
endfunction
cd c:\Users\admin\pictures
A=imread("pic27.jpg");
picstep3=stretch2(A,20);
size(picstep3);
B=double(picstep3)/255;
imwrite("strechedpic.jpg",B(:,:,1),B(:,:,2),B(:,:,3))
Step 5
Find at least one picture whose average color is one of the 27 possible colors that appear in the picture.
Tuesday, June 24, 2008
Assign 10 pic 2
For the above black and white (2-D) picture:
Radius = 150 pixels and
Rotated =540 degrees counterclockwise (anticlockwise)
Code will be like this:
A=imread("celebrity2.jpg");
function [outpic]=swirl(A,r,gamma);
A=double(A);
Cx=size(A,1)/2;
Cy=size(A,2)/2;
for x=1:size(A,1);
for y=1:size(A,2);
d=sqrt((x-Cx)^2+(y-Cy)^2);
if (d>r)||(d<1);
outpic(x,y)=A(x,y);
else;
if (y>Cy);
theta=acos((x-Cx)/d);
else;
theta=-acos((x-Cx)/d);
end;
fr=(d/r)^2*(1-(d/r))^2*16;
newx=d*cos(theta+gamma*fr)+Cx;
newy=d*sin(theta+gamma*fr)+Cy;
a=floor(newx);
b=floor(newy);
for k=1:3;
outpic(x,y)=[1-newx+a, newx-a]*[A(a,b), A(a,b+1);A(a+1,b), A(a+1,b+1)]*[1-newy+b; newy-b];
end;
end;
end;
end;
endfunction;
B=swirl(A,150,3*pi); #the pic was rotated counterclockwise 540 degrees so gamma=+3*pi
image(B)
Assign 10 pic 1
The given image has:
Radius = 180 pixels and
Rotated =540 degrees clockwise.
Code will be like this:
pkg load image
cd c:\users\admin\pictures
A=imread("celebrity1.jpg");
function [outpic]=swirl(A,r,gamma);
A=double(A);
Cx=size(A,1)/2;
Cy=size(A,2)/2;
for x=1:size(A,1);
for y=1:size(A,2);
d=sqrt((x-Cx)^2+(y-Cy)^2);
if (d>r)||(d<1);>Cy);
theta=acos((x-Cx)/d);
else;
theta=-acos((x-Cx)/d);
end;
fr=(d/r)^2*(1-(d/r))^2*16;
newx=d*cos(theta+gamma*fr)+Cx;
newy=d*sin(theta+gamma*fr)+Cy;
a=floor(newx);
b=floor(newy);
for k=1:3;
outpic(x,y,k)=[1-newx+a, newx-a]*[A(a,b,k), A(a,b+1,k);A(a+1,b,k), A(a+1,b+1,k)]*[1-newy+b; newy-b];
end;
end;
end;
end;
endfunction;
B=swirl(A,180,-3*pi);
C=double(B)/255;
imwrite("celebrity1.jpg",C(:,:,1),C(:,:,2),C(:,:,3));
Sunday, June 22, 2008
Assign 9 Q1
Problem
Take thge images from Assignment 6 # 3 part(c) and convert them to an image with only 64 colors.
Do this by rounding the color values for every image so that each of the red, blue and green channels have values that are in the set {0, 85, 170, 255} (alternatively, if you are working with matrices with values between 0 and 1 then you will want to take your values to be 0, 1/3, 2/3 and 1. You will be representing the image by at most 4 x 4 x4 = 64 different colors.
Solution
pkg load image
cd c:\users\admin\pictures
A=imread("P1.jpg");
B=floor((A)/64)*85;
C=255*B;
imshow(C)
Assign 8 Q 5
Problem:
Start with the picture on the left (from Wiki) ,
make the picture on the right(change an
original into a new image).
Picture in question is at the following link.
http://wiki.math.yorku.ca/images/c/cf/Start.jpg
Using the nearest neighbourhood approach (Ass#7method #2) , I scaled down four of the images by factors of 0.8, 0.6, 0.4, and 0.2 like so.
A=imread("Assignment8last problem.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;
Assign8 Q3
Problem:
Enlarge the color images that you scaled down in assignment 7
using a factor of by 1/f. Use the bilinear interpolation approximation.
function largeimage=growing(small,f)
rows=size(smallimage,1);
cols=size(smallimage,2);
Mp=floor(size(smallimage,1)*f);
Np=floor(size(smallimage,2)*f);
for i=1:(Mp-1);
for j=1:(Np-1);
a=i/f+1;
b=j/f+1;
r=floor(a);
s=floor(b);
if(r greater 0)&(r less rows)&(s greater 0)&(s less cols)
for k = 1:3;
largeimage(i,j,k)=[1-a+r,a-r]*double([small(r,s,k),small(r,s+1,k);small(r+1,s,k),small(r+1,s+1,k)])*[1-b+s;b-s];
end
end
end
end
endfunction
A=imread("p2.jpg");
B=growing((double(A)/255),(4));
imshow(B)
Enlarge the color images that you scaled down in assignment 7
using a factor of by 1/f. Use the bilinear interpolation approximation.
function largeimage=growing(small,f)
rows=size(smallimage,1);
cols=size(smallimage,2);
Mp=floor(size(smallimage,1)*f);
Np=floor(size(smallimage,2)*f);
for i=1:(Mp-1);
for j=1:(Np-1);
a=i/f+1;
b=j/f+1;
r=floor(a);
s=floor(b);
if(r greater 0)&(r less rows)&(s greater 0)&(s less cols)
for k = 1:3;
largeimage(i,j,k)=[1-a+r,a-r]*double([small(r,s,k),small(r,s+1,k);small(r+1,s,k),small(r+1,s+1,k)])*[1-b+s;b-s];
end
end
end
end
endfunction
A=imread("p2.jpg");
B=growing((double(A)/255),(4));
imshow(B)
Assign8 Q2
Problem:
Enlarge the color images that you scaled down in assignment 7
using a factor of 1/f. Use the "nearest neighbor" approximation.
function largeimage=growing(smallimage,f)
Mp=size(smallimage,1);
Np=size(smallimage,2);
newMp=floor(size(smallimage,1)*f);
newNp=floor(size(smallimage,2)*f);
for i=0:(newMp-1)
for j=0:(newNp-1)
a=round((i/f)+1);
b=round((j/f)+1);
if(a greater 0)&(a lessMp)&(b greater 0)&(b less Np)
largeimage(i+1,j+1,:)=smallimage(a,b,:);
end
end
end
end
A=imread("p1.jpg");
B=growing((double(A)/255),(4/3));
imshow(B)
Enlarge the color images that you scaled down in assignment 7
using a factor of 1/f. Use the "nearest neighbor" approximation.
function largeimage=growing(smallimage,f)
Mp=size(smallimage,1);
Np=size(smallimage,2);
newMp=floor(size(smallimage,1)*f);
newNp=floor(size(smallimage,2)*f);
for i=0:(newMp-1)
for j=0:(newNp-1)
a=round((i/f)+1);
b=round((j/f)+1);
if(a greater 0)&(a lessMp)&(b greater 0)&(b less Np)
largeimage(i+1,j+1,:)=smallimage(a,b,:);
end
end
end
end
A=imread("p1.jpg");
B=growing((double(A)/255),(4/3));
imshow(B)
Assign 8 Q1
1. Modify assignment 7 problem #1 so that you can scale in
the x and y directions by different factors.Scale one of the
two pictures that you downloaded from the web
(whichever looks better) by
(a) fx = 1.0 , fy = 0.5
(b) fx = 0.5, fy = 1.0
(c) fx = 0.2, fy = 0.8
function small=scale(largeimage,fx,fy);
Mp=floor(size(largeimage,1)*fx);
Np=floor(size(largeimage,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=largeimage(x+1,y+1,:)
ival=double(ival);
if (x less i/fx)
ival=ival*(1-i/fx+x)
end;
if (x+1 greater (i+1)/fx)
ival=ival*(1-(i+1)/fx+x+1);
end;
if(y less i/fy)
ival=ival*(1-j/fy+y);
end;
if (y+1 greater (j+1)/fy)
ival=ival*(1-(j+1)/fy+y+1);
end;
smallimage(i+1,j+1,:)=smallimage(i+1,j+1,:)+ival;
end;
small(i+1,j+1,:)=small(i+1,j+1,:)/((1/fx)*(1/fy));
end;
end;
endfunction
A=imread("CokeCan.jpg");
B=scale((double(A)/255),1,.5);
imshow(B)
the x and y directions by different factors.Scale one of the
two pictures that you downloaded from the web
(whichever looks better) by
(a) fx = 1.0 , fy = 0.5
(b) fx = 0.5, fy = 1.0
(c) fx = 0.2, fy = 0.8
function small=scale(largeimage,fx,fy);
Mp=floor(size(largeimage,1)*fx);
Np=floor(size(largeimage,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=largeimage(x+1,y+1,:)
ival=double(ival);
if (x less i/fx)
ival=ival*(1-i/fx+x)
end;
if (x+1 greater (i+1)/fx)
ival=ival*(1-(i+1)/fx+x+1);
end;
if(y less i/fy)
ival=ival*(1-j/fy+y);
end;
if (y+1 greater (j+1)/fy)
ival=ival*(1-(j+1)/fy+y+1);
end;
smallimage(i+1,j+1,:)=smallimage(i+1,j+1,:)+ival;
end;
small(i+1,j+1,:)=small(i+1,j+1,:)/((1/fx)*(1/fy));
end;
end;
endfunction
A=imread("CokeCan.jpg");
B=scale((double(A)/255),1,.5);
imshow(B)
Assign 7 Q1
Shrinking the image
Please see the following useful link:
http://www.shrinkpictures.com
Method one:
Average over all pixels in the destination point(Sub square average shrink)
Code would be like this:
pkg load image
cd C:\
largeimage=double(imread("p1.jpg"));
f=0.10;
Mp=floor(size(largeimage,1)*f);
Np=floor(size(largeimage,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=largeimage(x+1,y+1,:);
if (x<(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
end
end
small=smallimage*f*f;
imshow(double(small)/255);
Method 2:
To take the nearest neighborhood.
That is, the pixel at (i,j) on the smaller image
will be equal to the value of the pixel nearest
to (i/f, j/f) on the larger image.
pkg load image
cd C:\
largeimage=imread("p1.jpg");
largeimage=double(largeimage);
f=0.75;
Mp=floor(size(largeimage,1)*f-1);
Np=floor(size(largeimage,2)*f-1);
smallimage(:,:,1)=zeros(Mp-1,Np-1);
smallimage(:,:,2)=zeros(Mp-1,Np-1);
smallimage(:,:,3)=zeros(Mp-1,Np-1);
for i=1:(Mp-1)
for j=1:(Np-1)
a=round(i/f);
b=round(j/f);
smallimage(i,j,:)=largeimage(a,b,:);
end;
end;
imshow(double(smallimage/255))
Method 3
To take bilinear interpolation of the larger image.
The pixel at (i,j) on the smaller image will be equal
to the bilinear interpolation of 4 nearest neighbors of (i/f, j/f).
pkg load image
cd C:\
largeimage=double(imread("P1.jpg"));
f=0.75;
Mp=floor(size(largeimage,1)*f);
Np=floor(size(largeimage,2)*f);
smallimage(:,:,1)=zeros(Mp,Np);
smallimage(:,:,2)=zeros(Mp,Np);
smallimage(:,:,3)=zeros(Mp,Np);
for i=1:Mp
for j=1:Np
a=i/f;
b=j/f;
r=floor(a);
s=floor(b);
if (r>0) &(r(lessthan size(largeimage,1)&(s>0)&(s(lessthan size(largeimage,2)
for k=1:3
smallimage(i,j,k)=[1-a+r,a-r]*[largeimage(r,s,k),largeimage(r,s+1,k);largeimage(r+1,s,k),largeimage(r+1,s+1,k)]*[1-b+s;b-s];
end;
end;
end;
end;
imshow(smallimage/255)
Please see the following useful link:
http://www.shrinkpictures.com
Method one:
Average over all pixels in the destination point(Sub square average shrink)
Code would be like this:
pkg load image
cd C:\
largeimage=double(imread("p1.jpg"));
f=0.10;
Mp=floor(size(largeimage,1)*f);
Np=floor(size(largeimage,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=largeimage(x+1,y+1,:);
if (x<(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
end
end
small=smallimage*f*f;
imshow(double(small)/255);
Method 2:
To take the nearest neighborhood.
That is, the pixel at (i,j) on the smaller image
will be equal to the value of the pixel nearest
to (i/f, j/f) on the larger image.
pkg load image
cd C:\
largeimage=imread("p1.jpg");
largeimage=double(largeimage);
f=0.75;
Mp=floor(size(largeimage,1)*f-1);
Np=floor(size(largeimage,2)*f-1);
smallimage(:,:,1)=zeros(Mp-1,Np-1);
smallimage(:,:,2)=zeros(Mp-1,Np-1);
smallimage(:,:,3)=zeros(Mp-1,Np-1);
for i=1:(Mp-1)
for j=1:(Np-1)
a=round(i/f);
b=round(j/f);
smallimage(i,j,:)=largeimage(a,b,:);
end;
end;
imshow(double(smallimage/255))
Method 3
To take bilinear interpolation of the larger image.
The pixel at (i,j) on the smaller image will be equal
to the bilinear interpolation of 4 nearest neighbors of (i/f, j/f).
pkg load image
cd C:\
largeimage=double(imread("P1.jpg"));
f=0.75;
Mp=floor(size(largeimage,1)*f);
Np=floor(size(largeimage,2)*f);
smallimage(:,:,1)=zeros(Mp,Np);
smallimage(:,:,2)=zeros(Mp,Np);
smallimage(:,:,3)=zeros(Mp,Np);
for i=1:Mp
for j=1:Np
a=i/f;
b=j/f;
r=floor(a);
s=floor(b);
if (r>0) &(r(lessthan size(largeimage,1)&(s>0)&(s(lessthan size(largeimage,2)
for k=1:3
smallimage(i,j,k)=[1-a+r,a-r]*[largeimage(r,s,k),largeimage(r,s+1,k);largeimage(r+1,s,k),largeimage(r+1,s+1,k)]*[1-b+s;b-s];
end;
end;
end;
end;
imshow(smallimage/255)
Saturday, June 21, 2008
Assign 6 Q3b
Rainbow from Assignment 4 Q4 is given by:
To find its average color write the following code:
pkg load image
cd C:\users\admin\pictures
A=imread("assign4q18rainbow.jpg");
imshow(A)
size(A)
ans =
89 129 3
sum(sum(A))
ans =
ans(:,:,1) = 2076422
ans(:,:,2) = 1669866
ans(:,:,3) = 1580322
B=sum(sum(A))/size(A,1)/size(A,2)
B =
ans(:,:,1) = 180.86
ans(:,:,2) = 145.45
ans(:,:,3) = 137.65
for i=1:3
averagecolor(:,:,i)=B(i)*ones(100);
end
imshow(averagecolor)
Average color = white screen
Subscribe to:
Posts (Atom)