Chapter 2. Basic Image Processing with ImageJ

If you followed the instructions found in the preceding chapter, you will have a working ImageJ installation on your system. This chapter includes:

  • Open and save images (from disk and from the ImageJ sample repository)
  • Zoom on the image and obtain pixel values
  • Color, multichannel, and stack images
  • Adjusting your image brightness, contrast, and image size
  • Thresholding

Image reading/writing

In this section, we learn how to open and write different images.

Opening images with a certain format

First of all, we will need an image to open. For this first exercise, we are going to use one of the several public domain images showing the "happy face" on Mars. Go to http://www.msss.com/msss_images/2008/01/31/index.html and download the image you see (happy_face.jpg, image credit to NASA / JPL / Malin Space Science Systems). Store it on your hard disk in a place that is easy to access, as you shall need it right away.

Now run ImageJ, execute File | Open..., and select the file you just downloaded. ImageJ will load the image contents into a new window and you should see something like this:

Opening images with a certain format

You just opened your first image using ImageJ. Smile!

We have scaled the image to fit in the page (note the (50%) on the title bar; more on this later), so your window will be bigger. In any case, you can check some common elements to all images opened in ImageJ.

  • Image title: In this case, happy_face.jpg, which by default is the name of the file. This value is useful in order to know which image you are selecting for a specific processing step, or when you are dealing with macros. You can change it using Image | Rename....
  • Information text about the image properties (Metadata): In this case, 710x1068 pixels; 8-bit; 741K; these numbers indicate the dimensions of the image, in pixels (width * height), the number of bits per pixel, and the total amount of memory used by the image.
  • The image itself displayed.

Let's stop for a while on the second point. A digital image is just a matrix containing numbers which are then represented on a computer screen as different gray values (as in this case) or a color scale. A single cell of that matrix is called a pixel; the dimensions of the image are the dimensions of the matrix in pixels. In this case, the image is contained within a matrix of 710 pixels wide and 1068 pixels high.

The number of bits per pixel indicates how the intensity values have been coded. An 8-bit image can store 256 different levels of intensity (2^8 = 256). The more bits per pixel an image uses, the more intensity levels we can store, but the image will need more disk or memory space. ImageJ handles the following image data types:

  • 8 bits (unsigned integers): It consists of values from 0 to 255.
  • 16 bits (unsigned integers): It comprises values from 0 to 65,535.
  • 32 bits (real numbers): It handles floating point values.
  • 8 bits color: It uses a lookup table (LUT) to convert each gray level to a RGB triplet.
  • 24/32 bits color: It uses RGB values, every channel with 8 bits per pixel and an optional extra 8 bits for the alpha/transparency layer.

Now, the space occupied by the image is always a measure of how much memory is used. If an image has P pixels and each pixel takes up B bits, then it will need P * B bits of the RAM memory. In the case of the "happy face", this size would be 710 * 1068 * 8 = 6066240 bits; dividing by 8 we get 758280 bytes, and dividing by 1024 (that is the number of bytes in one KB) we obtain 741 KB, exactly as ImageJ displays. However, if we check the file size on disk, it takes 702 KB. Why this (small) discrepancy? Why is the image using more space on ImageJ than on our computer's hard disk? Some image formats (such as JPG which was used to encode this image) allow compressing the data so it takes less space on disk. In this case, the compression level is small, but much bigger ratios can be achieved, especially if we are willing to lose some information in exchange for less disk space.

ImageJ reads the following image formats natively, JPG, PNG, TIFF, BMP, GIF, DICOM, PGM, and FITS; the first ones are the most common. TIFF is the preferred format in ImageJ, because it is the one that can store several non-image properties as metadata (including regions of interest). DICOM is the standard format used for medical imaging, and FITS is typically used for astronomy data. These image formats are the ones that ImageJ can open natively, but there are several plugins that will allow it to open many other different formats. Please refer to the Chapter 5, ImageJ Plugins for Users for more information on this.

Another functionality ImageJ offers is the possibility of reading images stored in a text file as a 32-bit image. These images are stored in a tabbed format, and the number of rows and columns will define the final dimensions of the image. You can convert an image to this format using Image | Transform | Image to Results (and Image | Transform | Results to Image works the other way around).

Reading raw data

If your image data is stored in a proprietary format, or as binary data (for instance, if it is the result of some measures done using different software, and stored directly to disk), you may open it with the File | Import | Raw.... menu option:

Reading raw data

Dialog for importing raw images

A raw image is simply a sequence of values stored in a file that may be mixed with some header information. If you know how these values are ordered in the file, you can successfully load the image. As you can see in the previous interface, you will need to provide the data type of the stored values, image dimensions (Width, Height, and Number of images), Offset to first image (number of bytes of non-image data before the image values start in the file), and Gap between images (number of bytes of non-image data after the end of each image, before the start of the next one). You can select some specific features using the checkboxes.

As you have seen, importing raw images is a powerful feature, but the user needs to have lots of information regarding how the values have been stored on the file, and the total amount of images to open (for stacks). If the wrong values are provided, it is still possible to open the image, but it will display in erroneous ways (for instance, just part of the image is opened, or the pixel values are scrambled, or the image is replicated several times… the particular effect depends on the particular details of the data you need to open, and the parameter that has been incorrectly set). All this information is contained in the image file when a supported format is read, so in most cases you won't need to worry about it.

Tip

We could have also opened the "happy face" image by dragging-and-dropping it over the main ImageJ window, or by copying the URL, and using the File | Import | URL... menu option. Dragging-and-dropping is also a fast method to open folders with several images as stacks.

Online sample images

In this book, we will be using two groups of sample images to illustrate the different concepts that will be presented:

  • ImageJ comes with several sample images that can be accessed from File | Open Samples. If we browse to that menu option, we can see different image modalities and formats listed. These images are stored in the NIH servers, and are downloaded from their remote location every time they are opened; please check if you can open any of them. If you are going to need them for offline access, a ZIP file that contains them can be downloaded from http://imagej.nih.gov/ij/download/sample-images.zip.
  • We also provide some example images with the book, which can be downloaded from the Packt site. We will refer to these images as "test images".

One of the test images is named tuberculosis_sample.tif. Open it using the File | Open… (Ctrl + O), and you will obtain the following window:

Online sample images

tuberculosis_sample.tif file opened in ImageJ

Saving images

Finally, suppose you have performed some analysis on a given image and want to store the results. This is as easy as running File | Save As… and selecting the appropriate format, which will depend on what are you going to use the image for (saving for posterior analysis will require a format with maximum quality like TIFF, while for web publication losing quality with JPG can be acceptable in exchange for small file size). When in doubt, select a format that uses lossless compression which will allow you to open the image exactly as you left it, such as TIFF.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset