ECE 280/Imaging Labs

From PrattWiki
Jump to navigation Jump to search

This page serves as a supplement to the 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 issues 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

The following will take you through several examples from the MATLAB Image Processing Toolbox. Along the way you will learn about different types if images, how they are stored and saved, and what you can do with them in MATLAB. Open the Add-On Explorer, search for Image Processing, and complete the following:

Overview Tab

Read the overview. Several of the terms may be meaningless to you at the moment.

Learn More Button

On the right of the Explorer screen, there is a Learn More box; click that to go to the Image Processing Toolbox web page. Watch the video (~2 minutes). Once that is done, you can close that page and go back to the Add-On Explorer.

Open Documentation Button

Also on the right of the Explorer screen, there is an Open Documentation button; click that to open the toolbox documentation. You will be going through several sections of this documentation.

Get Started with the Image Processing Toolbox

In the Tutorials section of this page, go through Basic Image Import, Processing, and Export. By the end of it, you will have used several basic commands: imread, imshow, whos, figure, imhist, histeq, imwrite, and iminfo. You will see more details about these as you go along. Once done, go back to the Get Started page.

Next, in the About Image Processing section, go through Image Types in the Toolbox. There are 7 different types of images listed but we will only concern ourselves with the first four: binary, indexed, grayscale, and truecolor. Read through the descriptions of each along with the following accompaniment:

  • Binary images only have two values (0/1, False/True, Off/On). They store the least amount of information but can certainly still be useful.
  • Indexed images are basically electronic "paint by numbers." Each pixel is given an integer value, and that integer value refers to a row in a lookup table that contains the amount of red, green, and blue with which to color that pixel. In the example, anything mapped to a 4 would be pure blue since the fourth row of the color map yields [0, 0, 1].
  • Grayscale images will have a range of brightness values - typically either a float in the range of 0 to 1 or an 8-bit unsigned integer in the range of 0 to 255, though 16-bit signed or unsigned integers can also be used.
  • Truecolor images will have three values for each pixel representing the amount for red, green, and blue. These ranges will also generally either be a float between 0 and 1 or an 8-bit unsigned integer between 0 and 255. These images therefore take up 3 times the amount of memory as a similarly-sized grayscale image and 24 times as much memory as a similarly-sized binary image! 16-bit unsigned integers can also be used.

You can certainly read about the other image types but we will not be using them in this lab.


Import, Export, and Conversion

In this section, go to the Read and Write Image Data from Files page. Under Functions, click the "Generic File Import and Export" section to expand the dropbox. We will be looking at each of the three functions:

  • imread: By the end of this documentation, you should be able to read an image from a local file or web page and have a better understanding of indexed versus truecolor images. Note that the example files are built in to the Image Processing Toolbox; for instance, you can follow along by typing
    A = imread('ngc6543a.jpg');
    in the command window and then typing any other commands in the documentation in the command window. You can also follow along by opening the live script and running it (F5). What follows will be some extra information or explanation of the documentation; you only need to read the sections/subsection mentioned below. For instance, in the Examples, once you have read "Convert Indexed Image to RGB" you can skip to the "Input Arguments" section.
    • Examples:
      • Read and Display Image: pretty straightforward - reads a built-in image and then displays it. If you type whos A you will see that the information is stored in three layers - that is because it is a truecolor image. Try the code imshow(A(:,:,1)). That will display only the red layer as a grayscale image - notice that this image is brighter where there is more red. See if you can show just the green layer as a grayscale - there should be some bright parts on the far left and right of the nebula since that is where the green is. Note that for all three layers the text will show up as white.
      • Convert Indexed Image to RGB: this will take an image that has a set of indices and a colormap and convert it into a three-layer array. X represents the "paint by numbers" sheet, cmap is the dictionary to see what color each number is, and RGB has three layers where each row and column now has three layers with values taken from the cmap based on the X value that was at that location before. For example, X(1,1) is color 105, and cmap(106,:) (X is 0-indexed but cmap is 1-indexed) gives (0.82, 0.56, 0.49) which is kind of a pink clay color. RGB(1,1,:) will show those same three values stored in three different layers of a single matrix. Note that the memory required to store the RGB matrix is much larger than the total memory required for X and cmap together!
    • Input Arguments:
      • filename: note that you can read from a file or a web page. For instance, try:
        S = imread('http://people.duke.edu/~mrg/spinner.jpg');
        imshow(S)
    • More About: Algorithms
      • This section shows all the different file types the imread can read. It has far more information than you really need right now but is a great resource if you end up doing more complex image manipulation
Once done with imread, click the "Back to previous page" button at the top left of the Help window. You will need to expand the Generic File Import and Export dropbox again to click....
  • imwrite: By the end of this documentation, you should know how to save information in a matrix as an image file.
    • Description - MATLAB will generally handle any conversions for you, but note that for indexed images you need to include the image data and the map.
    • Examples
      • Write Grayscale Image to PNG: Note that rand(50) creates a 50x50 array of random numbers between 0 and 1; since it is one layer, MATLAB will interpret as a grayscale image. Try
        imshow(A)
        to see the matrix as an image.
      • Write Indexed Image Data to PNG: Behold! A Clown!
      • Write Indexed Image with MATLAB Colormap: Behold! A Culown!
      • Write Truecolor Image to JPEG: This time, the rand command is used three times to generate a 50x50 array of random numbers for each of three different layers of A.
    • Input Arguments specifically the fmt section - gives an idea of the kinds of files MATLAB can write.

Display and Exploration

For this part, go to the Basic Display page. From there:

  • Under Apps click on the Image Viewer (imtool) page. That page has a link to Explore Images with Image Viewer App. Go through that page, then back up to the Image Viewer page.
  • On the Image Viewer page, now click on the imtool link under Programmatic Use. Basically, imtool is a more functional version of imshow. Go back to two pages to get to Basic Display again.
  • Under Topics, go to Display an Image in a Figure Window then go back to Basic Display
  • Under Topics, go to Interact with Images Using Image Viewer App and read the information above the figure windows to learn about what you can do with imtool. Use imtool on some graphics file and make sure you understand the tools provided with imtool. In particular, try both grayscale and color images to see how the pixel information tool changes.