If you followed the instructions found in the preceding chapter, you will have a working ImageJ installation on your system. This chapter includes:
In this section, we learn how to open and write different images.
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:
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.
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....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:
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).
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:
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.
In this book, we will be using two groups of sample images to illustrate the different concepts that will be presented:
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:
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.