The Research Experience for Teachers Program /

Creating an Image Using a Text File

Procedure

Background: Images are around us and important to our lives. By using simple code we can create images to show how computers can visualize data. Computer images are made from pixels, a pixel is a block of color coded with a piece of data. The pixel color can be very complex as there are millions of different shades of in the natural world. Using the standard 9 numbers to code for the colors red, blue and green can be very complex and takes up lots of space. Today we will start by using only 2 numbers to code for black and white.

A bit is a piece of information a computer uses to store and recall information. Binary code is composed of 2 numbers, 0 and 1, which code for all of the things a computer does. We will start by simply using a 0 to show a very dark color (black) and 1 will code for a very light color (white). Using these two digits the computer will be able to create basic images. Using the following 2 activities we will create images from data files. Using the 3rd activity you will create functions using ImageJ to analyze 4 images.

Preparation: Make sure your computer has a word processor such as Notepad and ImageJ loaded.

Lab Activity: Activity 1:

Open Notepad or another simple word processor. Below is a sample of what to write, we will then save it and look at your digits visually using an image program.

Open Notepad: Type “P2” on the First line, this is the image program header and tells the program it will be a 2 color image. On the 2nd line put the “#” symbol, this line is used if you would like to name the image later. Next we will set up the size of the image. The computer will look for a grid and we are going to start with a very small one. We will have 4 pixels total in 2 rows and 2 columns. To get this we would multiply 2 by 2 to get 4, so on the 3rd line put the numbers “2 2”. And finally we will set the colors for the pixels. Since we are using 2 shades of color the will put a 1 in the 4th row. The 1 comes from the number of color shades we will use (n) - 1 for 0 being a digit. Since we have 2 shades of colors (black and white) in this image we use 2 – 1 which is 1.

To create an image we will use the digits 0 and 1 on line 5 we will put 2 numbers and on line 6 we put the other 2 digits. Type “0 1” on line 5 and “1 0” on line 6. You do not need to put commas; however you can if you like. You DO need spaces between each digit.

Your code should like this, save the image to your desktop and open with ImageJ. You can use “Ctrl +” to make the image big enough to see on your screen.

Line1 P2

Line2 #

Line3 2 2

Line4 1

Line5 0 1

Line6 1 0

We can now create a bigger image in black and white.

Open the code you wrote, you may save it now under a new name. On the 3rd line we will make our grid larger, this time make it “4 4” for a total of 16 pixels. Now change the numbers in the grid to the code below and save the file. Open the new file in ImageJ and use “Ctrl +” to make it large enough to see.

1 0 0 1

0 1 1 0

0 1 1 0

1 0 0 1

Now for the last part to activity 1 we will make a larger image. On the 3rd line of code make the pixel size 36 by putting “6 6”. Now change the numbers in the grid to the code below. Save the file again using a new name and then open in ImageJ. You may notice what the image is before you create it in ImageJ.

1 0 0 1 0 1

1 0 0 1 0 0

1 1 1 1 0 1

1 1 1 1 0 1

1 0 0 1 0 1

1 0 0 1 0 1

Activity 2

Images are not just black and white (Binary). We are ready to create an image in different shades of gray in between the colors black and white. This is called a grayscale image.

As in our previous code above, we will use “P2” for the 1st line, “#”, for the 2nd line, “4 4” for line three. Now we want to have more shades. 0 will still equal black, and we have 16 pixels in our grid so we can have 16 different shades. For line 4 we will use the number of shades we want 16 – 1, so put “15” in line 4. Make a grid, 0-3 on the first line, 4-7 on line 2, 8-11 on line 3, and 12-16 for the final row. Do not forget to leave a space between each of the numbers. Your image should look like Figure-2.

Figure 2– A 4-by-4 Grayscale image

Early computer monitors were able to use the 16 shades (4-bit) so they could view the image you created. Monitors now have more color capabilities. We will use an 8-bit image for the next image. These images can be much better in quality because it allows for 256 different intensities (2^ 8 = 256)

0 is still equal to black, and (n-1) for 256 shades is 255, so 255 will represent pure white. Create a 5x5 image. Line 1 “P2”, line 2 “#’, Line 3 “5 5”, line 4 “255”.

  • Line 5 (5 numbers between 0-51)
  • Line 6 (5 numbers between 52-102)
  • Line 7 (5 numbers between 102-153)
  • Line 8 (5 numbers between 154-204)
  • Line 9 (5 numbers between 204-255)

Notice it is hard to tell the difference between numbers which are close to each other. It is hard to tell the difference between 0 and 20, or 235 and 255. Using grayscale you can get more detail (depth) in your images.

  • Create your initials on an image 10 high and 15 wide using grayscale. Save the image and show it to your instructor.

Activity 3

Grayscale images can be analyzed using ImageJ to see how the numbers can be related to a function. A function is a relationship between numbers. Below is an image created with the following function from left to right, and the box across the image was created by using the*Rectangular* button and analyzed using the analyze dropdown and plot profile. If you have a hard time seeing the points you can make list the points using the button on the lower left corner of the plot profile: f(x) = x + 10.

Figure 3– Analyze scree in ImageJ

By looking at the plot you can find the x and y coordinates, and then with the coordinates create the function which created the image.

For this image, at x=0, y=10, at x=2, y=30, at x=4, y=50. Looking at these points you can see the graph goes up by 10 each pixel and the y intercept 10, so the equation f(x) must be = x+10.

You will open the following 4 images (Unknown Functions 1-4 provided by your instructor) in ImageJ to determine the function which created the image.