Dec 15, 2017 - C++ Source Code : A Monograph ... This monograph presents a comprehensive reference of C++ source code for ..... continue"; getch();.
Ghazali Sulong, Wan Nural Jawahir Hj Wan Yussof, Abdul Aziz K. Abdul Hamid, Rozniza Ali, Muhammad Suzuri Hitam, Nur Hafizah Ghazali and Nurulhuda Ghazali
Digital Image Processing with C++ Source Code : A Monograph December 15, 2017
To our parents
Preface
This monograph presents a comprehensive reference of C++ source code for digital image processing. The main objectives of this monograph are to assist developers in utilizing the fundamental concepts and methodologies of digital image processing, and to create programs that can be used for research purposes. Image processing is a method or technique to convert an image into digital form and execute some operations on it, in order to acquire an enhanced image or to extract some useful information from it. Image processing also can be described as a processing of images using mathematical operations for which the input is an image such as a photograph while the output of image processing may be either an image or a set of characteristics related to the image. Most of the image processing techniques involve brightness, inverse, bitplane slicing, halftoning, dithering, convolution, low pass, high pass filter, high boost, median filter, edge detection (Prewitt and Sobel), shrinking and enlargement. These techniques can be applied on images for modification or enhancement.
Malaysia, December 2017
Ghazali Sulong Wan Nural Jawahir Wan Yussof Abdul Aziz K. Abdul Hamid Rozniza Ali Muhammad Suzuri Hitam Nur Hafizah Ghazali Nurulhuda Ghazali
Acknowledgement
The completion of this undertaking could not have been possible without the participation and assistance of so many people whose name may not all be enumerated. We would like to express our gratitude to everyone who has encouraged and motivated us along the way to write this monograph. This monograph represents our experience in teaching digital image processing subjects at both undergraduate and postgraduate levels in these past few years. Our deepest appreciation goes to our colleagues and mentors from Universiti Malaysia Terengganu, Universiti Teknologi Malaysia, Universiti Malaysia Perlis and Universiti Teknologi MARA (Jasin Campus) for their guidance and ideas in order to help teaching the students in a simple and effective way. Finally, we would like to convey our gratefulness to our supportive and loving family. They all kept us going and this monograph would not have been possible without them.
December 2017
Ghazali Wan Nural Jawahir Abdul Aziz Rozniza Muhammad Suzuri Nur Hafizah Nurulhuda
Contents
1
Extracting Data from TIFF file . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.1.1 TIFF Image File Format . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1.3 Output of File Extraction Result . . . . . . . . . . . . . . . . . . . . . . . . . . Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2
Convert Image to Raw Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.3 Output Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3
Brightness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3.3 Output Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
17 17 17 20
4
Bit-Plane Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4.3 Output Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
21 21 21 27
5
Bit 5.1 5.2 5.3
31 31 31 39
6
Image Inversion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 6.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
Slicing (Binary) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Output Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1 1 1 4 4 6 6
XII
Contents
6.3 Output Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 7
Patterning . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
49 49 49 53
8
Dithering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
57 57 57 62
9
Greyscale Level Enhancement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
65 65 65 69
10 Convolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
71 71 71 74
11 Low Pass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
77 77 77 80
12 High Pass Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
83 83 83 88
13 High Boost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
91 91 91 96
14 Median Filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 14.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 14.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 14.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 15 Edge Detection (Prewitt) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 15.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 15.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 15.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Contents
XIII
16 Edge Detection (Sobel) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 16.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 16.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 16.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 17 Enlargement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 17.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 17.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 17.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 18 Shrinking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 18.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 18.2 Source Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 18.3 Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
List of Figures
1.1 1.2 1.3
Arrangement of Header, IFD and Data in a TIFF file . . . . . . . . . Input image in tiff format . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Program Output to extract Tiff file . . . . . . . . . . . . . . . . . . . . . . . . .
2 6 7
2.1 2.2
Program Output to Convert Image in .raw File . . . . . . . . . . . . . . 15 Conversion from 2.2(a) to 2.2(b) . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3.1 3.2
Program output to include brightness to the original image . . . . 20 Conversion from 3.2(a) to 3.2(b) with brightness level=50 . . . . . 20
4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9
Program Output for Bit 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Program Output for Bit 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Program Output for Bit 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Program Output for Bit 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Program Output for Bit 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Program Output for Bit 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Program Output for Bit 7 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Program Output for Bit 8 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Bit-Plane Slicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5.1 5.2 5.3
Bit-Plane Slicing Concept . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Sample input image (Bitplane 1) . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Output of input image in value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
6.1 6.2
Program Output to inverse input image . . . . . . . . . . . . . . . . . . . . . 47 Image inversion from 6.2(a) to 6.2(b) . . . . . . . . . . . . . . . . . . . . . . . 47
7.1 7.2
Program Output for patterning . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Patterning from 7.2(a) to 7.2(b) . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55
8.1 8.2
Program Output for Dithering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 Dithering Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
27 28 28 28 28 29 29 29 30
XVI
9.1 9.2
List of Figures
Program Output for Gray Level Enhancement . . . . . . . . . . . . . . . 69 Gray Level Enhancement Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
10.1 Program Output for Convolution . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 10.2 Convolution Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 11.1 Program Output for Low Pass Filtering . . . . . . . . . . . . . . . . . . . . . 81 11.2 Low Pass Filtering Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 12.1 Program Output for High Pass Filtering . . . . . . . . . . . . . . . . . . . . . 89 12.2 High Pass Filtering Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 13.1 Program Output for High Boost . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 13.2 High Boost Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 14.1 Program Output for Median Filter . . . . . . . . . . . . . . . . . . . . . . . . . 103 14.2 Median Filtering Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 15.1 Program Output for Edge Detection (Prewitt) . . . . . . . . . . . . . . . 112 15.2 Edge Detection Result (Prewitt) . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 16.1 Program Output for Edge Detection (Sobel) . . . . . . . . . . . . . . . . 121 16.2 Edge Detection Result (Sobel) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 17.1 Program Output for Image Enlargement . . . . . . . . . . . . . . . . . . . . 126 17.2 Enlargement Result . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 18.1 Program Output for Median Filter . . . . . . . . . . . . . . . . . . . . . . . . . 135 18.2 Shrinking Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
1 Extracting Data from TIFF file
1.1 Introduction Digital image processing involves the processing of digital images using specific image processing algorithm. Digital images exist in many file formats. Example of popular file formats are TIFF, JPG, PNG, GIF, BMP and raw image file. An image file format may be store data either in uncompressed, compressed or vector format depending on the purpose of the data storage. This is the reason why there are many types of image file formats exist. Among the popular file format that is used by many practitioners is TIFF file format. TIFF or sometimes knwn as TIF stands for Tagged Image File format. It is a flexible image file format where it uses almost exclusively as a lossless image storage format with no compression at all. Thus, a TIFF file format can be very large in size due to a lot of image data information were stored. A TIFF file can be identified by a file with a ”.tiff” or ”.tif” file name extension. It is flexible format that normally saves 8 bits or 16 bits per color (red, green, blue). TIFF contains tag which provides information of the images. Tag provides information such as width, length and number of pixel. Data in TIFF is arranged by Header, Image File Directories (IFD) and Data. 1.1.1 TIFF Image File Format TIFF describes a tag-based file format for storing and interchanging digital images. History: • TIFF file format was developed by Aldus Corp. (1986) • Rev. 4.0 (1987). Rev. 5.0 (1988). Rev. 6.0 (1992)
2
1 Extracting Data from TIFF file
Features: • Support bilevel, grayscale, palette-colour, and full-colour image data in several colour space. • Extensibility-capable adapting new type of image • Portable-support various environments • Revisable-edit, change & process (not read only) Tag refers to files basic structure: • • • • • •
Tag provides information : Width Length Number of pixel Store in Tag Directory Tag Directory has not set length/ number just a pointer - so it can grow and adapt in future
Image consists of 2D array of numbers, which stores colour intensity of each pixel location. TIFF format consists of Header, Image File Directories (IFD) and Data, which are arranged according to the following order:
Fig. 1.1: Arrangement of Header, IFD and Data in a TIFF file
• File Header: – First 8 bytes of the file – Same format in all TIFF files and constant – Contains first IFD offset • Image File Directories (IFD) – Provides image information such as image • Data – Contains real data for specific information such as pixel value and etc – Located after or before IFD
1.1 Introduction
File Header • Byte Order - How data will be read from a file – SB-Intel($4949) little-endian – (Byte order is from LSB to MSB) – SB-Motorola($4D4D) Big-endian – Byte order is from MSB to LSB) • Byte order is from MSB to LSB) • FD Offset - First IFD offset or address • SB-must do swap 2 or 4 byte
• FD contains Directory Entry (DE)
3
4
1 Extracting Data from TIFF file
• The first 2 bytes of IFD, it provides number of DE, m • DE has 12 byte each • So in each IFD, total number of DE is – m x 12 • DE format: Tag (2 bytes), Type (2 bytes), Length (4 bytes) and Value (4 bytes) • Tag - First 2 bytes • Type of information - Second 2 bytes which provides TIFF data type – TIFF data type · 1 = byte 8 bit unsigned integer · 2 = ASCII 8 bit bytes that store ASCII codes (last byte must be null) · 3 = short 16 bit (2 byte) unsigned integer · 4 = long 32 bit (4 byte) unsigned integer · 5 = rational 2 long:first is numerator. Second denominator • Length of the information – 4 bytes – Depend on Tag – variable real size – S = Length x Size that define from Type • Value – 4 bytes – Depend on Tag – variable real size – S = Length x Size that define from Type
1.2 Source Code The following are the source code listing for extracting information from TIFF file format #include #include #include using namespace std; #define ROW 10000 #define COLUMN 10000 short unsigned int rawData[ROW][COLUMN]; struct Header { short int byteOrder; //TO DETERMINE WHETHER INTEL OR MOTOROLA short int fileType; // FILE TYPE
1.2 Source Code short int offset; // IFD ADDRESS(TO DETERMINE FIRST DIRECTORY ENTRY) short int numberOfDE; }; struct DE //FOR DIRECTORY ENTRY { short int tag; //2 BYTE short int type; //2 BYTE long int length; //4 BYTE long int value; //4 BYTE };
struct Image { long int long int long int };
//FOR IMAGE width; length; dataSize;
void readTiff(ifstream&, Header&, DE*, Image&); int main() { int noOfTry = 0; char fileName[100],fileName2[50]; ifstream fileTiff; Image img; Header head; DE de; while(true){ do{ cout