Friday, May 23, 2008

Assignment 5

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

Solution: (done on Octave 3.0.1)

A = zeros(256);
for x = 1:256;
for y = 1:256;
if (x-128)^2 + (y-128)^2 <= 2500;
A(x, y) = 1;
endif;
endfor;
endfor;
B = 255*A
imshow(B)






2 . Give Octave commands to draw the top part of figure 6.4 in the book.

Solution: (done on Octave 3.0.1)

circle1=zeros(256);
for x=1:256; for y=1:256;
if (x-99.1324)^2 + (y-128)^2 <= 2500;
circle1(x,y)=1;
endif;
endfor;
endfor;
circle2=zeros(256);
for x=1:256; for y=1:256;
if (x-142.4337567)^2 + (y-103)^2 <= 2500;
circle2(x,y)=1;
endif;
endfor;
endfor;
circle3=zeros(256);
for x=1:256; for y=1:256;
if (x-142.4337567)^2 + (y-153)^2 <= 2500;
circle3(x,y)=1;
endif;
endfor;
endfor;
A=255*circle1;
B=255*circle2;
C=255*circle3;
D(:,:,1)=B;
D(:,:,2)=A;
D(:,:,3)=C;
imshow(D)


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



b) Skew the image by a factor of 2 pixels horizontally.

Note: The next part of this solution comes from a collaborative effort with Violeta, Malgorzata & Mike. In fact, I'm not sure which part of this (if any) I can really take credit for... oh yes, I drew borders around the images so that you could tell where the image ends and the blog begins.


Solution: (done on Matlab 2007)

for x=1:256; for y=1:256;
y1=rem(2*x+y,256)+1;
bigT1(x,y1)=bigT(x,y);
end; end;
imshow(bigT1)



c) Rotate the image (5*pi/4) degrees.

Solution: (done on Matlab 2007)

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);
u1=round(u);
v1=round(v);
bigT2(mod(u1,256)+1,mod(v1,256)+1)=bigT1(x,y);
end; end;
imshow(bigT2)


And here's my own attempt to put both transformations together in a shorter sequence of commands. Here are the matrices that would be multiplied to give the overall transformation:




These are not the ones listed in our text, but are the CORRECT ones.





At each step, we will have to take the values (mod 256)+1 so that the image will wrap around.
Next, I will create a sequence of commands that will take each point (x, y) and map it to (x', y').


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;
ymod=mod(2*x+y,256)+1;
xp=x*cos(5*pi/4)-ymod*sin(5*pi/4);
yp=x*sin(5*pi/4)+ymod*cos(5*pi/4);
xpr=round(xp);
ypr=round(yp);
newbigT(mod(xpr,256)+1,mod(ypr,256)+1)=bigT(x,y);
end;
end;
imshow(newbigT)

Thursday, May 22, 2008

Assignment 4 - as good as it's going to get

On Octave/Matlab here are the commands (obviously these would need to be done somewhat sequentially since the ones below reference those above ... saves on typing). Note that these first few were done in the Gauss lab (using Octave) and the later ones were done using Matlab 2007, so the commands might be slightly different:

(1) make a matrix of 256 x 1 ones: ones(256,1)
(2) make a matrix of the integers 0 through 255 in a single row: A=[0:1:255]
(3) make a column of the integers 0 through 255: A'
(4) make a 256 x 256 matrix with the rows all the same equal to the numbers 0 to 255: B=ones(256,1)*A
(5) make a 256 x 256 matrix with the columns all the same equal to the numbers 0 to 255: B'
(6) make a 256 x 256 matrix of all zeros: zeros(256)
(7) make a 256 x 256 matrix of all ones: ones(256)
(8) make a 256 x 256 matrix of the value 128: 128*ones(256)
(9) make a 256 x 256 matrix of all zeros - it's still just: zeros(256)
(10) display a greyscale image like the following: imshow(B)










(11) display the RB face of the colour cube:


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





(12) display the RG face of the colour cube:


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




(13) display the GB face of the colour cube:



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





(14) display the CM face of the colour cube:


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





(15) display the CY face of the colour cube:



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



(16) display the YM face of the colour cube:


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





(17) display the whole colour cube in a cross with white regions where there doesn't need to be anything:















>> W(:,:,1)=ones(256);
>> W(:,:,2)=ones(256);
>> W(:,:,3)=ones(256);
>> RG(:,:,1)=[ones(256,1)*[0:1:255]/255];
>> RG(:,:,2)=[ones(256,1)*[0:1:255]/255]';
>> RG(:,:,3)=zeros(256);
>> RG=imrotate(RG,90);
>> GB(:,:,1)=zeros(256);
>> GB(:,:,2)=[ones(256,1)*[0:1:255]/255];
>> GB(:,:,3)=[ones(256,1)*[0:1:255]/255]';
>> GB=imrotate(GB,90);
>> CM(:,:,1)=[ones(256,1)*[0:1:255]/255]';
>> CM(:,:,2)=[ones(256,1)*[0:1:255]/255];
>> CM(:,:,3)=ones(256);
>> CM=imrotate(CM,180);
>> CY(:,:,1)=[ones(256,1)*[0:1:255]/255];
>> CY(:,:,2)=ones(256);
>> CY(:,:,3)=[ones(256,1)*[0:1:255]/255]';
>> CY=imrotate(CY,180);
>> YM(:,:,1)=ones(256);
>> YM(:,:,2)=[ones(256,1)*[0:1:255]/255]';
>> YM(:,:,3)=[ones(256,1)*[0:1:255]/255];
>> RB(:,:,1)=[ones(256,1)*[0:1:255]/255]';
>> RB(:,:,2)=zeros(256);
>> RB(:,:,3)=[ones(256,1)*[0:1:255]/255];
>> imshow([W,RB,W;W,YM,W;RG,CY,CM;W,GB,W])


(18) display something similar to the rainbow image that appears on the Math 5300 facebook page:

In the rainbow image from Facebook, only the following colours appear: red, orange, yellow, green, blue, violet. So I tried to create an image without cyan or magenta.







The commands to create this image were:
R(:,:,1)=ones(256);
R(:,:,2)=[ones(256,1)*[0:1:255]/255];
R(:,:,3)=zeros(256);
Y(:,:,1)=[ones(256,1)*[255:-1:0]/255];
Y(:,:,2)=ones(256);
Y(:,:,3)=zeros(256);
G(:,:,1)=zeros(256);
G(:,:,2)=ones(256,1)*[0:1:255]/255;
G(:,:,3)=ones(256,1)*[255:-1:0]/255;
GB=imrotate(G,180);
B(:,:,1)=ones(256,1)*[0:1:255]/255;
B(:,:,2)=zeros(256);
B(:,:,3)=ones(256);
I=[R,Y,GB,B];
imshow(I)


(19) shift the entries of a matrix A one place left (wrapping around left -> right):

I wrote the detailed explanation in 21) below for how the diag(x, n) command works. In order to shift the entries of a matrix A one place left (with wrap around), here's what you need to do, after defining the nxn matrix A:
Create an nxn identity matrix: x=ones(n, 1);
Shift the main diagonal down 1: B=diag(x(1:(n-1)),-1);
Replace the nth column: B(:,n)=[1 0 0 0 ... 0]; (there are n-1 zeros)
To shift the entries of A: A*B

(20) shift the entries of a matrix A one place down (wrapping around bottom -> top):
After defining the matrix A, use the same command sequence as for 19) above, but in the last step multiply B*A.

(21) shift the entries of a matrix A one place left (dropping values on the left edge):

In order to do this, you need to multiply the matrix A on the right side by a matrix consisting of 1's on the 1st sub-diagonal and 0's everywhere else.

The Matlab/Octave command to create this matrix is: diag(x, n) where the nth diagonal will contain the vector x. If n is 0, the vector will go on the main diagonal; if n is positive the vector will go on the nth super-diagonal and if n is negative the vector will go on the nth sub-diagonal. Note that the nth diagonal is shorter by n than the leading diagonal, so for instance if x=ones(3,1) then diag(x,0) produces a 3x3 matrix, while diag(x,1) and diag(x,-1) produce 4x4 matrices.
So, for our particular question, we would first need to define the nxn matrix A and then define a matrix x=ones(n,1). If we set B=diag(x(1:(n-1)),-1) and then multiply A*B we shift the entries of A one place left.

(22) shift the entries of a matrix A one place down (dropping values on the bottom edge):
The explanation and setup is the same as for 21) above, but this time multiply B*A.

[Incidentally to shift the entries of a matrix A one place right, you would define x=ones(n,1) and then B=diag(x(1:(n-1)),1) and then multiply A*B. To shift the entries of a matrix A one place up, set up x and B like this and multiply B*A.]

Monday, May 19, 2008

Assignment 3 - done ... too tired to do any more

1. Why does colour space look like a shark fin?

  • there are 3 types of cones where each type responds differently to different wavelengths of light; these responses are called tristimulus values (X, Y, Z) and they are functions of wavelength
  • these X, Y, Z are integrated responses where the response of each cone type = sum of responses to the individual wavelengths of light
  • if we get X, Y, Z from tables of experimental results, we can calculate x and y
  • the sum X+Y+Z is called the total integrated response; we "normalize" values by dividing by this to calculate the chromaticity coordinates x, y and z. (eg. x = X/(X+Y+Z))we use these chromaticity coordinates to describe a colour (but we really only need x and y since z=1-x-y and is easy to calculate)
  • each colour plots a point in the chromaticity diagram; if a source producing light at just one wavelength is swept through the range 400-700 nm, it traces out the curve of the shark fin
  • since the colour purple can't be produced by a single wavelength (it needs a combination of short and long wavelengths), there is a "line of purples" joining extreme blue to extreme red across the bottom of the shark fin diagram


What do these graphs represent?
Clearly, the middle graph (wavelength vs. y) is just a plot showing the response of the "green" colour receptors to varying wavelengths of light.


As for the other two graphs, http://en.wikipedia.org/wiki/CIE_1931_color_space notes:

The CIE XYZ color space was deliberately designed so that the Y parameter was a measure of the luminance of a color. The chromaticity of a color was then specified by the two derived parameters x and y, two of the three normalized values which are functions of all three tristimulus values X, Y, and Z:The derived color space specified by x, y, and Y is known as the CIE xyY color space and is widely used to specify colors in practice.The X and Z tristimulus values can be calculated back from the chromaticity values x and y and the Y tristimulus value:

X=(Y/y)x and Z=(Y/y)z


What does this mean for the graphs? I'm not sure. The (x/y) is the multiplier that would convert Y into X, and the (z/y) is the multiplier that would convert Y into Z. So, this question would make a lot more sense to me if we were given the Y values as well and asked to graph wavelength vs. Y*(x/y), Y and Y*(z/y) since those graphs would represent the original X, Y and Z tristimulus responses.

2. Consider any two valid colours, c_1 and c_2 with coordinates (x_1, y_1) and (x_2, y_2) in the chromaticity diagram of Figure 6.5. Derive the necessary general expressions for computing the relative percentages of colours c_1 and c_2 composing a given colour that is known to lie on the straight line joining these two colours.



And, yes OK if c_1 and c_2 lie on a vertical line then this calculation won't work. In that case, use the analogous results based on the y-coordinates. So f_2 = (y-y_1)/(y_2 - y_1).



3. Consider any three valid colours now c_1, c_2 and c_3 with coordinates (x_1, y_1), (x_2, y_2) and (x_3, y_3) in the chromaticity diagram of Figure 6.5. Derive the necessary general expressions for computing the relative percentages of c_1, c_2 and c_3 composing a given colour that is known to lie within the triangle whose vertices are at the coordinates of c_1, c_2 and c_3.



4. a) Sketch the CMY components of the image in problem 6.6 as they would appear on a monochrome monitor.




b) If the CMY components sketched in a) are fed into the RGB inputs of a colour monitor, respectively, describe the resulting image.

Once an image has been converted into CMY colour space, it cannot be displayed correctly on a computer monitor since the monitor requires input in RGB form. If the CMY components from a) were fed into the RGB inputs of a colour monitor, respectively an inverted, or negative colour image will be seen (Source: www-viz.tamu.edu/faculty/parke/ends489f00/notes/sec1_4.html).
So, the 8 strips in the image will be coloured (left to right): white, cyan, blue, magenta, red, yellow, green, black. The outside border will still be gray.


5. What is the transformation on the RGB coordinates of the image which inverts the colours of the image?



The transformation to the complement is:



What effect does this transformation have on the HSI coordinates of the image?

The equations for hue, saturation and intensity could be modified by replacing R with 1-R, G with 1-G, and B with 1-B since these represent the complement transformations. When this is done, the equations look like:



The textbook (pg. 430-431) illustrates how each of the hues in the complement image can be predicted using the corresponding input colour. This could theoretically be done using the equation above, but not tonight.


The text, in its solution to exercise 6.18, points out that the saturation component of the complement can't be predicted from the saturation component of the input alone (www.prenhall.com/gonzalezwoods). In fact, the saturation depends on the hue. If the original image had red as the dominant colour, the transformed image will have red as the minimum colour. If the original image had blue as the dominant colour, it will be the minimum colour in the transformed image. Similarly, if the original image had green as the dominant colour, it will be the minimum in the transformed image. Since the minimum colour is the limiting factor which determines the saturation (since all 3 colours have to be at least at this level for finding the amount of gray), the dominant colour from the original image will determine the saturation for the transformed image. And even then, many different colours have the same saturation value so as the text solution points out, the same starting saturation can result in two different complementary colour saturations.


The intensity of the transformed image will be equal to 1-I of the original image.


6. a) Saturation and intensity don't change. For the hue, theta -> (240 - theta) for values between 0 and 240 degrees. For values of theta between 240 and 360 degrees, theta -> (300-(theta - 300)). Note that this is equivalent to a reflection (or flip) across the "green-magenta" axis in the colour wheel.


b) Here is the chromaticity diagram with the red and blue channels exchanged:


Here are the Photoshop instructions: (source: http://www.deborahsandidge.com/-/deborahsandidge/article.asp?ID=2903)
Step One: (Layer>New Adjustment Layer>Channel Mixer). Start with the output channel set to red (default).
Step Two: Adjust the source channels, moving the red channel slider from 100% to 0, and increase the blue channel slider from 0 to 100%.
Step Three: Change the output channel to blue, and increase the red source channel to 100%.
Step Four: Change the blue source channel to from 100% to 0% to exchange the blue channel for the red.