Saturday, June 21, 2008

Assign 6Q3Cpic2


This is my picture 2 in the centre and on the right corner is its average color.


The code for the average color of picture 2isas follows:

octave-3.0.1.exe:39> cd c:\
octave-3.0.1.exe:40> G=imread("p2.jpg");
octave-3.0.1.exe:41> H=double(G)/255;
octave-3.0.1.exe:42> size(H)
ans =

640 480 3

octave-3.0.1.exe:43> averagecolor=sum(sum(H))/(640*480)
averagecolor =

ans(:,:,1) = 0.31509
ans(:,:,2) = 0.38258
ans(:,:,3) = 0.20739


octave-3.0.1.exe:44> I=ones(256);
octave-3.0.1.exe:45> I(:,:,1)=0.31509*ones(256);
octave-3.0.1.exe:46> I(:,:,2)=0.38258*ones(256);
octave-3.0.1.exe:47> I(:,:,3)=0.20739*ones(256);
octave-3.0.1.exe:48> cd c:\users\public\documents
octave-3.0.1.exe:49> imwrite("p2out.jpg",I(:,:,1),I(:,:,2),I(:,:,3));

assign 6Q3Cpic1


My first picture is this(far right corner:
The average color of this oicture is in the middle:


The code for the average color is as follows:

octave-3.0.1.exe:28> cd c:\
octave-3.0.1.exe:29> A=imread("p1.jpg");
octave-3.0.1.exe:30> B=double(A)/255;
octave-3.0.1.exe:31> size(B)
ans =

158 106 3

octave-3.0.1.exe:32> averagecolor=sum(sum(B))/(158*106)
averagecolor =

ans(:,:,1) = 0.35086
ans(:,:,2) = 0.60035
ans(:,:,3) = 0.85278


octave-3.0.1.exe:33> C=ones(256);
octave-3.0.1.exe:34> C(:,:,1)=0.35086*ones(256);
octave-3.0.1.exe:35> C(:,:,2)=0.60035*ones(256);
octave-3.0.1.exe:36> C(:,:,3)=0.85278*ones(256);
octave-3.0.1.exe:37> cd c:\users\public\documents
octave-3.0.1.exe:38> imwrite("p1out.jpg", C(:,:,1),C(:,:,2),C(:,:,3));

Assign 4 Q 18


Q 18 Code for Rainbow is as follows:
Considering 6 colors, starting from Red and go up to magenta.
Red, Yellow, Green, Cyan, Blue,magenta:
Red+Green=Yellow
Red+Blue=Magenta
Blue+Green=cyan

R(:,:,1)=ones(256);
R(:,:,2)=ones(256,1)*[0:1:255]/255;
R(:,:,3)=zeros(256);
imshow(R)

Y(:,:,1)=ones(256,1)*[255:-1:0]/255;
Y(:,:,2)=ones(256);
Y(:,:,3)=zeros(256);
imshow(Y)

G(:,:,1)=zeros(256);
G(:,:,2)=ones(256);
G(:,:,3)=ones(256,1)*[0:1:255]/255;
imshow(G)

C(:,:,1)=zeros(256);
C(:,:,2)=ones(256,1)*[255:-1:0]/255;
C(:,:,3)=ones(256);
imshow(C)

B(:,:,1)=ones(256,1)*[0:1:255]/255;
B(:,:,2)=zeros(256);
B(:,:,3)=ones(256);
imshow(B)

M(:,:,1)=ones(256);
M(:,:,2)=zeros(256);
M(:,:,3)=ones(256,1)*[255:-1:0]/255;
imshow(M)

A=[R,Y,G,C,B,M];
imshow(A)
Note: Any other combination and order of colors can be considered and change the outlook of rainbow.

Thursday, June 5, 2008

assignment #6 Q3(a)


Average color of part (a)

sum(sum(A))/100/100
ans(:,:,1) = 1
ans(:,:,2) = 0.5050
ans(:,:,3) = 0
------------------------
B=ones(100);
B(:,:,1)=1*ones(100);
B(:,:,2)=0.5050*ones(100);
B(:,:,3)=zeros(100);
imshow (B)

Wednesday, June 4, 2008

assignment #6 Q3


Q(3) Given a color image determine the 'average' color. Compute the average color for the image with:
(a)
R=ones(100);, G=tril(ones(100));, B=zeros(100);
Ans.

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

assignment #6 Q1


Q(1)

newmatrix=128*ones(256);

bigT=255*ones(256);

bigT(30:79,64:191)=zeros(50,128);

bigT(50:199,111:146)=zeros(150,36);

theta=(pi)/6;

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-1*up,256)+1;

r=floor(a);

s=floor(b);

if (r>0) & (r<256)>0) & (s<256);

mata=[1-a+r, a-r];

matb=[bigT(r,s), bigT(r,s+1); bigT((r+1),s), bigT((r+1),(s+1))];

matc=[1-b+s;b-s];

newmatrix(x,y)=mata*matb*matc;

end;

end;

end;

imshow(newmatrix)

assignment #6 Q2

Q(2):Create a 256 x 256 matix with ones in the (i,i+1) position and zeros elsewhere.

Ans.

A=zeros(256);

for x=1:256;

A(x,x+1)=1;

end;

imshow(A)
--------------------------

A 256 x 256 matrix with 1's in the (i,i+1) position and zeros elsewhere can be coded as follows:

A=tril(ones(256),1);

B=tril(ones(256),0);

A-B

----------------------

In one line:

A=(tril(ones(256,1))-tril(ones(256),0)

imshaow(A)

----------------

Tuesday, June 3, 2008

assignment #5 Q 3(part 3)

Rotate pi/6 around the corner

bigT=255*ones(256);
bigT(30:79,64:191)=zeros(50,128);
bigT(50:199,111:146)=zeros(150,36);

theta=(pi)/6;

bigT1=ones(256);

for x=0:255;

for y=0:255;

x1=(x*cos(theta)-y*sin(theta))+256;

y1=(x*sin(theta)+y*cos(theta));

x2=round(x1);

y2=round(y1);

bigT1(mod(x2,256)+1,mod(y2,256)+1)=bigT(x+1,y+1);

end;

end;

imshow(bigT1)
----------------------------------------------------------------------------------------------------------------------------

Skew and rotate:

newbigT=ones(256);


bigT=255*ones(256);


bigT(30:79,64:191)=zeros(50,128);


bigT(50:199,111:146)=zeros(150,36);


theta=(pi)/6;


for x=0:255

for y=0:255

x1=x;

y1=mod((y+1*x),256)+1;

x2=(x1*cos(theta)-y1*sin(theta))+256;

y2=(x1*sin(theta)+y1*cos(theta));

x3=round(x2);

y3=round(y2);

newbigT(mod(x3,256)+1,mod(y3,256)+1)=bigT(x+1,y+1);

end;

end;

imshow(newbigT)


assignment #5 Q 3(part 2)

Skew (s = 1)

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

x1=x;

y1=mod((y+1*x),256)+1;

bigT1(x1,y1)=bigT(x,y);

end;

end;

imshow(bigT1)

assignment #5 Q 3

Q(3) Consider the black and white image from the matrix
bigT=255*ones(256);
bigT(30:79,64:191)=zeros(50,128);
bigT(50:199,111:146)=zeros(150,36);
imshow(bigT)
----------------------------

assignment #5 Q 2


Q(2) Give Octave commands to draw the top part of figure 6.4 in the book(without lettering)
Ans.
rgbycm=zeros(256);
for x=1:256;
for y=1:256;
r=(x-128)^2 + (y-100)^2;
g=(x-78)^2 + (y-125)^2;
b=(x-128)^2 + (y-150)^2;
if r<=2500;
red=1;
else;
red=0;
end;
if g<=2500;
green=1;
else;
green=0;
end;
if b<=2500;
blue=1;
else;
blue=0;
end;
rgbycm(x,y,1)=red;
rgbycm(x,y,2)=green;
rgbycm(x,y,3)=blue;
end;
end;
imshow(rgbycm)

assignment #5 Q 1



Q(1) Find Octave commands for constructing a 256 x 256 matrix with entries of 0 everywhere except inside a circle with radius 50 where the values are 1.
Ans.

C=zeros(256);
for x=1:256;
for y=1:256;
if (x-128)^2+(y-128)^2 <= 2500;
C(x,y)=1;
end;
end;
end;
C1=255*C;
imshow(C1);

Monday, June 2, 2008

Assignment # 4 Q 15, 16, 17, 19, 20, 21, 22

Q(15)
Display the CY face of the color cube(cyan and yellow)

Ans.
Cyan= Green + Blue
Yellow =Red + Green
Code is as follows:
CY(:,:,1)=[ones(256,1)*[0:1:255]/255];
CY(:,:,2)=ones(256);
CY(:,:,3)=[ones(256,1)*[0:1:255]/255]';
imshow(CY)

Q(16) Display the GM face of the color cube.

Ans.
Green & Magenta( Magenta = Red + Blue)
Here is the code:
CM(:,:,1)=[ones(256,1)*[0:1:255]/255]';
CM(:,:,2)=[ones(256,1)*[0:1:255]/255];
CM(:,:,3)=ones(256);
imshow(CM)








Q(17)Display the whole color cube in a cross with white regions where there doesn't need to be anything.
Ans.

The octave commands are:
RB(:,:,1)=[ones(256,1)*[0:1:255]/255];
RB(:,:,2)=[zeros(256)];
RB(:,:,3)=[ones(256,1)*[0:1:255]/255]';
RB=rotdim(RB, 270);

RG(:,:,1)=[ones(256,1)*[0:1:255]/255]';
RG(:,:,2)=[ones(256,1)*[0:1:255]/255];
RG(:,:,3)=zeros(256);

GB(:,:,1)=zeros(256);
GB(:,:,2)=[ones(256,1)*[0:1:255]/255];
GB(:,:,3)=[ones(256,1)*[255:1:0]/255]';
GB=rotdim(GB, 270);

CY(:,:,1)=[ones(256,1)*[0:1:255]/255];
CY(:,:,2)=ones(256);
CY(:,:,3)=[ones(256,1)*[0:1:255]/255]';

YM(:,:,1)=ones(256);
YM(:,:,2)=[ones(256,1)*[0:1:255]/255]';
YM(:,:,3)=[ones(256,1)*[0:1:255]/255];

CM(:,:,1)=[ones(256,1)*[0:1:255]/255]';
CM(:,:,2)=[ones(256,1)*[0:1:255]/255];
CM(:,:,3)=ones(256);

WA(:,:,1)=ones(256);
WA(:,:,2)=ones(256);
WA(:,:,3)=ones(256);

WB(:,:,1)=ones(256);
WB(:,:,2)=ones(256);
WB(:,:,3)=ones(256);

WC(:,:,1)=ones(256);
WC(:,:,2)=ones(256);
WC(:,:,3)=ones(256);

WD(:,:,1)=ones(256);
WD(:,:,2)=ones(256);
WD(:,:,3)=ones(256);

WE(:,:,1)=ones(256);
WE(:,:,2)=ones(256);
WE(:,:,3)=ones(256);

WF(:,:,1)=ones(256);
WF(:,:,2)=ones(256);
WF(:,:,3)=ones(256);

Square=[WA,CM,WB,WC;RB,YM,CY,GB;WD,RG,WE,WF];
imshow(Square)


Q (19)
Shift the entries of a matrix A one place left(wrapping around left --> right)
Ans.


octave:10>A=[1,3,0;2,3,6;4,7,8]
A =

1 3 0
2 3 6
4 7 8

octave:11> B=[0,0,1;1,0,0;0,1,0]
B =

0 0 1
1 0 0
0 1 0

octave:12> A*B
ans =

3 0 1
3 6 2
7 8 4
2nd column takes the place of the 1st , 3rd takes the place of 2nd and 1st takes the place of 3rd. So one step wrapping takes place from left to right.
--------------------------------------------------------------------------------------------------

Q( 20)

Shift the entries of a matrix A one pixel down(wrapping around bottom to top)
Ans.
After defining the matrix A, use the same command sequence as for Q( 19) above, but in the last step multiply B*A.

octave:10>A=[1,3,0;2,3,6;4,7,8]
A =

1 3 0
2 3 6
4 7 8

octave:11> B=[0,0,1;1,0,0;0,1,0]
B =

0 0 1
1 0 0
0 1 0

octave:12> B*A
ans =

4 7 8
1 3 0
2 3 6
--------------------------------------------------------------------------------------------------

Q (21)
Shift the entries of a matrix A one place left(dropping values on the left edge)
Ans.
Multiply the given matrix, say, A on the right side by a matrix consisting of 1's on the 1st sub-diagonal and 0's everywhere else.
A*B
------------------------------------------------------------------------------------------------
Q(22)
Shift the entries of a matrix A one place down(dropping values on the bottom edge)

Same as s for Q( 21) above, but this time multiply B*A.

Sunday, June 1, 2008

Assignment # 4 Q # 14


Q(14) Dispaly the CY face of the color cube.
Ans.
Cyan= Green + Blue
Yellow = Red + Green
CY(:,:,1)=[ones(256,1)*[0:1:255]/255]';
CY(:,:,2)=[ones(256,1)*[0:1:255]/255];
CY(:,:,3)=ones(256);
imshow(CY)

Saturday, May 31, 2008

Assignment # 4 , Q #12 & # 13

Q(12)
Display the RG face of the color cube.
Ans.
RG(:,:,1)=[ones(256,1)*[0:1:255]/255]';
RG(:,:,2)=[ones(256,1)*[0:1:255]/255];
RG(:,:,3)=zeros(256);
imshow (RG)








Q(13)
Display the GB face of the color cube.
Ans.
GB(:,:,1)=zeros(256);
GB(:,:,2)=[ones(256,1)*[0:1:255]/255]';
GB(:,:,3)=[ones(256,1)*[255:-1:0]/255];
imshow(GB)

Assignment # 4 Q # 11


Q(11)Display the RB face of the color cube(see fig 6.9 of page 404 of the text) Ans.
RB(:,:,1)=[ones (256,1)*[0:1:255]/255]';
RB(:,:,2)=zeros(256);
RB(:,:,3)=[ones(256,1)*[0:1:255]/255];
imshow(RB)

Assignment # 4 Q # 10


Q(10) Display a greyscale image like the following.
Ans imshow((1/255)*ones(256,1)*[0:1:255])

Assignment # 4 , Q # 4, 5, 6, 7, 8, 9

Q 4. On Octave find a command to make a 256 x 256 matrix with the rows, all the same, equal to the numbers 0 to 255.
Ans. ones(256,1) * [0:1:255]
Q5. On Octave find a command to make a 256 x 256 matrix with the columns, all the same, equal to the numbers 0 to 255.
Ans.[ ones(256,1) * [0:1:255]]'
Q 6. On Octave find a command to make a 256 x 256 matrix of all zeros.
Ans. zeros(256)
Q(7)On Octave find a command to make a 256 x 256 matrix of all ones.
Ans. ones(256)
Q(8).On Octave find a command to make a 256 x 256 matrix of the value 128.
Ans. 128 * ones(256)
Q(9) same as Q 6

Assignment # 4 , Q # 1, 2, 3

Q(1) Find an Octave command to make a matrix of 256 x 1 size
Ans. ones(256,1)
Q(2) Find an Octave command to make a matrix of the integers 0 through 255 in a single row.
Ans. M=[0:1:255]
Q(3). Find an Octave command to make a column of integers 0 through 255.
Ans.M=[0:1:255]' ( transpose of the matrix M in Q 2)

Saturday, May 24, 2008

Assignment #3, Q2 & Q3

Problem 2: (book Page 457) Consider any two valid colors C1 and C2 with coordinates

(x1, y1) and (x2,y2)in the chromaticity diagram. Derive the necessary general expressions for computing the relative percentages of colors C1 and C2 composing a given color that is known to lie on the straight line joining these two colors.

Solution:

Let C(x, y) be any point (color) on the

line joining the two given point (colors) C1(x1, y1) and C2(x2, y2). The distance between the two given colors C1 and C2 is given by the distance formula in calculus:

d (c1, c2) = √(x1 – x2)2 + (y1 – y2)2

Distance between C1(x1, y1) and C(x, y) is d1 (c1, c) = = sqrt{(x1 – x)2 + (y1 – y)2} . And distance between C2(x2, y2) and (x, y) is

d2 (c2, c) = sqrt{(x2 – x)2 + (y2 – y)2}.

Let P1 = percentage of color C1(x1, y1) in C(x, y)

and P2 = percentage of color C2(x2, y2) in C(x, y)

then P1= [{d (c1, c2) - d1 (c1, c)}/ d (c1, c2) ]* 100 % .....(i)

and P2 = {d (c1, c2) - d2 (c2, c) }/ d (c1, c2) = (100 – P1) % ......(ii)

Special Cases:

(1) When C = C1 then

P1 = [{d (c1, c2) - 0} / d (c1, c2) ]* 100 % = 1 * 100 % = 100 % and

P2 = {d (c1, c2) - d2 (c2, c1)}/ d (c1, c2) = 0% = (100 – P1) % = (100 – 100)% = 0%

(2)When C = C2 then

P1=[{d (c1, c2) - d1 (c1, c2)}/ d (c1, c2) ]* 100 % = 0% and

P2 = {d (c1, c2) - d2 (c2, c2)}/ d (c1, c2) ]*100 % = 1*100% = 100%

= (100 – P1) % = (100 – 0) % = 100%

Note:

Percentage of the colors C1(x1, y1) and C2(x2, y2) in any given point (color) between C1 and C2 can be calculated by using the equations (i) and (ii) above.

For Example:

Let us consider 380 nm and 520 nm wavelengths. 380 nm wavelength has x and y coordinates as C1 (0.175, 0.003) and 520nm wavelength has x and y coordinated as C2 (0.055, 0.840). We take any point (color) e.g. C(x, y) with x and y coordinates say C (0.115, 0.4215) on the line joining C1 and C2. We can calculate percentage of C1 and C2 in C as follows:

Using (i) above: Percentage of 380nm wavelength in C(x, y) = P1, so

P1= {d (c1, c2) - d1 (c1, c)}/ d (c1, c2) * 100 %

= {√ (0 .175 – 0.055)2 + (0.003 – 0.840)2 - √ (0.175 - 0.115)2 + (0.003 – 0.4215)2} / {√ (0 .175 – 0.055)2 + (0.003 – 0.840)2} * 100 %

= {√0.0144 + 0.700569 - √0.0036 + 0.17514225} / {√0.0144 + 0.700569} * 100 %

= (0.845558395 – 0.422779197)/ (0.845558395) * 100 %

= (0.422779198) / (0.845558395) * 100 %

= 0.5 * 100 %

= 50%

Therefore, Percentage of 380nm wavelength [C1 (0.175, 0.003] in C(x, y) = C (0.115, 0.4215) is 50 %

Hence, percentage of 520nm wavelength [C2 (0.055, 0.840)] in C(x, y)
P2= (100 – P1) % = (100 - 50)% = 50% -----------------------------------------------------------------------------------------
Problem #3 ( Problem 6.3 p 457)

Consider any three valid colors now c1, c2 and c3 with coordinates (x1,y1), (x2, y2), and
(x3,y3) in the chromacity diagram of Fig 6.5. Derive the necessary general expressions for computing the relative percentages of c1, c2 and c3 composing a given color that is known to lie with in the triangle whose vertices are the coordinates of c1, c2 and c3.

Solution:





























----------------------------------------------------------------------------------------------------