Basic Operations on Image Processing Using PGM File Format

 

 

What are Pixels?

Pixels, pels, image elements or picture elements, these terms represent a digital image’s finite number of elements, each of which has a particular value and is contained within a particular location. Defining an image as a two-dimensional function, f(x, y), where x and y are the coordinates at a spatial plane, the amplitude of f at a particular coordinate pair (x, y) gives the intensity or gray level at that particular point. Each coordinate pair serves as distinct pixel location, while the corresponding amplitude of the function serves as the value of the pixel. Note that f, x, and y are discrete for a digital image [1].

 

What is a PGM?

A PGM file format is the most basic type of grayscale file format. As a matter of fact, it is “… designed to be extremely easy to learn and write programs for” [2].

Grayscale graphic images are represented by PGM image. A PGM can be conceptualized as an array containing random valued integers. It has two versions, the raw and the plain. One factor that distinguishes the two is their magic number, which is a file type identifier. Raw is encrypted with characters “P5” while plain contains “P2” [2].

 

What is this ABOUT?

In this experiment, basic operations revolving on a PGM image format were executed. MATLAB, a high-level language and interactive environment for numerical computation, visualization, and programming, was utilized.

 

Synthesizing Images

Synthetic images with resolution of 30×30 pixels were created and saved as plain PGM files. The images below are PNG copy of the PGM files, since the latter format is not compatible with any typesetting system at hand. Scale of images below were not altered during uploading, which means that the ratio is one. All the images below are binary, except for Fig. 1f, which is composed of varying gray levels. When read using a basic word processor, such as WordPad, PGM file of Fig. 1a is represented by Fig. 8. As expected, it is a plain PGM file, its magic number is “P2”, as a result of saving it in ASCII encoding. The programming language automatically set the maximum value for each pixel equal to 255.

Figure 1. Synthetic images generated with a resolution of 30×30 pixels and maxval of automatically 255.

 

Grayscale conversion

Digital image copy of the famous painting, “The Starry Night” by Vincent Van Gogh, was uploaded, converted into grayscale, and saved in PGM file format, again, Fig. 2b is just a PNG copy for the sake of compatibility. Fig. 2 shows the input and output of the process.

Figure 2. Grayscale conversion of any image file format, in this case, Fig. 2a is in JPG format.

 

Matrix of Pixel Values

A test PGM file was upload, see Fig. 3. Matrix of the PGM le was generated, and the output is in Fig. 9. See that it gives justication on the true pixel value of the middle character ‘1’, since it is white, which is the maximum pixel value, 255.

Figure 3. Test PGM file and its corresponding pixel values.

 

Extraction of Image Properties

Figure 4 shows the painting, “Mona Lisa” by Leonardo da Vinci, in PNG copy of a PGM image format. Its properties are written beside it. This image is saved in 8-bit grayscale, allowing 256 distinct gray level values to be recorded.

Slide1

Figure 4: PNG version of the PGM image of a painting, “Mona Lisa” by Leonardo da Vinci, and its corresponding image properties.

 

Basic Manipulations

Basic manipulations were applied on the PGM format of Fig. 2. Fig. 5a shows that when a constant value, 100, is added to the loaded image, each pixel of the image will increase by the same value, 100. On the other hand, applying multiplication, instead of the preceding operation, gives drastic changes. As a matter of fact, Fig. 5b shows that almost whole of the image was washed white when each pixel is multiplied by 100. Additionally, each remaining tiny dark spots have product values less than the maximum value, 255. Zero
valued pixels, especially, remains unchanged due to their multiplicative property.

Figure 5: Applying basic image manipulations on the PGM version of 2.

Combining two images of similar matrix dimension was also executed by performing operations of addition and subtraction. When images of Figures 1c & 1e were added, the resultant image is Fig. 6a. On the other hand, if Fig. 1c is subtracted from 1e, the difference would be Fig. 6b. Some negated pixel values, 255, of the cross cancelled out with the smiley’s pixels of similar coordinate points and magnitude of value. Other negated pixels of the cross which did not intersect with the smiley’s non-zero pixels automatically became zero, since pixel values can only range from 0 to 255. Note that the images are only binary, making observation, analysis, and prediction simple.

Figure 6: Operating addition and subtraction on two images of similar matrix dimension.

 

Histogram of an Image’s Gray Level Values

PGM images of smiley (Fig. 1e), concentric squares (Fig. 1f), and Mona Lisa (Fig. 4) were explored once again by plotting histogram of their gray level values. Fig. 7 shows the generated plots, containing circles to indicate the tip of each bin. The smiley is a binary, consumed mostly by black. Fig. 7a follows the preceding description by protruding only two bins above the zero of the vertical axis, corresponding to pixel values of 0 and 255. Moreover, pixel value of zero stands higher, confirming the greater area it covers over the digital image plane. The concentric squares of different gray levels has a histogram whose middle bins have tips that are roughly diagonally aligned, Fig. 7b. This alignment is due to the concentric distribution of area of almost fixed interval of gray level difference. The Mona Lisa, which is a data that is very arbitrary in its pixel values,
manifests the histogram in Fig. 7c. The spike of bins near zero correlates with the artwork’s low overall intensity. The spike has almost continuous exponential curves on both sides (after and before it), showing that the paint covers roughly all gray levels.

Figure 7: Histograms of images with dierent number of gray levels.

 

 

References

[1] R. Gonzalez and R. Woods. Digital Image Processing, 2nd ed. Chapter 1. Prentice Hall. New Jersey. 2002.
[2] J. Poskanzer. PGM [sic]. Netpbm. 1991. Accessed on August 21, 2018. [Online].
http://netpbm.sourceforge.net/doc/pgm.html

 

Appendix

Dot_WordPad

Figure 8: PGM file of Fig. 1a read through WordPAd

 

Matrix_MATLAB

Figure 9: Generated matrix output of the PGM version of3a’s pixel values.

 

Leave a comment