Difference between revisions of "ECE 280/Imaging Lab 1"

From PrattWiki
Jump to navigation Jump to search
(Image Processing Toolbox)
Line 9: Line 9:
  
 
=== Using MATLAB Remotely ===
 
=== Using MATLAB Remotely ===
If you do not want to install MATLAB, you can use Duke's installation.  You will first need to connect to the Duke Linux system using either MobaXterm (Windows) or XQuartz (macOS), making sure you correctly issues the ssh command to get graphics.  Then simply type <code>matlab &</code> to start MATLAB.  More information is available at the page [[How To Get Work Done]]
+
If you do not want to install MATLAB, you can use Duke's installation.  You will first need to connect to the Duke Linux system using either MobaXterm (Windows) or XQuartz (macOS), making sure you correctly issue the ssh command to get graphics.  Then simply type <code>matlab &</code> to start MATLAB.  More information is available at the page [[How To Get Work Done]]
  
 
== Image Processing Toolbox ==
 
== Image Processing Toolbox ==
Line 25: Line 25:
 
* <code>colormap MAP</code> will assign a particular colormap.  For this assignment, the most useful one is '''gray''' for grayscale images.
 
* <code>colormap MAP</code> will assign a particular colormap.  For this assignment, the most useful one is '''gray''' for grayscale images.
 
* <code>axis equal</code> will tell MATLAB to display an item such that each direction has the same scale.  For the imaging commands, this is useful in that it will make each pixel a square regardless of the shape of the figure window, thus preserving the geometry of an image.
 
* <code>axis equal</code> will tell MATLAB to display an item such that each direction has the same scale.  For the imaging commands, this is useful in that it will make each pixel a square regardless of the shape of the figure window, thus preserving the geometry of an image.
 +
 +
== Examples ==
 +
The following sections will contain both the example programs given in the lab as well as the image or images they produce.  You should still type these into your own version of MATLAB to make sure you are getting the same answers.  These are provided so you can compare what you get with what we think you should get.
 +
 +
=== Example 1: Black & White Images===
 +
[[File:IP1 E1 Plot1.png|thumb]]
 +
<syntaxhighlight lang=matlab>
 +
a = [ 1 0 1 0 0; ...
 +
      1 0 1 0 1; ...
 +
      1 1 1 0 0; ...
 +
      1 0 1 0 1; ...
 +
      1 0 1 0 1 ];
 +
figure(1); clf
 +
imagesc(a)
 +
colormap gray; colorbar
 +
</syntaxhighlight>
 +
<br clear=all>
 +
 +
=== Example 2: Simple Grayscale Images===
 +
[[File:IP1 E2 Plot1.png|thumb]]
 +
<syntaxhighlight lang=matlab>
 +
b = 0:255;
 +
figure(1); clf
 +
image(b)
 +
colormap gray; colorbar
 +
</syntaxhighlight>
 +
<br clear=all>
 +
 +
=== Example 3: Less Simple Grayscale Images===
 +
[[File:IP1 E3 Plot1.png|thumb]]
 +
<syntaxhighlight lang=matlab>
 +
[x, y] = meshgrid(linspace(0, 2*pi, 201));
 +
z = cos(x).*cos(2*y);
 +
figure(1); clf
 +
imagesc(z)
 +
axis equal; colormap gray; colorbar
 +
</syntaxhighlight>
 +
Notice how the use of <code>axis equal</code> made the image look like a square since it is 201x201 but also caused the display to be filled with whitespace as a result of the figure size versus the image size.
 +
<br clear=all>
 +
 +
=== Example 4: Building an Image===
 +
[[File:IP1 E4 Plot1.png|thumb]]
 +
<syntaxhighlight lang=matlab>
 +
rad = 100;
 +
del = 10;
 +
[x, y] = meshgrid((-3*rad-del):(3*rad+del));
 +
[rows, cols] = size(x);
 +
dist = @(x, y, xc, yc) sqrt((x-xc).^2+(y-yc).^2);
 +
venn_img = zeros(rows, cols, 3);
 +
venn_img(:,:,1) = (dist(x, y, rad.*cos(0), rad.*sin(0)) < 2*rad);
 +
venn_img(:,:,2) = (dist(x, y, rad.*cos(2*pi/3), rad.*sin(2*pi/3)) < 2*rad);
 +
venn_img(:,:,3) = (dist(x, y, rad.*cos(4*pi/3), rad.*sin(4*pi/3)) < 2*rad);
 +
figure(1); clf
 +
image(venn_img)
 +
axis equal
 +
</syntaxhighlight>
 +
<br clear=all>
 +
 +
=== Example 2: ===
 +
[[File:|thumb]]
 +
<syntaxhighlight lang=matlab>
 +
 +
</syntaxhighlight>
 +
<br clear=all>
 +
 +
=== Example 2: ===
 +
[[File:|thumb]]
 +
<syntaxhighlight lang=matlab>
 +
 +
</syntaxhighlight>
 +
<br clear=all>
 +
 +
=== Example 2: ===
 +
[[File:|thumb]]
 +
<syntaxhighlight lang=matlab>
 +
 +
</syntaxhighlight>
 +
<br clear=all>
 +
 +
=== Example 2: ===
 +
[[File:|thumb]]
 +
<syntaxhighlight lang=matlab>
 +
 +
</syntaxhighlight>
 +
<br clear=all>
 +
 +
=== Example 2: ===
 +
[[File:|thumb]]
 +
<syntaxhighlight lang=matlab>
 +
 +
</syntaxhighlight>
 +
<br clear=all>
 +
 +
=== Example 2: ===
 +
[[File:|thumb]]
 +
<syntaxhighlight lang=matlab>
 +
 +
</syntaxhighlight>
 +
<br clear=all>
 +
 +
=== Example 2: ===
 +
[[File:|thumb]]
 +
<syntaxhighlight lang=matlab>
 +
 +
</syntaxhighlight>
 +
<br clear=all>
 +
 +
=== Example 2: ===
 +
[[File:|thumb]]
 +
<syntaxhighlight lang=matlab>
 +
 +
</syntaxhighlight>
 +
<br clear=all>

Revision as of 23:10, 6 October 2020

This page serves as a supplement to the first Digital Image Processing Labs for ECE 280. It has been updated for the Fall 2020 semester, including providing instructions for working on labs remotely.


Running MATLAB

MATLAB can be run remotely on the Duke Linux system, but it will generally be more convenient for you to install it on your own computer. All the documentation for this lab was written using MATLAB R2020a. It is likely that R2020b will also work, but neither that nor previous versions of MATLAB have been tested.

Installing MATLAB

MATLAB is free for Duke students. Follow the instructions in the Installation section of the EGRWiki page on MATLAB. Be sure to select the appropriate PRODUCTS when asked.

Using MATLAB Remotely

If you do not want to install MATLAB, you can use Duke's installation. You will first need to connect to the Duke Linux system using either MobaXterm (Windows) or XQuartz (macOS), making sure you correctly issue the ssh command to get graphics. Then simply type matlab & to start MATLAB. More information is available at the page How To Get Work Done

Image Processing Toolbox

For this lab, there are a few commands you will need to learn (or remember):

  • imread('filename.ext') will load an image into MATLAB. Depending on the image type, the return from this function may be a matrix of binary numbers (black and white images), an array of integers between 0 and 255 (grayscale images), or a three-layer matrix of integers between 0 and 255 (color images).
  • image(matrix) will display the contents of a matrix as viewed from above.
    • By default, the image command for a 1-layer matrix will assign colors based on a colormap that spans values from 0 to 255. Anything outside of that range will be clipped.
    • There are ways to change that, but you will not need to for this lab
    • The image command for a 3-layer matrix will assign colors based on the first layer being the red component, the second layer being the green component, and the third layer being the blue component.
      • If the matrix is made up of floating-point numbers, image expects those numbers to be in the range [0, 1]
      • If the matrix is made up of unsigned integers, image expects those numbers to be in the range [0, 255]
      • If the matrix is made up of signed integers, image expects those numbers to be in the range [-128, 127]
  • imagesc(matrix) for a one layer matrix will display the contents of a matrix as viewed from above and will also map the minimum value to "color 0" and the maximum value to "color 255"; for a three-layer matrix it will work just like image
  • colorbar will add a...colorbar to the right of an image to show the numerical values and colors associated with them. This is only useful for a one-layer image.
  • colormap MAP will assign a particular colormap. For this assignment, the most useful one is gray for grayscale images.
  • axis equal will tell MATLAB to display an item such that each direction has the same scale. For the imaging commands, this is useful in that it will make each pixel a square regardless of the shape of the figure window, thus preserving the geometry of an image.

Examples

The following sections will contain both the example programs given in the lab as well as the image or images they produce. You should still type these into your own version of MATLAB to make sure you are getting the same answers. These are provided so you can compare what you get with what we think you should get.

Example 1: Black & White Images

IP1 E1 Plot1.png
a = [ 1 0 1 0 0; ...
      1 0 1 0 1; ...
      1 1 1 0 0; ...
      1 0 1 0 1; ...
      1 0 1 0 1 ];
figure(1); clf
imagesc(a)
colormap gray; colorbar


Example 2: Simple Grayscale Images

IP1 E2 Plot1.png
b = 0:255;
figure(1); clf
image(b) 
colormap gray; colorbar


Example 3: Less Simple Grayscale Images

IP1 E3 Plot1.png
[x, y] = meshgrid(linspace(0, 2*pi, 201));
z = cos(x).*cos(2*y);
figure(1); clf
imagesc(z)
axis equal; colormap gray; colorbar

Notice how the use of axis equal made the image look like a square since it is 201x201 but also caused the display to be filled with whitespace as a result of the figure size versus the image size.

Example 4: Building an Image

IP1 E4 Plot1.png
rad = 100;
del = 10;
[x, y] = meshgrid((-3*rad-del):(3*rad+del));
[rows, cols] = size(x);
dist = @(x, y, xc, yc) sqrt((x-xc).^2+(y-yc).^2);
venn_img = zeros(rows, cols, 3);
venn_img(:,:,1) = (dist(x, y, rad.*cos(0), rad.*sin(0)) < 2*rad);
venn_img(:,:,2) = (dist(x, y, rad.*cos(2*pi/3), rad.*sin(2*pi/3)) < 2*rad);
venn_img(:,:,3) = (dist(x, y, rad.*cos(4*pi/3), rad.*sin(4*pi/3)) < 2*rad);
figure(1); clf
image(venn_img)
axis equal


Example 2:

[[File:|thumb]]


Example 2:

[[File:|thumb]]


Example 2:

[[File:|thumb]]


Example 2:

[[File:|thumb]]


Example 2:

[[File:|thumb]]


Example 2:

[[File:|thumb]]


Example 2:

[[File:|thumb]]


Example 2:

[[File:|thumb]]