Chapter 4: Image class – Part 1 In this previous chapter, we looked at the overview of the various classes available in Magick++. In this chapter, we will focus on the Image class, the base class and learn its features and some of it functionalities. We will compliment these with example programs that will aid in the understanding of its use. Image class As indicated earlier, Image class is the main class in Magick++. It can 1. Read and Write almost all common formats including some field domain specific formats like dicom 2. Create new images 3. Perform image filtering1 4. Perform geometric image transformation 5. Improve quality of photographs 6. Perform image modification like slicing, bordering etc 7. Segmentation2 8. Set image attributes 9. and more such features which could not arranged in to this list Image class should be used with other classes like BLOB, STL, Color, Drawable. Image class can read single frame images like JPEG, PNG etc while STL can read multiframe images like TIFF, GIF etc. Image class is included by using the header “#include ”. The command “using namespace Magick;” is specified to ensure that Magick++ functions and classes get called with the appropriate namespace. The programmer can also use “Magick::” before these functions. Image classes are best instantiated automatically (using stack) instead of using pointers (via new). This ensures that Magick++ keeps better bookkeeping and hence lesser memory leaks.
Reading and Writing Images The following program illustrates the use of Magick++ using a simple example. An image file will be read and written back to the disc using a different file name.
1 Filtering refers to a combination of operations like removal of noise, sharpening edges, convolving images to produce blurring etc 2 Segmentation is the process of seperating the contents of an image in to parts by analyzing characteristics of the image like color, texture or based on correlation with real world objects.
1. //Instruction for compiling 2. //Compiling - c++ -o readwrite readwrite.cpp `Magick++-config --cppflags --cxxflags --ldflags -libs` 3. //Executing - readwrite infile.ext outfile.ext 4. 5. 6. #include 7. #include 8. using namespace std; 9. using namespace Magick; 10. 11. int main(int argc,char **argv){ 12. 13. //If all arguments are not supplied, print error 14. if(argc<3){ 15. printf("Usage: readwrite infile.ext outfile.ext\n"); 16. exit(0); 17. } 18. 19. //Create the class and read the file 20. Image master; 21. 22. //Read the inputfile 23. master.read(argv[1]); 24. 25. //Display image 26. master.display(); 27. 28. //Write the file to a different format 29. master.write(argv[2]); 30. 31. return 0; 32. }
Prog1 : Readwrite.cpp : Read and Write image files Line 20 creates an instance of the Image class and will be referred as master. Line 23 reads the file and line 26 displays it. In system not setup for X Windows session, line 26 has to be commented to ensure there is no error. Line 29 outputs the image in to the file name specified in the command prompt.
Add noise to an image In the next program, we will add noise to an image using the addNoise function. This is one of the simplest example of a function that can be called in the Image class.
1. //Instruction for compiling 2. //Compiling - c++ -o addnoise addnoise.cpp `Magick++-config --cppflags --cxxflags --ldflags -libs` 3. //Executing - addnoise inputfilename.ext outputfilename.ext 4. 5. #include 6. #include 7. using namespace std; 8. using namespace Magick; 9.
10. int main(int argc,char **argv){ 11. 12. if(argc<3){ 13. printf("Usage: addnoise inputfilename.ext outputfilename.ext\n"); 14. exit(0); 15. } 16. 17. //Create the class and read the file 18. Image master(argv[1]); 19. 20. //Add Poisson Noise. This can be any other noise type as well. Refer full documentation 21. master.addNoise(ImpulseNoise); 22. 23. //Write the file to a different format 24. master.write(argv[2]); 25. 26. master.display(); 27. 28. return 0; 29. }
Prog2: addnoise.cpp: Adding impulse noise (also called Salt and Pepper noise) to an image Line 21 is used to add impulse noise in the image. There are other types of noise available as constants defined in Magick++. For full details, refer the documentationT3. The image below illustrates the effect of adding impulse noise to the image. The left image is the original image and the right image is the noisy image, characterized by its salt and pepper style of noise.
(Left) Original image before adding impulse noise (Right) Image after adding impulse noise. Note the characteristic salt and pepper formation in the image.
Removing noise from image The program below is used to remove the impulse noise that was added in the previous image. A median filter is used to remove the impulse noise. So in this program, we will use the median filter on the noisy image created using program 2.
3 The website for viewing complete documentation - http://www.imagemagick.org/Magick++/Documentation.html
1. //Instruction for compiling 2. //Compiling - c++ -o removenoise removevoise.cpp `Magick++-config --cppflags --cxxflags -ldflags --libs` 3. //Executing - removenoise infile.ext outfile.ext 4. 5. #include 6. #include 7. using namespace std; 8. using namespace Magick; 9. 10. int main(int argc,char **argv){ 11. 12. if(argc<3){ 13. printf("Usage: removenoise inputfilename.ext outputfilename.ext\n"); 14. exit(0); 15. } 16. 17. //Create the class and read the file 18. Image master(argv[1]); 19. 20. ///Remove ImpulseNoise using median filter. The input to the function is the radius of the circular neighborhood considered for median filter 21. master.medianFilter(1.0); 22. 23. //Write the file to a different format 24. master.write(argv[2]); 25. 26. master.display(); 27. 28. return 0; 29. }
Prog3: removenoise.cpp: Removing impulse noise added in the previous program Line 21 uses the medianFilter function to remove the impulse noise. The imput to the function is the radius of the neighborhood in which the median filter is applied. Higher values will result in more blurring of the filtered image.
(Left) Original image before removing impulse noise (Right) Image after removing impulse noise. The median filter also resulted in some blurring in the image.
In this chapter, we looked at the different features of the Image class and worked in detail to read, write and filter images. Image class provides many more functions to perform complex operations on the image. Some of these functions require the use of primary classes like Drawables, Blob etc and also secondary classes like Color, TypeMetric etc. Please refer to the documentation for more details. In one of the subsequent chapter, we will discuss the other important image processing function, segmentation and will provide a few examples for illustration. But before we proceed with more details of the Image class, we will work on the Exception class, in-order to provide a method to identify errors in our program at run time.