Chapter 6: Image class – Part 2

Reviews
Shared by: Ravi
Stats
views:
574
rating:
not rated
reviews:
0
posted:
3/24/2008
language:
English
pages:
0
Chapter 6: Image class – Part 2 In chapter 4, we looked at the overview of the Image class and discussed a few functions, for filtering images, in detail using some example program. In this chapter, we will discuss a higher order image processing function like segmentation. We will first demonstrate a program for performing adaptive thresholding and then a program for performing fuzzy c mean clustering. Each of these algorithms will be explained and then the program will be illustrated to ensure complete understanding. Segmentation Segmentation is the process of separating 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 as the first step in understanding the content of an image. There are multiple techniques for performing segmentation like thresholding, clustering, watershed segmentation etc. ImageMagick and Magick++ provides functionalities to perform adaptive thresholding and fuzzy c mean clustering. Adaptive thresholding In a typical thresholding, the property that the background has a uniform intensity is used in determining the the difference between foreground and background. But in real world image, the background may not be uniform and hence a more sophisticated approach that takes in to account the change in background with location in the image is required. Adaptive thresholding studies the background in a region smaller than the size of the image and determines the threshold that separates the background from the foreground for that region. 1. //Instruction for compiling 2. //Compiling - c++ -o adaptivethreshold adaptivethreshold.cpp `Magick++-config --cppflags -cxxflags –ldflags -libs` 3. //Executing – adaptivethreshold inputfilename.ext outputfilename.ext 4. #include 5. #include 6. #include 7. 8. using namespace std; 9. using namespace Magick; 10. 11. int main(int argc,char **argv){ 12. if(strcmp(argv[1],argv[2])==0){ printf("You are trying to overwrite the file. But I stopped it.\nTry again with a different output file name\n"); 46. exit(0); 47. } 48. else{ 49. image.write(argv[2]); 50. } 51. } 52. catch( Exception &error_ ){ 53. cout << "Caught exception: " << error_.what() << endl; 54. return 1; 55. } 56. return 0; 57. } Prog 5 : adaptivethreshold.cpp : Perform adaptivethresholding on an image 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. int width,height,intensity_offset; if(argc<3){ printf("Usage: adaptivethreshold infile.ext outfile.ext width height intensity_offset"); exit(0); } //Construct the image object. Seperating image construction from the // the read operation ensures that a failure to read the image file // doesn't render the image object useless. Image image; try { // Read a file into image object image.read(argv[1]); //If width is not specified, assume the smaller region is same as the full image if(argc<3){ width = image.columns(); height = image.rows(); intensity_offset = 0; } else{ width = atoi(argv[3]); height = atoi(argv[4]); intensity_offset = atoi(argv[5]); } image.adaptiveThreshold(width,height,intensity_offset); // Write the image to a file Line 23 creates an instance of the Image class and will be referred as image and Line 27 reads the file. Lines 29-38, sets the width, height and offset depending on either the user input or to the default values of image height, width and offset of zero (0). Line 40 performs adaptive thresholding. Line 44-51 outputs the image in to the file name specified in the command prompt with an error check to prevent file overwriting. The program can be run with the following input. The width and the height of the region is chosen as 100 and 100 pixels respectively. The offset is chosen as 30 gray scale values. adaptivethreshold scan.jpg adaptivethresh.jpg 100 100 30 The input given above results in segmentation as shown in figure below. The left image is the original image before thresholding. It contains two objects VGA adaptor and a marker. The background intensity is lighter in the top region compared to the bottom. Hence a single threshold cannot be used to segment the two objects from their background and hence adaptive thresholding is an ideal technique in such cases. (Left) Original image before thresholding (Right) Image after thresholding. Note that the different background under the two objects have been segmented and identified as a single background. The width and height of the region is chosen depending on the expanse of the background region. If the background region varies considerably across the image, a smaller width and height has to be chosen. Since in this example, the variation is less frequent, a larger region size was chosen. Fuzzy C Mean Clustering The adaptive thresholding technique separates the foreground from the background. It gives an intensity of 1 for all foreground pixels and 0 for all background pixels. What if you need to classify the regions in to finer elements like identifying pixels in the left image in previous figure belong to VGA adaptor and pixels belonging to the marker and its cap. For such detailed analysis, a fuzzy c mean clustering technique is an ideal choice for segmentation. This method works by classifying the different pixels in the image as belonging to on e of the many possible clusters. The clusters share a common property that their intensities are similar. This process of clustering is repeated till all possible clusters are identified. The following program can be used to perform the clustering. 1. //Instruction for compiling 2. //Compiling - c++ -o segment_fuzzyc segment_fuzzyc.cpp `Magick++-config --cppflags -cxxflags –ldflags -libs` 3. //Executing – segment_fuzzyc inputfilename.ext outputfilename.ext 4. #include 5. #include 6. using namespace std; 7. using namespace Magick; 8. //g++ -o segment segment_fuzzyc.cpp `Magick++-config --cppflags --cxxflags --ldflags --libs` 9. //segment infile.ext outfile.ext cluster_threshold smoothing_threshold 10. int main(int argc,char **argv){ 11. 12. double cluster_threshold,smoothing_threshold; 13. 14. if(argc<3){ 15. printf("Usage: segment infile.ext outfile.ext cluster_threshold smoothing_threshold"); 16. exit(0); 17. } 18. 19. if(argc<4){ 20. cluster_threshold = 1.0; 21. smoothing_threshold = 1.5; 22. } 23. else if(argc<5){ 24. cluster_threshold = atof(argv[3]); 25. smoothing_threshold = 1.5; 26. } 27. else{ 28. cluster_threshold = atof(argv[3]); 29. smoothing_threshold = atof(argv[4]); 30. } 31. 32. Image image; 33. 34. try { 35. //Read a file into image object 36. image.read(argv[1]); 37. 38. image.segment(cluster_threshold,smoothing_threshold); 39. 40. //Write the image to a file 41. image.write(argv[2]); 42. } 43. catch( Exception &error_ ){ 44. cout << "Caught exception: " << error_.what() << endl; 45. return 1; 46. } 47. return 0; 48. } Prog2: segment_fuzzyc.cpp: Performing Fuzzy C Mean clustering to an image To run the program, use the following command, segment_fuzzyc scan.jpg segment_fuzzyc.jpg 2.0 3.0 Line 19-30 sets the parameters for fuzzy c mean clustering. If all the values are not provided in the command line, a default value of 1.0 and 1.5 are assumed for the cluster threshold and the smoothing threshold. Line 38 performs the fuzzy c mean clustering using the segment function. The image below illustrates the effect of performing fuzzy c mean clustering to an image. The left image is the original image and the right image is the fuzzy c mean clustered image. The clustered image has multiple regions corresponding to the VGA adaptor, marker body, marker cap and their respective backgrounds. (Left) Original image before segmentation (Right) Image after C Mean clustering. The clustering operation has produced multiple regions that have homogeneous characteristics. In this chapter, we discussed the one of the important image processing function, segmentation and illustrated it with a few examples. In the next chapter, we will look at drawable class and study methods to create vector graphics like line, circle etc and also print text which would later be used in advanced tutorials in creating captchas.

Shared by: Ravi
Other docs by Ravi
Life on a pendulum
Views: 17  |  Downloads: 0
Abaqus_python
Views: 706  |  Downloads: 5
Magick++ chapter 1 to 6
Views: 720  |  Downloads: 20
Chapter 5: Exception class
Views: 492  |  Downloads: 4
Chapter 4: Image class – Part 1
Views: 561  |  Downloads: 8
Chapter 3: Introduction to Magick++ classes
Views: 594  |  Downloads: 6
Chapter 2: Compiling Magick++ program
Views: 833  |  Downloads: 9
Introduction to Magick++
Views: 897  |  Downloads: 2
Related docs
chapter 2
Views: 9  |  Downloads: 0
CHAPTER 3, PART 7
Views: 6  |  Downloads: 0
chapter 2 the sky
Views: 1  |  Downloads: 0
Chapter #2
Views: 11  |  Downloads: 0
chapter 2
Views: 4  |  Downloads: 0
CHAPTER 2
Views: 0  |  Downloads: 0
chapter 2
Views: 6  |  Downloads: 0
chapter 2
Views: 0  |  Downloads: 0
chapter 2 algorithm analysis
Views: 8  |  Downloads: 1
Week 1, Part 2 Apartheid
Views: 1  |  Downloads: 0
Chapter 1, Class Society & State - 708417110
Views: 0  |  Downloads: 0
class #6
Views: 0  |  Downloads: 0
class 3doc
Views: 2  |  Downloads: 0
PART 2
Views: 20  |  Downloads: 0
PART A
Views: 0  |  Downloads: 0