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).

No comments: