Sunday, June 29, 2008
PhotoMosaic
Step#1 - Reduce the resolution of the image so that it looks as clear as possible - use nearest neighbour method.
% the size of the original image is 150 by 150 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 greaterthan 0)&(a lessthan (size(pic,1)))&(b greaterthan 0)&(b lessthan(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.2, the resulting image will be 30 by 30 pixels.
A=imread("stop.jpg");
B=shrink2(A,.2);
C=double(B)/255;
imwrite("stopstep1.png",C(:,:,1),C(:,:,2),C(:,:,3))
Step#2 - Reduce the colours of "stopstep1.png" so that there are at most 27 colours.
A=imread("stopstep1.png");
B = floor(double(A)/86)*86+42;
imwrite("stopstep2.png",double(B)(:,:,1)/255, double(B)(:,:,2)/255, double(B)(:,:,3)/255);
Step#3 - Enlarge the image using the nearest neighbour approach.
The image "stopstep2.png" will be enlarged by a factor f = 30. The resulting image "stopstep3.png" will be 900 by 900 pixels.
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 greaterthan 0)&(a lessthan (size(pic,1)))&(b greaterthan 0)&(b lessthan (size(pic,2)));
smallimage(i+1,j+1,:)=pic(a,b,:);
end;
end;
end;
endfunction;
A=imread("stopstep2.png");
B=enlarge(A,30);
C=double(B)/255;
imwrite("stopstep3.png",C(:,:,1),C(:,:,2),C(:,:,3))
Step#4 & #5 & #6 - I will attempt to find the average colour of all visible colours within the 900 by 900 pixel image (there are 4 colours visible: red, washed-out red, pink, light pink).
%To locate a pixel and calculate its average colour, for example:.
A=imread("stopstep3.png");
B=A(240:269,60:90,:);
%To see the pixel (its colour) and calculate the average colour.
C=double(B)/255;
imwrite("stopstep4.png",C(:,:,1),C(:,:,2),C(:,:,3))
size(B)
> ans. 10 10 3
sum(sum(B))/(10*10)
> ans... (see following list of colours below)
Colourchip1 - washed out red
ans(:,:,1)=214
ans(:,:,2)=128
ans(:,:,3)=128
Colourchip2 - red
ans(:,:,1)=214
ans(:,:,2)=42
ans(:,:,3)=42
Colourchip3 - pink
ans(:,:,1)=214
ans(:,:,2)=42
ans(:,:,3)=128
Colourchip4 - light pink
ans(:,:,1)=214
ans(:,:,2)=128
ans(:,:,3)=214
Using GIMP, I adjusted the colour levels of each "stopstep1.png" image.
based on the above RGB parameters by clicking LEVELS and altering the colour levels thus, creating images that will be placed within the large image thereby creating the photomosiac. The new colour chips are now ready for placement within the large image.
Step#7 - Replace the pixels in the big image with the new smaller images which have average colours in the range {42, 128, 214} as defined in Step #2.
%Using the code from Assignment #8 Q5.
T=imread("stopstep1chip1washedred.png");
U=imread("stopstep1chip2red.png");
V=imread("stopstep1chip3pink.png");
W=imread("stopstep1chip4lightpink.png");
A=imread("stopstep3.png");
% washedred colour chips (T)
A(30:59,300:329,:)=T;
A(30:59,330:359,:)=T;
A(30:59,360:389,:)=T;
A(30:59,390:419,:)=T;
A(30:59,420:449,:)=T;
A(30:59,450:479,:)=T;
A(30:59,480:509,:)=T;
A(30:59,510:539,:)=T;
A(30:59,540:569,:)=T;
A(30:59,570:599,:)=T;
A(60:89,240:269,:)=T;
A(90:119,210:239,:)=T;
A(120:149,180:209,:)=T;
A(150:179,150:179,:)=T;
A(180:209,120:149,:)=T;
A(210:239,90:119,:)=T;
A(240:269,60:89,:)=T;
A(60:89,630:659,:)=T;
A(90:119,660:689,:)=T;
A(120:149,690:719,:)=T;
A(150:179,720:749,:)=T;
A(180:209,750:779,:)=T;
A(210:239,780:809,:)=T;
A(240:269,810:839,:)=T;
A(330:359,90:119,:)=T;
A(360:389,90:119,:)=T;
A(480:509,90:119,:)=T;
A(420:449,120:149,:)=T;
A(450:479,150:179,:)=T;
A(480:509,150:179,:)=T;
A(570:599,150:179,:)=T;
A(360:389,180:209,:)=T;
A(390:419,180:209,:)=T;
A(480:509,180:209,:)=T;
A(510:539,180:209,:)=T;
A(570:599,180:209,:)=T;
A(330:359,240:269,:)=T;
A(360:389,240:269,:)=T;
A(450:479,240:269,:)=T;
A(540:569,240:269,:)=T;
A(300:329,270:299,:)=T;
A(330:359,270:299,:)=T;
A(300:329,420:449,:)=T;
A(360:389,510:539,:)=T;
A(390:419,510:539,:)=T;
A(420:449,510:539,:)=T;
A(450:479,510:539,:)=T;
A(480:509,510:539,:)=T;
A(510:539,510:539,:)=T;
A(570:599,510:539,:)=T;
A(570:599,540:569,:)=T;
A(300:329,570:599,:)=T;
A(360:389,600:629,:)=T;
A(390:419,600:629,:)=T;
A(420:449,600:629,:)=T;
A(450:479,600:629,:)=T;
A(480:509,600:629,:)=T;
A(510:539,600:629,:)=T;
A(390:419,630:659,:)=T;
A(420:449,630:659,:)=T;
A(450:479,630:659,:)=T;
A(390:419,720:749,:)=T;
A(450:479,720:749,:)=T;
A(450:479,750:779,:)=T;
A(330:359,780:809,:)=T;
A(360:389,780:809,:)=T;
A(390:419,780:809,:)=T;
A(420:449,780:809,:)=T;
% pink colour chips (V)
A(390:419,90:119,:)=V;
A(510:539,90:119,:)=V;
A(570:599,210:239,:)=V;
A(360:389,300:329,:)=V;
A(390:419,300:329,:)=V;
A(420:449,300:329,:)=V;
A(450:479,300:329,:)=V;
A(480:509,300:329,:)=V;
A(510:539,300:329,:)=V;
A(540:569,300:329,:)=V;
A(570:599,330:359,:)=V;
A(570:599,360:389,:)=V;
A(330:359,420:449,:)=V;
A(330:359,600:629,:)=V;
A(300:329,630:659,:)=V;
A(330:359,630:659,:)=V;
A(360:389,630:659,:)=V;
A(480:509,630:659,:)=V;
A(510:539,630:659,:)=V;
A(540:569,630:659,:)=V;
A(570:599,660:689,:)=V;
A(570:599,690:719,:)=V;
A(360:389,720:749,:)=V;
A(540:569,450:479,:)=V;
% light pink colour chips
A(360:389,150:179,:)=W;
A(330:359,450:479,:)=W;
A(330:359,510:539,:)=W;
% red colour chips (U)
A(60:89,270:299,:)=U;
A(60:89,300:329,:)=U;
A(60:89,330:359,:)=U;
A(60:89,360:389,:)=U;
A(60:89,390:419,:)=U;
A(60:89,420:449,:)=U;
A(60:89,450:479,:)=U;
A(60:89,480:509,:)=U;
A(60:89,510:539,:)=U;
A(60:89,540:569,:)=U;
A(60:89,570:599,:)=U;
A(60:89,600:629,:)=U;
A(90:119,240:269,:)=U;
A(90:119,270:299,:)=U;
A(90:119,300:329,:)=U;
A(90:119,330:359,:)=U;
A(90:119,360:389,:)=U;
A(90:119,390:419,:)=U;
A(90:119,420:449,:)=U;
A(90:119,450:479,:)=U;
A(90:119,480:509,:)=U;
A(90:119,510:539,:)=U;
A(90:119,540:569,:)=U;
A(90:119,570:599,:)=U;
A(90:119,600:629,:)=U;
A(90:119,630:659,:)=U;
A(120:149,210:239,:)=U;
A(120:149,240:269,:)=U;
A(120:149,270:299,:)=U;
A(120:149,300:329,:)=U;
A(120:149,330:359,:)=U;
A(120:149,360:389,:)=U;
A(120:149,390:419,:)=U;
A(120:149,420:449,:)=U;
A(120:149,450:479,:)=U;
A(120:149,480:509,:)=U;
A(120:149,510:539,:)=U;
A(120:149,540:569,:)=U;
A(120:149,570:599,:)=U;
A(120:149,600:629,:)=U;
A(120:149,630:659,:)=U;
A(120:149,660:689,:)=U;
A(150:179,180:209,:)=U;
A(150:179,210:239,:)=U;
A(150:179,240:269,:)=U;
A(150:179,270:299,:)=U;
A(150:179,300:329,:)=U;
A(150:179,330:359,:)=U;
A(150:179,360:389,:)=U;
A(150:179,390:419,:)=U;
A(150:179,420:449,:)=U;
A(150:179,450:479,:)=U;
A(150:179,480:509,:)=U;
A(150:179,510:539,:)=U;
A(150:179,540:569,:)=U;
A(150:179,570:599,:)=U;
A(150:179,600:629,:)=U;
A(150:179,630:659,:)=U;
A(150:179,660:689,:)=U;
A(150:179,690:719,:)=U;
A(180:209,150:179,:)=U;
A(180:209,180:209,:)=U;
A(180:209,210:239,:)=U;
A(180:209,240:269,:)=U;
A(180:209,270:299,:)=U;
A(180:209,300:329,:)=U;
A(180:209,330:359,:)=U;
A(180:209,360:389,:)=U;
A(180:209,390:419,:)=U;
A(180:209,420:449,:)=U;
A(180:209,450:479,:)=U;
A(180:209,480:509,:)=U;
A(180:209,510:539,:)=U;
A(180:209,540:569,:)=U;
A(180:209,570:599,:)=U;
A(180:209,600:629,:)=U;
A(180:209,630:659,:)=U;
A(180:209,660:689,:)=U;
A(180:209,690:719,:)=U;
A(180:209,720:749,:)=U;
A(210:239,120:149,:)=U;
A(210:239,150:179,:)=U;
A(210:239,180:209,:)=U;
A(210:239,210:239,:)=U;
A(210:239,240:269,:)=U;
A(210:239,270:299,:)=U;
A(210:239,300:329,:)=U;
A(210:239,330:359,:)=U;
A(210:239,360:389,:)=U;
A(210:239,390:419,:)=U;
A(210:239,420:449,:)=U;
A(210:239,450:479,:)=U;
A(210:239,480:509,:)=U;
A(210:239,510:539,:)=U;
A(210:239,540:569,:)=U;
A(210:239,570:599,:)=U;
A(210:239,600:629,:)=U;
A(210:239,630:659,:)=U;
A(210:239,660:689,:)=U;
A(210:239,690:719,:)=U;
A(210:239,720:749,:)=U;
A(210:239,750:779,:)=U;
A(240:269,90:119,:)=U;
A(240:269,120:149,:)=U;
A(240:269,150:179,:)=U;
A(240:269,180:209,:)=U;
A(240:269,210:239,:)=U;
A(240:269,240:269,:)=U;
A(240:269,270:299,:)=U;
A(240:269,300:329,:)=U;
A(240:269,330:359,:)=U;
A(240:269,360:389,:)=U;
A(240:269,390:419,:)=U;
A(240:269,420:449,:)=U;
A(240:269,450:479,:)=U;
A(240:269,480:509,:)=U;
A(240:269,510:539,:)=U;
A(240:269,540:569,:)=U;
A(240:269,570:599,:)=U;
A(240:269,600:629,:)=U;
A(240:269,630:659,:)=U;
A(240:269,660:689,:)=U;
A(240:269,690:719,:)=U;
A(240:269,720:749,:)=U;
A(240:269,750:779,:)=U;
A(240:269,780:809,:)=U;
A(270:299,60:89,:)=U;
A(270:299,90:119,:)=U;
A(270:299,120:149,:)=U;
A(270:299,150:179,:)=U;
A(270:299,180:209,:)=U;
A(270:299,210:239,:)=U;
A(270:299,240:269,:)=U;
A(270:299,270:299,:)=U;
A(270:299,300:329,:)=U;
A(270:299,330:359,:)=U;
A(270:299,360:389,:)=U;
A(270:299,390:419,:)=U;
A(270:299,420:449,:)=U;
A(270:299,450:479,:)=U;
A(270:299,480:509,:)=U;
A(270:299,510:539,:)=U;
A(270:299,540:569,:)=U;
A(270:299,570:599,:)=U;
A(270:299,600:629,:)=U;
A(270:299,630:659,:)=U;
A(270:299,660:689,:)=U;
A(270:299,690:719,:)=U;
A(270:299,720:749,:)=U;
A(270:299,750:779,:)=U;
A(270:299,780:809,:)=U;
A(270:299,810:839,:)=U;
A(300:329,60:89,:)=U;
A(300:329,90:119,:)=U;
A(300:329,240:269,:)=U;
A(300:329,450:479,:)=U;
A(300:329,600:629,:)=U;
A(300:329,780:809,:)=U;
A(300:329,810:839,:)=U;
A(330:359,60:89,:)=U;
A(330:359,810:839,:)=U;
A(360:389,60:89,:)=U;
A(360:389,270:299,:)=U;
A(360:389,390:419,:)=U;
A(360:389,420:449,:)=U;
A(360:389,810:839,:)=U;
A(390:419,60:89,:)=U;
A(390:419,210:239,:)=U;
A(390:419,240:269,:)=U;
A(390:419,270:299,:)=U;
A(390:419,390:419,:)=U;
A(390:419,420:449,:)=U;
A(390:419,810:839,:)=U;
A(420:449,60:89,:)=U;
A(420:449,90:119,:)=U;
A(420:449,240:269,:)=U;
A(420:449,270:299,:)=U;
A(420:449,390:419,:)=U;
A(420:449,420:449,:)=U;
A(420:449,810:839,:)=U;
A(450:479,60:89,:)=U;
A(450:479,90:119,:)=U;
A(450:479,120:149,:)=U;
A(450:479,270:299,:)=U;
A(450:479,390:419,:)=U;
A(450:479,420:449,:)=U;
A(450:479,780:809,:)=U;
A(450:479,810:839,:)=U;
A(480:509,60:89,:)=U;
A(480:509,270:299,:)=U;
A(480:509,390:419,:)=U;
A(480:509,420:449,:)=U;
A(480:509,720:749,:)=U;
A(480:509,750:779,:)=U;
A(480:509,780:809,:)=U;
A(480:509,810:839,:)=U;
A(510:539,60:89,:)=U;
A(510:539,270:299,:)=U;
A(510:539,390:419,:)=U;
A(510:539,420:449,:)=U;
A(510:539,720:749,:)=U;
A(510:539,750:779,:)=U;
A(510:539,780:809,:)=U;
A(510:539,810:839,:)=U;
A(540:569,60:89,:)=U;
A(540:569,90:119,:)=U;
A(540:569,270:299,:)=U;
A(540:569,390:419,:)=U;
A(540:569,420:449,:)=U;
A(540:569,600:629,:)=U;
A(540:569,720:749,:)=U;
A(540:569,750:779,:)=U;
A(540:569,780:809,:)=U;
A(540:569,810:839,:)=U;
A(570:599,60:89,:)=U;
A(570:599,90:119,:)=U;
A(570:599,120:149,:)=U;
A(570:599,240:269,:)=U;
A(570:599,270:299,:)=U;
A(570:599,300:329,:)=U;
A(570:599,390:419,:)=U;
A(570:599,420:449,:)=U;
A(570:599,450:479,:)=U;
A(570:599,480:509,:)=U;
A(570:599,570:599,:)=U;
A(570:599,600:629,:)=U;
A(570:599,630:659,:)=U;
A(570:599,720:749,:)=U;
A(570:599,750:779,:)=U;
A(570:599,780:809,:)=U;
A(570:599,810:839,:)=U;
A(600:629,60:89,:)=U;
A(600:629,90:119,:)=U;
A(600:629,120:149,:)=U;
A(600:629,150:179,:)=U;
A(600:629,180:209,:)=U;
A(600:629,210:239,:)=U;
A(600:629,240:269,:)=U;
A(600:629,270:299,:)=U;
A(600:629,300:329,:)=U;
A(600:629,330:359,:)=U;
A(600:629,360:389,:)=U;
A(600:629,390:419,:)=U;
A(600:629,420:449,:)=U;
A(600:629,450:479,:)=U;
A(600:629,480:509,:)=U;
A(600:629,510:539,:)=U;
A(600:629,540:569,:)=U;
A(600:629,570:599,:)=U;
A(600:629,600:629,:)=U;
A(600:629,630:659,:)=U;
A(600:629,660:689,:)=U;
A(600:629,690:719,:)=U;
A(600:629,720:749,:)=U;
A(600:629,750:779,:)=U;
A(600:629,780:809,:)=U;
A(600:629,810:839,:)=U;
A(630:659,90:119,:)=U;
A(630:659,120:149,:)=U;
A(630:659,150:179,:)=U;
A(630:659,180:209,:)=U;
A(630:659,210:239,:)=U;
A(630:659,240:269,:)=U;
A(630:659,270:299,:)=U;
A(630:659,300:329,:)=U;
A(630:659,330:359,:)=U;
A(630:659,360:389,:)=U;
A(630:659,390:419,:)=U;
A(630:659,420:449,:)=U;
A(630:659,450:479,:)=U;
A(630:659,480:509,:)=U;
A(630:659,510:539,:)=U;
A(630:659,540:569,:)=U;
A(630:659,570:599,:)=U;
A(630:659,600:629,:)=U;
A(630:659,630:659,:)=U;
A(630:659,660:689,:)=U;
A(630:659,690:719,:)=U;
A(630:659,720:749,:)=U;
A(630:659,750:779,:)=U;
A(630:659,780:809,:)=U;
A(660:689,120:149,:)=U;
A(660:689,150:179,:)=U;
A(660:689,180:209,:)=U;
A(660:689,210:239,:)=U;
A(660:689,240:269,:)=U;
A(660:689,270:299,:)=U;
A(660:689,300:329,:)=U;
A(660:689,330:359,:)=U;
A(660:689,360:389,:)=U;
A(660:689,390:419,:)=U;
A(660:689,420:449,:)=U;
A(660:689,450:479,:)=U;
A(660:689,480:509,:)=U;
A(660:689,510:539,:)=U;
A(660:689,540:569,:)=U;
A(660:689,570:599,:)=U;
A(660:689,600:629,:)=U;
A(660:689,630:659,:)=U;
A(660:689,660:689,:)=U;
A(660:689,690:719,:)=U;
A(660:689,720:749,:)=U;
A(660:689,750:779,:)=U;
A(690:719,150:179,:)=U;
A(690:719,180:209,:)=U;
A(690:719,210:239,:)=U;
A(690:719,240:269,:)=U;
A(690:719,270:299,:)=U;
A(690:719,300:329,:)=U;
A(690:719,330:359,:)=U;
A(690:719,360:389,:)=U;
A(690:719,390:419,:)=U;
A(690:719,420:449,:)=U;
A(690:719,450:479,:)=U;
A(690:719,480:509,:)=U;
A(690:719,510:539,:)=U;
A(690:719,540:569,:)=U;
A(690:719,570:599,:)=U;
A(690:719,600:629,:)=U;
A(690:719,630:659,:)=U;
A(690:719,660:689,:)=U;
A(690:719,690:719,:)=U;
A(690:719,720:749,:)=U;
A(720:749,180:209,:)=U;
A(720:749,210:239,:)=U;
A(720:749,240:269,:)=U;
A(720:749,270:299,:)=U;
A(720:749,300:329,:)=U;
A(720:749,330:359,:)=U;
A(720:749,360:389,:)=U;
A(720:749,390:419,:)=U;
A(720:749,420:449,:)=U;
A(720:749,450:479,:)=U;
A(720:749,480:509,:)=U;
A(720:749,510:539,:)=U;
A(720:749,540:569,:)=U;
A(720:749,570:599,:)=U;
A(720:749,600:629,:)=U;
A(720:749,630:659,:)=U;
A(720:749,660:689,:)=U;
A(720:749,690:719,:)=U;
A(750:779,210:239,:)=U;
A(750:779,240:269,:)=U;
A(750:779,270:299,:)=U;
A(750:779,300:329,:)=U;
A(750:779,330:359,:)=U;
A(750:779,360:389,:)=U;
A(750:779,390:419,:)=U;
A(750:779,420:449,:)=U;
A(750:779,450:479,:)=U;
A(750:779,480:509,:)=U;
A(750:779,510:539,:)=U;
A(750:779,540:569,:)=U;
A(750:779,570:599,:)=U;
A(750:779,600:629,:)=U;
A(750:779,630:659,:)=U;
A(750:779,660:689,:)=U;
A(780:809,240:269,:)=U;
A(780:809,270:299,:)=U;
A(780:809,300:329,:)=U;
A(780:809,330:359,:)=U;
A(780:809,360:389,:)=U;
A(780:809,390:419,:)=U;
A(780:809,420:449,:)=U;
A(780:809,450:479,:)=U;
A(780:809,480:509,:)=U;
A(780:809,510:539,:)=U;
A(780:809,540:569,:)=U;
A(780:809,570:599,:)=U;
A(780:809,600:629,:)=U;
A(780:809,630:659,:)=U;
A(810:839,270:299,:)=U;
A(810:839,300:329,:)=U;
A(810:839,330:359,:)=U;
A(810:839,360:389,:)=U;
A(810:839,390:419,:)=U;
A(810:839,420:449,:)=U;
A(810:839,450:479,:)=U;
A(810:839,480:509,:)=U;
A(810:839,510:539,:)=U;
A(810:839,540:569,:)=U;
A(810:839,570:599,:)=U;
A(810:839,600:629,:)=U;
F=double(A)/255;
imwrite("final.png",F(:,:,1),F(:,:,2),F(:,:,3))
Original Image
Final Image
Wednesday, June 18, 2008
Assignment #10
The given image has a radius of 180 pixels and was rotated 540 degrees clockwise. To see who this celebrity is, we must use the following code:
A=imread("star1.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,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("starA.jpg",C(:,:,1),C(:,:,2),C(:,:,3));
this command also shows image:
image(swirl(A,180,-3*pi))
For the following black and white (2-D) picture, the radius is 150 pixels and it too was rotated 540 degrees but counterclockwise so the following code (similar to the one above) must rotate the pic clockwise 540 degrees. Here is the code:
A=imread("star2.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)
A=imread("star1.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,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("starA.jpg",C(:,:,1),C(:,:,2),C(:,:,3));
this command also shows image:
image(swirl(A,180,-3*pi))
For the following black and white (2-D) picture, the radius is 150 pixels and it too was rotated 540 degrees but counterclockwise so the following code (similar to the one above) must rotate the pic clockwise 540 degrees. Here is the code:
A=imread("star2.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)
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.
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))
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">
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.
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.
Subscribe to:
Posts (Atom)