Friday, May 23, 2008

Octave - Part 2

{deleted pictures for #1/2 to edit blog for #3 more easily}

(1) To construct a 256 x 256 matrix with entries of 0 everywhere except inside of a circle with radius 50 where the values are 1...a white circle with r=50 inside a black square of "length"=256.
S=zeros(256);
for x = 1:256;
for y = 1:256;
if ((x-128)^2 + (y-128)^2<=2500);
S(x,y)=1;
endif;
endfor;
endfor;
I=255*S;
imshow(I).
Thanks Alice, I had everything but the second last line. Thought of something: You must multiply S by 255 because imshow displays a color image and a 255 entry corresponds to White; an entry of 1 corresponds to White when it is a grey scale image. Or.. it is because before we had to /255 with commands, and now we have to *255..something to do with dimensions?

(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;

for y = 1:256;

if ((x-140.5)^2 + (y-115.5)^2<=2500);

C1(x,y)=1;

endif;

endfor;

endfor;

C2=zeros(256);

for x = 1:256;

for y = 1:256;

if ((x-115.5)^2 + (y-128)^2<=2500);

C2(x,y)=1;

endif;

endfor;

endfor;

C3=zeros(256);

for x = 1:256;

for y = 1:256;

if ((x-140.5)^2 + (y-140.5)^2<=2500);

C3(x,y)=1;

endif;

endfor;

endfor;

I(:,:,1)=255*C1;

I(:,:,2)=255*C2;

I(:,:,3)=255*C3;

imshow(I).

At first, I had C1(x,y)=G; C2(x,y)=R, and C3(x,y)=B...where RGB are defined in Assignment #4:

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

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

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

This doesn't work due to a dimension error, but I am still trying to comprehend this in 3-space and Octave 2-space.

(3) First display bigT:

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

a) Skew the image by a factor of 1 pixel horizontally:

for x=1:256;
for y=1:256;
u=x;
v=mod((1*x)+y,256)+1;
newbigT1(x,y)=bigT(u,v);
endfor;
endfor;
imshow(newbigT1).

b) Rotate the image by 5pi/4 degrees [(x,y) -> (x*cos(theta) - y*sin(theta), x*sin(theta) + y*cos(theta)]:

bigT2=ones(256);
for x=1:256;
for y=1:256;
newx=round(mod(x*cos(5*pi/4) - y*sin(5*pi/4), 256) +1);
newy=round(mod(x*sin(5*pi/4) + y*cos(5*pi/4), 256) +1);
bigT2(newx,newy) = bigT(x,y);
endfor;
endfor;
imshow(bigT2).


c) Skew the image by a factor of 1 pixel horizontally and then rotate by 5pi/4 degrees:
- This was done using all of the commands in a & b, with a change in the line that assigns bigT2: bigT2(newx, newy)=newbigT1(x,y)

Thursday, May 22, 2008

Octave - Part 1

(1) ones(256, 1) = 256 x 1 ones
(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

This blog was created because I am having trouble going back to old blogs and editing by adding an image. I used to be able to Format the Picture in terms of text wrapping, but that option is not available on any of the images I've uploaded into Assignment #3. Any suggestions?

(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

(6)b) Figure 6.5 with Red and Blue color channels exchanged:

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?

(1)




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.