230 likes | 362 Views
CSE 20232 Lecture 18 – Arrays & Images. What is an image? 2-dimensional arrays Creating a new type name (typedef) Creating a structure (struct) What is a histogram? Demo (histo.cpp). Images (photographic). Images (fractal). Image Representation.
E N D
CSE 20232Lecture 18 – Arrays & Images • What is an image? • 2-dimensional arrays • Creating a new type name (typedef) • Creating a structure (struct) • What is a histogram? • Demo (histo.cpp)
Image Representation • An image is a rectangular grid of pixels • A pixel is a single picture element • Each pixel has a value representing the color (or intensity) of a single point in the image • Image size is in pixels (640 x 480, etc.) • Image resolution is in pixels / inch
Color Maps • If each pixel is a single byte, its value is often used as an index into a color map • (a table of actual 3 byte color codes) Black Dark Red Medium Yellow Bright Cyan White
Multi-dimensional Arrays • Arrays can have multiple dimensions • A 2-d array with 100 rows and 50 columns is declared as … • int matrix[100][50]; • Access is by name and both indices • To set the matrix element on row 5, column 25 to the value 37 … • matrix[5][25] = 37;
To process the whole array … // set all values to 100 for(int row=0; row< 100; row++) for (int col=0; col< 50; col++) matrix[row][col] = 100; // read all values from a file infile.open(“matrix.data”); infile >> numRows >> numCols; for(int row=0; row< numRows; row++) for (int col=0; col< numCols; col++) infile >> matrix[row][col];
To process the whole array … // find the position of the largest value largest = matrix[0][0]; r = 0; c = 0; for(int row=0; row< 100; row++) for (int col=0; col< 50; col++) if (matrix[row][col] > largest) { largest = matrix[row][col]; r = row; c = col; }
Passing multi-dimensional arrays as parameters // only the first index range can be left // unspecified. The other ranges are needed by the // compiler to compute how to index into the array // correct void processIt (int matrix[][25], int m, int n); void processIt (int matrix[][100][100]); void processIt (int matrix[100][100][100]); // NOT correct void processIt (int matrix[][], int m, int n); void processIt (int matrix[100][], int m, int n);
Defining new type names • We can associate a simple meaningful name with a type that is more complex, using … • … the C/C++ typedef keyword • Format: • typedef <actual type> <new name>; • Example: • typedef unsigned int uint;
Using the new type names • The new type name can be used as a substitute for the more complicated actual type description anywhere in the code • Examples: // prototype return value or parameter type uint factorial(uint n); // variable declaration uint number = 87; // function implementation void swap (uint &x, uint &y) { uint temp = x; x = y; y = temp; }
Structures • Collections of related data items can be collected within a structure defined using … • … the C/C++ struct keyword • Format: • struct <struct name> { • <type> <item name>; // item or field 1 • <type> <item name>; // item or field 2 • … • } <object or type name>;
Structure Example • Here is a structure used to represent a student struct student { string name; unsigned char age; }; • In C you declare student variables s1 & s2 struct student s1, s2; • In C++ you can do the same OR student s1, s2;
Using the structure • There is a dot (.) notation used to access individual items or fields within the structure // set student s1’s name s1.name = “mary”; // read student s2’s age cin >> s2.age;
Using typedef and struct together • To associate a simple structure type name usable in both C & C++ we can combine the use of typedef and struct typedef struct student { string name; unsigned char age; } Student; // Now, in C & C++ “Student” is the only required // type name in any later declarations Student s1, s2;
Putting it together …Images in memory // create a type name for a 2-d array of unsigned char // (shades of gray) and call it GrayPixelMatrix typedef unsigned char GrayPixelMatrix[MAXDIM][MAXDIM]; // create a type of struct to hold a GrayscaleImage typedef struct _grayscale_image { GrayPixelMatrix pixmap; // the shades of gray unsigned rows, cols; // the size of the image } GrayscaleImage; // the type name // use the new type in a function prototype bool loadImage(GrayscaleImage &image, string filename);
The GrayscaleImage 0 1 2 3 4 5 …. pixmap 0 1 2 3 4 5 … rows cols
What is a histogram? • A histogram is a count of the occurrences of each color (or shade of gray) in an image • We can use these percentages to determine • How to enhance contrast • How to segment the image into figure and background • …
Example Histogram HISTOGRAM: percentage of each color range in image. colors[ 0.. 15] 18% +++++++++ colors[ 16.. 31] 12% ++++++ colors[ 32.. 47] 7% ++++ colors[ 48.. 63] 4% ++ colors[ 64.. 79] 3% ++ colors[ 80.. 95] 3% ++ colors[ 96..111] 2% + colors[112..127] 4% ++ colors[128..143] 8% ++++ colors[144..159] 14% +++++++ colors[160..175] 22% +++++++++++ colors[176..191] 8% ++++ colors[192..207] 0% colors[208..223] 0% colors[224..239] 0% colors[240..255] 0%
Grayscale Portable Gray Map (PGM) • File format for *.pgm image files
C++ system() function • Requests operating system to run a command • Command may be system command or another program • Examples: // List all PgM image files in current directory system(“ls *.pgm”); // Start Eye of Gnome (eog) or xview to // display m1.pgm image system(“eog m1.pgm &”); system(“xview m1.pgm >& /dev/null &”); • Requires literal string or C-style char array as argument
Demo Program • histo.cpp • Lists available image filenames • Displays any image using xview • Loads pnm images, computes and displays histograms • Illustrates use of typedef & struct