Title: Image Representation and Manipulation
1Image Representation and Manipulation
- CS302 Data Structures
- Prof. George Bebis
- http//www.cse.unr.edu/CVL
2How are images represented?
8 bits/pixel
3Color images
4A Simple model of image formation
- The scene is illuminated by a single source.
- The scene reflects radiation towards the camera.
- The camera senses the light (i.e., through solid
state cells for CCD cameras)
5CCD (Charged-Coupled Device) cameras
- Tiny solid state cells convert light energy into
electric charge which is then digitized (A/D
conversion).
6Image file formats
- Many image formats adhere to the following simple
model - Header
- Data (line by line, no breaks between lines)
7Image file formats (cont.)
- Header contains at least
- A signature or magic number (i.e., a short
sequence of bytes for identifying the file
format). - The width and height of the image.
8Common image file formats
- PGM (Portable Gray Map)
- PNG (Portable Network Graphics)
- GIF (Graphic Interchange Format) -
- JPEG (Joint Photographic Experts Group)
- TIFF (Tagged Image File Format)
- FITS (Flexible Image Transport System)
9PGM format
- A popular format for grayscale images (8
bits/pixel) - Closely-related formats are
- PBM (Portable Bitmap), for binary images (1
bit/pixel) - PPM (Portable Pixelmap), for color images (24
bits/pixel) - ASCII or binary
(raw) storage
ASCII
Binary
10Image Class
- class ImageType
- public
- ImageType() // constructor
- ImageType() // destructor
- void getImageInfo(int, int, int)
- void setImageInfo(int, int, int)
- void setPixelVal(int, int, int)
- void getPixelVal(int, int, int)
- // more functions ...
- private
- int N, M, Q //N rows, M columns
- int pixelValue
-
11Input / Output Functions
- C routine to read the header of a PGM image
- ReadImageHeader.cpp
- C routine to read a PGM image
- ReadImage.cpp
- C routine to write a PGM image
- WriteImage.cpp
12An example - Threshold.cpp
- void readImageHeader(char, int, int, int,
bool) - void readImage(char, ImageType)
- void writeImage(char, ImageType)
- void main(int argc, char argv)
-
- int i, j, M, N, Q
- bool type
- int val, thresh
- // read image header
- readImageHeader(argv1, N, M, Q, type)
- // allocate memory for the image array
- ImageType image(N, M, Q)
-
13Threshold.cpp (contd)
- // read image
- readImage(argv1, image)
- cout ltlt "Enter threshold "
- cin gtgt thresh
- // threshold image
- for(i0 iltN i)
- for(j0 jltM j)
- image.getVal(i, j, val)
- if(val lt thresh)
- image.setVal(i, j, 255)
- else
- image.setVal(i, j, 0)
-
- // write image
- writeImage(argv2, image)
-
14Reading/Writing PGM images
(1D array of unsigned char)
(2D array of int)
Use write
(1D array of unsigned char)
(2D array of int)
Use read
15Writing a PGM image to a file
- void writeImage(char fname, ImageType image)
- int N, M, Q
- unsigned char charImage
- ofstream ofp
- image.getImageInfo(N, M, Q)
- charImage (unsigned char ) new unsigned char
MN - // convert integer values to unsigned char
- int val
- for(i0 iltN i)
- for(j0 jltM j)
- image.getVal(i, j, val)
- charImageiMj(unsigned char)val
-
-
16Writing a PGM image... (contd)
- ofp.open(fname, iosout iosbinary)
- if (!ofp) cout ltlt "Can't open file " ltlt fname
ltlt endl exit(1) -
- ofp ltlt "P5" ltlt endl
- ofp ltlt M ltlt " " ltlt N ltlt endl
- ofp ltlt Q ltlt endl
-
- ofp.write( reinterpret_castltchar gt(charImage),
(MN)sizeof(unsigned char)) - if (ofp.fail()) cout ltlt "Can't write image "
ltlt fname ltlt endl exit(0) -
- ofp.close()
-
17Reading a PGM image from a file
- void readImage(char fname, ImageType image)
-
- int i, j
- int N, M, Q
- unsigned char charImage
- char header 100, ptr
- ifstream ifp
- ifp.open(fname, iosin iosbinary)
- if (!ifp)
- cout ltlt "Can't read image " ltlt fname ltlt endl
- exit(1)
-
-
18Reading a PGM image from a file
- // read header
- ifp.getline(header,100,'\n')
- if ( (header0!80) // 'P'
- (header1!53) ) // '5'
- cout ltlt "Image " ltlt fname ltlt " is not PGM"
ltlt endl - exit(1)
-
- ifp.getline(header,100,'\n') // skip comments
- while(header0'')
- ifp.getline(header,100,'\n')
- Mstrtol(header,ptr,0) // read M, N
- Natoi(ptr)
19Reading a PGM image . (contd)
- ifp.getline(header,100,'\n')
- Qstrtol(header,ptr,0)
- charImage (unsigned char ) new unsigned char
MN - ifp.read( reinterpret_castltchar gt(charImage),
(MN)sizeof(unsigned char)) - if (ifp.fail())
- cout ltlt "Image " ltlt fname ltlt " has wrong size"
ltlt endl - exit(1)
-
- ifp.close()
20Reading a PGM image(contd)
- // Convert unsigned characters to integers
-
- int val
- for(i0 iltN i)
- for(j0 jltM j)
- val (int)charImageiMj
- image.setVal(i, j, val)
-
-
21How do I see images on my computer?
- Unix/Linux xv, gimp
- Windows Photoshop
- Irfanview
22How do I convert an image from one format to
another?