image representation and manipulation
DESCRIPTION
Image Representation and Manipulation. CS302 Data Structures Prof. George Bebis http://www.cse.unr.edu/CVL. 0. 255. How are images represented?. 8 bits/pixel. Color images. A Simple model of image formation. The scene is illuminated by a single source. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/1.jpg)
Image Representation and Manipulation
CS302 Data Structures
Prof. George Bebis
http://www.cse.unr.edu/CVL
![Page 2: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/2.jpg)
How are images represented?
8 bits/pixel
0
255
![Page 3: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/3.jpg)
Color images
![Page 4: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/4.jpg)
A 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)
![Page 5: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/5.jpg)
CCD (Charged-Coupled Device) cameras
• Tiny solid state cells convert light energy into electric charge which is then digitized (A/D conversion).
![Page 6: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/6.jpg)
Image file formats
• Many image formats adhere to the following simple model:
– Header– Data (line by line, no breaks between lines)
![Page 7: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/7.jpg)
Image 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.
![Page 8: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/8.jpg)
Common 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)
![Page 9: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/9.jpg)
PGM 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
![Page 10: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/10.jpg)
Image 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; };
![Page 11: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/11.jpg)
Input / 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
![Page 12: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/12.jpg)
An 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(argv[1], N, M, Q, type);
// allocate memory for the image array ImageType image(N, M, Q);
![Page 13: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/13.jpg)
Threshold.cpp (cont’d) // read image readImage(argv[1], image);
cout << "Enter threshold: "; cin >> thresh;
// threshold imagefor(i=0; i<N; i++)
for(j=0; j<M; j++) { image.getVal(i, j, val); if(val < thresh) image.setVal(i, j, 255); else image.setVal(i, j, 0); }
// write image writeImage(argv[2], image); }
![Page 14: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/14.jpg)
Reading/Writing PGM images
(1D array of unsigned char)
(1D array of unsigned char)
(2D array of int)
(2D array of int)
Use “write”
Use “read”
![Page 15: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/15.jpg)
Writing 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 [M*N];
// convert integer values to unsigned char int val; for(i=0; i<N; i++) for(j=0; j<M; j++) image.getVal(i, j, val); charImage[i*M+j]=(unsigned char)val; }
![Page 16: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/16.jpg)
Writing a PGM image... (cont’d) ofp.open(fname, ios::out | ios::binary); if (!ofp) { cout << "Can't open file: " << fname << endl; exit(1); } ofp << "P5" << endl; ofp << M << " " << N << endl; ofp << Q << endl; ofp.write( reinterpret_cast<char *>(charImage), (M*N)*sizeof(unsigned char));
if (ofp.fail()) { cout << "Can't write image " << fname << endl; exit(0); } ofp.close();
}
![Page 17: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/17.jpg)
Reading 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, ios::in | ios::binary); if (!ifp) { cout << "Can't read image: " << fname << endl; exit(1); }
![Page 18: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/18.jpg)
Reading a PGM image from a file
// read header ifp.getline(header,100,'\n'); if ( (header[0]!=80) || // 'P' (header[1]!=53) ) { // '5' cout << "Image " << fname << " is not PGM" << endl; exit(1); }
ifp.getline(header,100,'\n'); // skip comments while(header[0]=='#') ifp.getline(header,100,'\n');
M=strtol(header,&ptr,0); // read M, N N=atoi(ptr);
![Page 19: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/19.jpg)
Reading a PGM image …. (cont’d)
ifp.getline(header,100,'\n'); Q=strtol(header,&ptr,0);
charImage = (unsigned char *) new unsigned char [M*N];
ifp.read( reinterpret_cast<char *>(charImage), (M*N)*sizeof(unsigned char));
if (ifp.fail()) { cout << "Image " << fname << " has wrong size" << endl; exit(1); }
ifp.close();
![Page 20: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/20.jpg)
Reading a PGM image…(cont’d)
// Convert unsigned characters to integers int val;
for(i=0; i<N; i++) for(j=0; j<M; j++) { val = (int)charImage[i*M+j]; image.setVal(i, j, val); }
}
![Page 21: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/21.jpg)
How do I “see” images on my computer?
• Unix/Linux: xv, gimp
• Windows: Photoshop
Irfanview
![Page 22: Image Representation and Manipulation](https://reader035.vdocuments.us/reader035/viewer/2022062315/5681513a550346895dbf518e/html5/thumbnails/22.jpg)
How do I convert an image from one format to another?
• Use “Save As” option