
Thursday, June 5, 2008
Thursday, May 29, 2008
Octave - Image Package


Computing the average color for this rainbow image:
Avg(R) = 15357868/255*225*310 = 0.86347
Avg(G) = 14141756/255*225*310 = 0.79509
Avg(B) = 13886570/255*225*310 = 0.78075.
The average color chip is a "light mauve", due to the components (220, 203, 199). This is different than the average color I obtained from my rainbow image, which was grey, with average color components (128, 128, 128). The average color function is blending all of the color components, I think.
(3)c) Here are the images, as generated by Octave-3.0.1:


B=imread('asst6_3c2.jpg')
imshow(B)
size(B)
ans = 107 160 3
sum(sum(B))
ans =
ans(:,:,1) = 1418726
ans(:,:,2) = 2754025
ans(:,:,3) = 2414097
max(B)
ans =
ans(:,:,1) = 255
ans(:,:,2) = 242
ans(:,:,3) = 225
Avg(R) = 1418726/255*107*160 = 0.32498
Avg(G) = 2754025/242*107*160 = 0.66474
Avg(B) = 2414097/255*107*160 = 0.62671
{used bucket fill in GIMP to generate Average Color}
- R = 83
- G = 170
- B = 160
To display the Average Color, Green/Blue:
V2=255*ones(256);
V2(:,:,1)=0.32498*ones(256);
V2(:,:,2)=0.66474*ones(256);
V2(:,:,3)=0.62671*ones(256);
imshow(V2).
A=imread('asst6_3c1.jpg')
imshow(A)
size(A)
ans = 108 160 3
sum(sum(A))
ans =
ans(:,:,1) = 2158047
ans(:,:,2) = 1096316
ans(:,:,3) = 1048465
max(A)
ans =
ans(:,:,1) = 255
ans(:,:,2) = 255
ans(:,:,3) = 255
Avg(R) = 2158047/255*108*160 = 0.48975
Avg(G) = 1096316/255*108*160 = 0.24880
Avg(B) = 1048465/255*108*160 = 0.23794
{used bucket fill in GIMP to generate Average Color}
- R = 125
- G = 64
- B = 61
To display the Average Color, "Maroon":
V1=255*ones(256);
V1(:,:,1)=0.48975*ones(256);
V1(:,:,2)=0.24880*ones(256);
V1(:,:,3)=0.23794*ones(256);
imshow(V1).
Based on the color components of the average colors, the color (as seen by my eye) of the 'average color chips' do not surprise me. As compared to the dominant colors when viewing the images, the average color chips are close to the dominant colors, but look more to be a blend of the colors in the original images.
Wednesday, May 28, 2008
Octave - Part 3
bigT=255*ones(256);
bigT(30:79,64:191)=zeros(50:128);
bigT(50:199,111:146)=zeros(150,36);
newbigT=255*ones(256);
for x=1:256;
for y=1:256;
u=x*cos(5*pi/4)+y*sin(5*pi/4);
v=-x*sin(5*pi/4)+y*cos(5*pi/4);
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)
(s>0) & (s<256));
newbigT(x,y)=[1-a+r,a-r]*[bigT(r,s),bigT(r,s+1);
bigT(r+1,s),bigT(r+1,s+1)]*[1-b+s;b-s];
endif;
endfor;
endfor;
imshow(newbigT).

(2) To create a 256 x 256 matrix with 1s in the (i, i+1) position and zeros elsewhere:
- Using a for loop, as I did in Assignment #4:
for i=1:256;
for j=1:256;
if (j-i)==1;
T(i,j)=1;
endif;
endfor;
endfor;
T.
- Using triu(M,k):
triu(ones(256),1) - triu(ones(256),2)
(3) Calculating Average Color:
X(:,:,3)=zeros(100);
imshow(X).size(X)
ans= 100 100 3sum(sum(X))
ans =ans(:,:,1)=10 000
ans(:,:,2)=5050
ans(:,:,3)=0
max(X)ans =
ans(:,:,1) = displays columns 1 through 100 with entries = 1ans(:,:,2) = displays columns 1 through 100 with entries = 1
ans(:,:,3) = displays columns 1 through 100 with entries = 0Avg(R) = 10 000/1*100*100 = 1
Avg(G) = 5050/1*100*100=0.505Avg(B) = 0/0*100*100 = 0
To display the average color (orange image above):A(:,:,1)=ones(256);
A(:,:,2)=0.505*ones(256);A(:,:,3)=zeros(256);
X=255*A;imshow(X).
b) Using X=[R, Y, G, C, B, M], where R, Y, G, C, B, and M are defined in Question #18, Assignment 4, as my Rainbow Image (no, Cyan isn't really visible on the facebook page, and my Magenta doesn't entirely look like the Purple component that is on the facebook page...I am curious to compare the Average colors generated using my Rainbow Image and those from the Facebook Rainbow when I can get imread to work).
First, define R, Y, G, C, B, M;
X=[R,Y,G,C,B,M];
size(X)ans = 256 1536 3
sum(sum(X))ans =
ans(:,:,1) = 196608ans(:,:,2) = 196608
ans(:,:,3) = 196608max(X)
ans =ans(:,:,1) = displays columns 1 through 1536 with entries between 0 and 1
ans(:,:,2) = displays columns 1 through 1536 with entries between 0 and 1ans(:,:,3) = displays columns 1 through 1536 with entries between 0 and 1
Avg(R)=Avg(G)=Avg(B)= 196608/1*256*1536 = 0.5To display the average color:
A(:,:,1)=0.5*ones(256);
A(:,:,2)=0.5*ones(256);
A(:,:,3)=0.5*ones(256);
X=255*A; {this line not necessary; 128*A works displays the same image}
imshow(X).
This is a black screen, but shouldn't it be grey?...not worth uploading.

Friday, May 23, 2008
Octave - Part 2
S=zeros(256);
for x = 1:256;
for y = 1:256;
if ((x-128)^2 + (y-128)^2<=2500);
(2) Top of Fig. 6.4:
To determine centre(p,q):=(rows,columns), assuming the three circles intersect at centre(128, 128):
- R: p = 128+12.5=140.5; q = 90.5+25=115.5
- G: p = 128-12.5=115.5; q=128
- B: p = 140.5; q = 128 + 12.5=140.5
C1=zeros(256);
for x = 1:256;
G(:,:,1)=zeros(256);G(:,:,2)=ones(256);G(:,:,3)=[ones(256,1)*[0:1:255]/255].
(3) First display bigT:
b) Rotate the image by 5pi/4 degrees [(x,y) -> (x*cos(theta) - y*sin(theta), x*sin(theta) + y*cos(theta)]:
c) Skew the image by a factor of 1 pixel horizontally and then rotate by 5pi/4 degrees:
Thursday, May 22, 2008
Octave - Part 1
(2) [0:1:255] = integers 0 through 255 in a single row (1 x 256)
(3) [o:1:255]' = integers 0 through 255 in a single column (256 x 1)
(4) ones(256, 1)*[0:1:255] = 256 x 256 equal rows of integers 0 through 255
(5) [0:1:255]'*ones(1, 256) = 256 x 256 equal columns of integers 0 through 255
(6) zeros(256) = 256 x 256 zeros
(7) ones (256) = 256 x 256 ones
(8) 128*ones(256) = 256 x 256 every entry 128
(9) zeros(256) = 256 x 256 zeros
(10a) imshow(ones(256, 1)*[0:1:255]/255) = greyscale image w/columns becoming lighter L to R
(10b) imshow([0:1:255]'*ones(1, 256)/255) = greyscale image w/rows becoming lighter top to bottom

(11) The RB face of the color cube:
RB(:,:,1) = [[0:1:255]'*ones(1, 256)/255];
RB(:,:,2) = zeros(256);
RB(:,:,3)=[ones(256, 1)*[0:1:255]/255];
imshow(RB).
Rationale: RB = [256 x 256 equal columns of integers 0 through 255; 256 x 256 zeros; 256 x 256 equal rows of integers 0 through 255] = 256 x 256 x 3 array (i.e. 3 layers)
The corners of the RB face are Black(0 0 0), Blue(0 0 255), Red (255 0 0 ), Magenta(255 0 255)...visualizing this 3-layer matrix, you can see that the (1,1) entry is (0 0 0), the (1,256) entry is (0 0 255), the (256, 1) entry is (255 0 0), and the (256, 256) entry is (255 0 255). Apply this same rationale to #12-#16 by changing the components of the cubes corners.
(12) The RG face of the color cube:
RG(:,:,1) = [[255:-1:0]'*ones(1, 256)/255];
RG(:,:,2) = [ones(256, 1)*[0:1:255]/255];
RG(:,:,3) = zeros(256);
imshow(RG).
(13) The GB face of the color cube:
GB(:,:,1) = zeros(256);
GB(:,:,2) = [ones(256, 1)*[255:-1:0]/255];
GB(:,:,3) = [[255:-1:0]'*ones(1, 256)/255];
imshow(GB).
(14) The CY face of the color cube:
CY(:,:,1) = [[255:-1:0]'*ones(1, 256)/255];
CY(:,:,2) = ones(256);
CY(:,:,3) = [ones(256, 1)*[0:1:255]/255];
imshow(CY).
(15) The CM face of the color cube:
CM(:,:,1) = [[0:1:255]'*ones(1, 256)/255];
CM(:,:,2) = [ones(256, 1)*[0:1:255]/255];
CM(:,:,3) = ones(256);
imshow(CM).
(16) The YM face of the color cube:
YM(:,:,1) = ones(256);
YM(:,:,2) = [ones(256, 1)*[0:1:255]/255];
YM(:,:,3) = [255:-1:0]'*ones(1, 256)/255];
imshow(YM).
(17) The net of the color cube:
First, defining a white face by
W(:,:,1) = ones(256)
W(:,:,2) = ones(256)
W(:,:,3) = ones(256).
Then use the faces defined in Question #11-16,
Net = [W,RB,W;RG,YM,CM;W,CY,W;W,GB,W]
imshow(Net).
(18) The facebook page rainbow image can be created by displaying Red(255, 0, 0), Yellow(255, 255, 0), Green(0, 255, 0), Cyan(0, 255, 255), Blue(0, 0, 255), and Magenta(0, 255, 255):
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)
X=[R,Y,G,C,B,M];
imshow(X)
Notice the pattern corresponding to each primary colors components and in terms of their complement colors by examining Red and Cyan:
For Red: [256 x 256 ones][256 x 256 equal rows of 0 through 255][256 x 256 zeros] i.e. if the color has component (255, 0, 0), for example, layer 1 has entries all equal to one.
For Cyan: [256 x 256 zeros][256 x 256 equal rows of 255 through 0][256 x 256 ones] i.e. the "opposite" or "invert" of Red's layers.
(19) To shift entries of matrix A one place left (wrap L to R):
A=rand(4, 4);
T=[0 1 0 0; 0 0 1 0; 0 0 0 1; 1 0 0 0];
A*T= matrix with entries of A shifted one place left.
{need to determine T for a general n x n matrix}:
T=zeros(256);
for i = 1;
for j = n;
T(i, j) = 1;
endfor;
endfor;
for i = 1:256;
for j = 1:256;
if (i - j) == 1;
T(i, j) = 1;
endif;
endfor;
endfor;
A=rand(n, n);
A*T = matrix of A with entries shifted one column left (wrapped L to R).
(20) To shift entries of matrix A one place down (wrap bottom to top):
T*A = matrix with entries of A shifted one place down, where T and A are as in (19).
(21) To shift entries of matrix A one place left (drop off L):
Determined by examining 3 x3 and 4 x4 case:
- A*[0 0 0; 1 0 0; 0 1 0], where A = 3 x 3, will shift one column left
- A*[0 0 0 0; 1 0 0 0; 0 1 0 0; 0 0 1 0], where A = 4 x 4, will shift column left
The transformation matricies, in both cases, are such that if the ith row is one away from the jth column, then the ij-entry should be a one, else entries should be zeros. Octave commands below.
T=zeros(256);
for i=1:256;
for j=1:256;
if (i - j) == 1;
T(i, j)=1;
endif;
endfor;
endfor;
A=rand(256, 256);
A*T = matrix with entries of A shifted one place left (drop of L column).
(22) T*A = matrix with entries of A shifted one place down (drop off bottom row).
Assignment 3 - Add ons
(1) Table of Values for the three plots in Question #1:
(3) Matrix solution for Question #3, without expressions for a, b, & d, the relative percentages of c1, c2, & c3...yet.
Tuesday, May 20, 2008
Colour Image Processing
In Gimp, Colors - Components - Channel Mixer; Red Output Channel: 0 0 100; Blue Output Channel: 100 0 0. My transformed image appears to have a white centre, using this function, rather than Filters - Decompose/Compose?


x values are Red, y values are Green, z values are Blue; x, y, z E [0, 1]. The first plot shows wavelength against R/G. The second plot shows wavelength against G. The third plot shows wavelength against B/G. Each (x, y) in a Chromaticity diagram represents the combination of visible wavelengths of light. The shark fin represents colours that can be viewed by humans with normal vision. The diagram takes the shark fin shape because the primary colours (RGB) are visible at different wavelengths. The peak of the second graph illustrates that Green is visible at a wavelength of 520nm...the top bound of the shark fin. The combinations of visible wavelengths of light are contained within bounds illustrated in the first and third graphs, which show the ratio of R/G and B/G, and form a shark fin when plotted along with wavelength vs. G.
(2) Note: vector quantity in BOLD/ITALIC
c1 = (x1, y1)
c2 = (x2, y2)
c1c2 = (x2 - x1, y2 - y1)
MAGc1c2 = SQRT[(x2 - x1)^2 + (y2 - y1)^2]; this is the magnitude (length of the vector)
colour = c = (cx, cy)
%c1 = MAGc1c/MAGc1c2
%c2 = MAGcc2/MAGc1c2
Using Matricies:
Since (cx, cy) = a(x1, y1) + b(x2, y2), and a + b = 1, solve for a & b in terms of the other variables c1=(x1, y1), c2=(x2, y2), c=(cx, cy).
- a + b = 1
- cx = ax1 + bx2
- cy = ay1 + by2
(3) Picture a triangle with verticies c1, c2, c3, and a colour c(cx, cy) that lies within the triangle.
- draw a line from c1 through c; label the point p1(cx1, cy1) where it intersects vector c2c3
- draw a line from c2 through c; label the point p2(cx2, cy2) where it intersects vector c1c3
- draw a line from c3 through c; label the point p3(cx3, cy3) where it intersects vector c1c2
%c1 = avg[MAGc1p2/MAGc1c3 + MAGc1p3/MAGc1c2]
%c2 = avg[MAGc2p3/MAGc2c1 + MAGc2p1/MAGc2c3]
%c3 = avg[MAGc3p2/MAGc3c1 + MAGc3p1/MAGc3c2]
Using Matricies:
Since (cx, cy) = a(x1, y1) + b(x2, y2) + d(x3, y3), and a + b + d = 1, solve for a, b & d in terms of the other variables c1 = (x1, y1), c2 = (x2, y2), c3 = (x3, y3), c = (cx, cy).
- a + b + d = 1
- cx = ax1 + bx2 + dx3
- cy = ay1 + by2 + dx3
(4) a) The RGB, and corresponding CMY, components of Fig. 6.6 (in order left to right) are:
BLACK: RGB = (0 0 0), CMY = (1 1 1)
RED: RGB = ( 1 0 0), CMY = (0 1 1)
YELLOW: RGB = (1 1 0), CMY = (0 0 1)
GREEN: RGB = (0 1 0), CMY = (1 0 1)
BLUE: RGB = (0 0 1), CMY = (1 1 0)
B/G: RGB = (.5 0 1), CMY = (.5 0 0)
Pink/R: RGB = (1 0 .5), CMY = (0 1 .5)
White: RGB = (1 1 1), CMY = (0 0 0)
with a grey border around all 8 colours.
b) The resulting image if the above CMY components were fed through a RGB colour monitor can be described as follows:
White, Cyan, Blue, Magenta, Yellow, Light red, Green-Blue, Black, with a Grey border.
(5) In MatLab to invert colour move control point (0, 0) to (0, 1) and control point (1, 1) to (1, 0). z = interplq([0 255]', [0 255]', [0:255]'); that is, z = [0 1 2 ...255]' is a one-to-one mapping connecting control points (o, o) and (255, 255). The invert transformation, in MatLab, is z = interplq([o 255]', [255 0]', [0:255]'). The is the transformation of a full colour image to its colour complement; and all the combinations in between.
1. Red->Cyan: (255 0 0) -> (0 255 255)
2. Green->Magenta: (0 255 0) -> (255 0 255)
3. Blue->Yellow: (0 0 255) -> (255 255 0)
The effect that this transformation has on the HSI coordinates is described by calculating (theta, S, I) for each primary colour and its colour complement:
Red: (0, 1, 1/3); Cyan: (180, 1, 2/3)
Green: (180, 1, 1/3); Magenta: (360, 1, 2/3)
Blue: (180, 1, 1/3); Yellow: (0, 1, 2/3).
The transformed colors (the color complements) have theta values = theta(RGB) + 180 degrees. Saturation values do not change because min(R, G, B) = 0 for CMY components also, and S = 1 - 0; perhaps S will vary for R, G, B greater than 0, less than 1. Intensity values for the transformed colors are given by I = 1 - I(RGB).
(6)a) Red -> Blue; theta from 0 -> 180, no change in SI
Blue -> Red; theta from 180 -> 0, no change in SI
Cyan -> Yellow; theta from 180 -> 0, no change in SI
Yellow -> Cyan; theta from 0 -> 180, no change in SI
These new theta values correspond to a "flip" of the color about the diameter (line joining Green-Magenta) of the color wheel (aka HSI circular color plane).
Also, no change in HSI coordinates for Green and Magenta.
b) Figure 6.5, with Red and Blue colour channels exchanged, is at the top of this blog, as I could not adjust the image properties to have it show down here.








