MEDICAL IMAGING THROUGH HISTOGRAM EQUALIZATION AND CANNY EDGE DETECTION METHOD MAZLINDA BINTI IBRAHIM This dissertation is submitted as a partial fulfillment for the award of the Master of Science (Mathematics) Faculty of Science Universiti Teknologi Malaysia NOVEMBER 2009 To my beloved family ma, ayah, abe kie, akak& abe yahya, lina, juwe, udin, and asri, not to forget my friend, Ita, love you all so much! ACKNOWLEDGEMENT With this opportunity, I would like to express my appreciation to Prof. Dr Shaharuddin Bin Salleh as my supervisor for his guidance and supportive suggestion during the whole development of this project. I also would like to express deepest thanks to my family and my friends. Last but not least, I am grateful towards all authorities which helped directly or indirectly for this project. Thanks to everyone. ABSTRACT Medical images edge detection is an important work for object recognition of the human organs. It is an important pre-processing step in surgery planning to detect the location and size of cancerous tissues or tumor. Today, many edge detection methods were introduced in order to obtain the better result. One of the most popular methods is Canny edge detector. But, Laplacian and Sobel method are still taken into account to the researcher of edge detection. Computer Tomography scan is one of the modalities in medical imaging. Image reconstruction methods were used to obtain the final images in CT scan. Test images from CT scan were used to apply the Canny, Laplacian and Sobel methods using simulation with C++ programming. Comparisons were made and the result show that the combination of Canny edge detector and histogram equalization perform well to the tested images. ABSTRAK Pengesanan rangka dalam imej perubatan adalah satu langkah yang penting untuk pengenalpastian organ-organ manusia. Ia juga merupakan langkah awal yang penting dalam perancangan pembedahan untuk mengenalpasti lokasi dan saiz tisu yang berkanser ataupun tumor. Hari ini, banyak kaedah untuk pengesanan rangka diperkenalkan yang bertujuan untuk mendapat hasil yang lebih baik. Salah satu kaedah yang terkenal adalah pengesanan rangka Canny. Tetapi kaedah Sobel dan Laplacian masih mendapat perhatian daripada pengkaji pengesanan rangka. Pengesan komputer tomografi adalah salah satu kaedah dalam memperoleh imej perubatan. Kaedah pembangunan semula imej digunakan untuk mendapat imej terakhir dalam pengesan komputer tomografi. Imej yang diuji adalah daripada pengesan komputer tomografi untuk mengaplikasikan kaedah Canny, Laplacian dan Sobel menggunakan simulasi dengan pengaturcaraan C++. Perbandingan dibuat dan keputusan menunjukan kombinasi pengesanan rangka Canny dan kesamarataan histogram bertindak dengan baik keatas imej yang diuji. TABLE OF CONTENTS CHAPTER SUBJECT PAGE COVER i DECLARATION ii DEDICATION iii ACKNOWLEDGEMENT iv ABSTRACT v ABSTRAK vi TABLES OF CONTENTS vii LIST OF TABLES xi LIST OF FIGURES xii LIST OF SYMBOLS xiv LIST OF APPENDICES xv CHAPTER 1 INTRODUCTION 1.1 Introduction 1 1.2 Problem Statement 3 1.3 Objectives 4 1.4 Scope 5 1.5 Project Outline 5 CHAPTER 2 EDGE DETECTION 2.1 Introduction 7 2.2 Image Processing 8 2.3 Edge Detection 8 2.4 Edge Detection via Image Spatial Gradients 12 2.5 Laplacian Edge Detection 15 2.6 Sobel Edge Detection 16 2.7 Canny Edge Detection 17 2.7.1 Detection Criteria 19 2.7.2 Localization Criteria 20 2.7.3 Only Response to Single Edge 22 2.7.4 Detector for Step Edge 24 2.8 Histogram Equalization 27 2.9 Conclusion 29 CHAPTER 3 MEDICAL IMAGING 3.1 Introduction 30 3.2 Image Reconstruction from Projection 31 3.3 X-ray or Computer Tomography 35 3.3.1 37 A Basic Model for Tomography 3.4 Radon Transform 40 3.5 Conclusion 44 CHAPTER 4 MODEL AND SIMULATION 4.1 Introduction 45 4.2 Algorithms for Laplacian and Sobel Edge Detection 46 4.3 Algorithm for Canny Edge Detection 48 4.4 Histogram Equalization 50 4.5 Simulation 51 4.6 X-ray Tomography Images 54 4.7 Conclusion 56 CHAPTER 5 RESULT AND DISCUSSION 5.1 Introduction 57 5.2 Result of Edge Detection 58 5.2.1 Laplacian 58 5.2.2 Sobel Edge Detection 59 5.2.3 Canny Edge Detection 61 5.3 Histogram Equalization 66 5.5 Discussion 67 5.6 Conclusion 68 CHAPTER 6 CONCLUSION DAN RECOMMENDATIONS 6.1 Introduction 69 6.2 Conclusion 70 6.3 Recommendations 71 REFERENCES 72 APPENDIX 75 LIST OF TABLES TABLE NO. 3.1 SUBJECT Attenuation coefficient of human tissues for 100-keV x-ray. PAGE 37 LIST OF FIGURES FIGURE NO. SUBJECT PAGE 2.1 Ideal step edge. 10 2.2 Ramp in step edge. 10 2.3 Histogram equalization. 27 3.1 Flat region, showing a simple object 31 in x-ray tomography. 3.2 Result of back projection of simple object. 32 3.3 The beam and detector rotated about 90D 33 and back projection. 3.4 The sum of two projections. 33 3.5 Reconstruction with 1,2,3,4 and 32 back projections.34 3.6 CT scanner configuration with a true 36 fan beam of x-ray. 3.7 Normal representation of a straight line. 40 3.8 Geometry of a parallel x-ray beam. 41 3.9 Shepp-Logan phantom and its sinogram. 42 3.10 Flow of the filter back projection algorithm. 43 4.1 Algorithm for Laplacian edge detection. 46 4.2 Algorithm for Sobel edge detection. 47 4.3 Algorithm for smoothing with Gaussian 48 and differentiation. 4.4 Algorithm for non maximal suppression. 49 4.5 Algorithm for hysterisis thresholding. 50 4.6 Algorithm for histogram equalization. 51 4.7 Layout of the simulation. 53 4.8 Simulation of Sobel Multidirectional with 53 Threshold=500. 4.9 Sample image in x-ray tomography. 55 4.10 Image after cropping. 55 5.1 Laplacian edge detection of the liver. 58 5.2 Edge detection using Sobel of the liver. 59 5.3 The result using Sobel multidirectional 60 edge detection method. 5.4 Image after histogram equalization. 61 5.5 Gaussian smoothing of the liver. 62 5.6 Image differentiation of the liver. 63 5.7 Image after locate the local maxima pixels. 64 5.8 Image after thresholding using hysterisis. 65 5.9 Canny edge detection without performing 66 histogram equalization. LIST OF SYMBOLS f ( x, y ) - Two dimensional light intensity function - Convolution I - Convolution kernel Sx - Horizontal kernel Sy - Vertical kernel I ( x) - Image function in one dimension G ( x) - Edge function in one dimension n( x) - Additive white Gaussian noise in one dimension W - Operator width SNR - Signal to noise ratio xave - Average distance between zero crossing xzc - Mean distance between zero crossings A - Amplitude of the step edge V - Standard deviation or sigma in Gaussian function I - The intensity of the x-ray beam P - Linear attenuation coefficient G - Impulse function { f ( x, y )} - Radon transform LIST OF APPENDIX APPENDIX A SUBJECT Coding of the simulation. PAGE 75 CHAPTER 1 INTRODUCTION 1.1 Introduction Image means picture. It can be analogue or digital. An analogue image is an image which exists on a photographic film, video magnetic tape or signal from a broadcast transmission (Joshi, 2001). Meanwhile, a digital image is a representation of two dimensional image using ones and zeros or in binary numbers. Digital image data is represented in the form of grey scale, corresponding to the intensity. It can be processed using the digital computer. The term digital image usually refers to raster images also called bitmap images. Raster images have a finite set of digital values called picture elements or pixels. Interest in digital image processing techniques dates back to the early 1920’s when digitized pictures of world news events were first transmitted by submarine cable between New York and London. Application of digital image processing concepts, however did not become widespread until the middle 1960’s, when third generation digital computers began to offer the speed and storage capabilities required for practical implementation of image processing algorithms. Since then, this area has experienced vigorous growth, having been a subject of interdisciplinary study and research in such fields as engineering, biology, computer science, medicine and chemistry. The result of these efforts have established the value of image processing techniques in a variety of problems ranging from restoration and enhancement of space-probe pictures to processing of fingerprints for commercial transactions. Image processing is an area of study for manipulating and modifying images. Image processing involves techniques for image enhancement, restoration and smoothing by moving, copying, deleting and modifying the contents of the pixels in the image. Before an object can be extracted from scenery, a way of assessing it shape and size is needed. This can be done using edge detection or segmentation, both of which are low-level image processing task, or their combination (Gudmundsson et all, 1998). The success of the higher level recognition is, therefore highly dependent on this lower level process. Edge is the boundary between two regions, with relatively distinct grey level properties (Joshi, 2001). In a continuous image, a sharp intensity transition between neighbouring pixels is considered as an edge. Laplacian and Sobel are classical mathematical methods for the edge detection. The Laplacian edge detection method uses a two dimensional linear filter to approximate the second order derivative of pixel values of the image. The Sobel edge detection method uses two dimensional linear filters to process vertical edges and horizontal edges separately. One of the popular methods in edge detection is the Canny Edge Detector (Heath et all, 1997). This method was introduced by John Canny on 1986. Due to the important of edge detection in image processing many methods were developing. In real world machine vision problems, numerous issues such as variable scene illumination make edge and object detection difficult. There exists no universal edge detection method which works well under all condition (Panetta and Wharton, 2008). The past 25 years have seen remarkable developments in medical imaging technology. Universities and industry have made huge investments in inventing and developing the technology needed to acquire images from multiple imaging modalities, such as CT, MRI, and Ultrasound. Every modality has its own working principle and system that enable us to obtain the final images. For example, X-ray computed tomography (CT) images are sensitive to tissue density and atomic composition, and the x-ray attenuation coefficient (Hajnal et all, 2001). Image reconstructions methods were used in order to obtain the final images in the medical imaging modalities. As an example, in x-ray tomography, image reconstruction from projection method such as the filtered back projection method was applied. After the image was reconstruct, we now can apply the image processing techniques as desired. 1.2 Problem Statement Given an image of size m u n . In what way can the image be reconstructed so as to produce binary versions which clearly display the high and low intensity pixels? The binary version is an image with black and white colour which the white colour shows the boundary of the object in the image. The white color indicates the edges and the black color indicate no edges. In medical field, the binary version of the image is a preliminary stage in any medical decision. The selection of edge detection method is also an important step because not all method can detect the desire edges. One of the available methods in edge detection is Canny edge detection. Canny edge detector used the first derivative of a Gaussian G ( x) as the optimal filter where; § x2 · G ( x) exp ¨ 2 ¸ © 2V ¹ where V is the standard deviation of the Gaussian function and x is the value from origin in one dimension case. Then, the edge point is defined to be a local maximum in the direction of n of the operator Gn applied to the image I , where Gn n is normal to the edge direction. At local maximum, we have w Gn I wn n G where 0 . Then the problem becomes to find the location of local maxima in the image. Since an image contains higher number of pixel, we need to use the simulation to find the edges. 1.3 Objectives The objectives of this study include: a) To apply the Laplacian , Sobel and Canny edge detection method on the medical images. b) To develop a simulation of Laplacian, Sobel and Canny edge detection method. c) To compare the performance of Laplacian, Sobel and Canny edge detection using visual assessment. 1.4 Scope This project use Laplacian, Sobel and Canny edge detector for detecting the edges. Comparisons were made to determine which method is the best using visual assessment. Laplacian and Sobel edge detection have one parameters meanwhile Canny edge detection has three parameters. The parameters values are just any values and we do not study the best parameters values for each method. We apply the method to the medical images from x-ray tomography. Some discussion about image reconstruction from projection in x-ray tomography also included. The test image is the liver image which we aim to locate a tumor. 1.5 Project Outline This project contains six chapters. The presentation of this project started in Chapter 1 with discussion of problem statement and objectives. It also includes scope and project outline. Chapter 2 discusses the edge detection methods. These include the Laplacian and Sobel method first. The chapter also introduces the Sobel multidirectional, the Canny edge detection method and histogram equalization. Chapter 3 begins with a look at medical images. The discussion also includes image reconstruction from projection in x-ray tomography. This lead to Radon transform for filter back projections in x-ray tomography. Then, in Chapter 4, our model will be presented, which apply edge detection on medical images. The simulation was used using C++ programming. We tested the model using medical image from x-ray tomography. Result and discussion presented in Chapter 5. Then Chapter 6, conclude the entire project. Supportive recommendations are given so that interested readers can follow the details. CHAPTER 2 EDGE DETECTION 2.1 Introduction This chapter discusses the background of image and edge detection. The concept of image mathematically and detail on the methods of edge detection were also given. Laplacian edge detection method uses a two dimension linear filter to approximate the second order derivative of the pixel value. Meanwhile, Sobel edge detection method uses two dimension linear filters to process vertical and horizontal edges separately. Canny edge detector uses the first derivative of a Gaussian as the optimal filter. Canny, 1986 defined three performance criteria in order to come out with Canny edge detector. Histogram equalization is a technique in image enhancement which aims to equally distribute the grey level in the image. 2.2 Image Processing According to Baxes, 1994 digital image processing operations can be broadly grouped into five fundamental classes. Edge detection is the one in the class of image enhancement. The five classes are image enhancement, image restoration, image analysis, image compression, and image synthesis. Image refers to a two dimensional light intensity function f ( x, y ) , where x and y denote the spatial coordinates and the value f is proportional to the brightness or grey level of the image at the point (Rafael, 1977). It is sometimes useful to view the image function in perspective with the third axis being brightness. A digital image is an image f ( x, y ) which has been discretized both in spatial coordinates and in brightness. We may consider a digital image as a matrix whose row and column indices identify a point in the image and the corresponding matrix element identifies the grey level at that point. The elements of such a digital array are called image element, pictures elements, pixels or pels. 2.3 Edge Detection Edge detection is one of the most commonly used operations in image analysis and there are probably more algorithms in the literature for enhancing and detecting edges than any other single subject. The reason for this is that edges form the outline of an object. Edge is the boundary between two regions, with relatively distinct grey-level properties (Joshi, 2001). In a continuous image, a sharp intensity transition between neighbouring pixels is considered as an edge. Edge is the boundary between an object and the background, and indicates the boundary between overlapping objects (Parker, 1997). This means that if the edges in an image can be identified accurately, all of the object can be located and basic properties such as area, perimeter, and shape can be measured. In medical field, we can study about the volume and growth of the tumor using edge detection. Since computer vision involves the identification and classification of objects in an image, edge detection is an essential tool. Edgels are edge elements that have significant local variations in image brightness, characterized by the position x and the orientation T of the brightness variation. Sequence of edgels forming smooth curve that is edge. Every pairs of pixels with differing intensities could be considered as potential edge. Edge detection contributes significantly to algorithms for feature detection, segmentation and motion analysis. Edge detection can be used to early detect the existence of cancerous tissues in a patient. Ideal step edge illustrated in Figure 2.1 adapted from Parker,1997 is the most common type of edges. In this one dimensional example, the edge is simply a change in grey level occurring at once specific location that is at pixel 10. The greater the change in the level, the easier the edge is to detect, but in the ideal case this can be seen quite easily. Other types of edges are ridge and roof edges (Canny, 1986). In Figure 2.1 and 2.2 the dotted line shows where the image was sliced to give the illustrated cross section. Figure 2.2 shows that the change of grey level extent some number of pixels. The first complication occurs because of digitization. It is unlikely that the image will be sampled in such a way that all of the edges happen to correspond exactly with a pixel boundary. The actual position of the edge in Figure 2.2 is considered to be the centre of the ramp connecting the low grey level to the high one. This is the ramp in the mathematical word only, since after the image has been jagged appearance of a staircase. Figure 2.1: Ideal step edge. Figure 2.2: Ramp in step edge. The second complication is the problem of noise. Due to a great many factors such as light intensity, type of camera and lens, motion, temperature, atmospheric effects, dust and other, it is very unlikely that two pixels that correspond to precisely the same grey level in the scene will have the same level in the image. Noise is a random effect and is characterizable only statistically. The result of noise on the image is to produce a random variation in level from pixel to pixel, and so the smooth lines and ramps of the ideal edges are never encountered in real images. Noise is usually expressed as a probability distribution with a specific mean and standard deviation. As example of noise is the Gaussian white noise. This noise characterized by the normal distribution with zero mean and specified standard deviation. Edge detection is carried out through a linear operation call convolution, or sometimes called filtering. A pixel value of intensity f ( x, y ) change its value to f '( x, y ) through a convolution kernel K , as shown in the Equation 2.1. (Salleh, 2008). f '( x, y ) f ( x, y ) K (2.1) where denote convolution, K is the kernel associate to the edge detection method chosen, x and y are the spatial coordinate in two dimension and f is correspond to the grey level. 2.4 Edge Detection via Image Spatial Gradients Since an edge is defined by a change in grey level, an operator that is sensitive to this change will operate as an edge detector. A derivative operator does this, one interpretation of a derivative is the rate of change of a function, and the rate of change of the grey levels in an image is large near an edge and small in constant areas. Since image is in two dimensions, we need to consider the level changes in two directions. For this reasons, the partial derivatives of the image function f ( x, y ) are used with respect to the x and y . The gradient of an image function f ( x, y ) in terms of directionally oriented spatial derivatives as f ( x , y ) ª wf ( x, y ) º « wx » « » « wf ( x, y ) » «¬ wy »¼ wf ( x ) wx (2.2) To calculate the derivatives in Equation 2.2 we can use the forward difference operator with respect to x and y as below wf ( x, y ) f ( x 'x, y ) f ( x, y ) | wx 'x wf ( x, y ) f ( x, y 'y ) f ( x, y ) | wy 'y (2.3) (2.4) Since an image is discrete, we used the differences instead of derivative at a pixe and thisl is approximated by the difference in grey levels over some local region. Note that the above operator is correspond to the correlation of the discretized image function with the operators of the form > 1 1@ and ª1º « 1» ¬ ¼ Respectively where we take 'x 1 and 'y 1 The one dimensional forward difference operator is defined as D1 ( x) f ( x 'x ) f ( x ) 'x (2.5) The central difference approximation is D2 ( x ) f ( x 'x) f ( x 'x) 2'x (2.6) D2 ( y ) f ( y 'y ) f ( y 'y ) 2'y (2.7) Similarly, D2 ( x) and D2 ( y ) have centered nature and can be implemented by convolving the image function with masks > 1 0 1@ and ª1º «0» « » «¬ 1»¼ Which operator is used to compute the gradient, the resulting vector contain information about how strong the edge is at the picture and what direction is. The magnitude of the gradient vector is the length of the hypotenuse of the right triangle having sides and wf ( x, y ) wx wf ( x, y ) and this reflects the strength of the edge or edge response at any given wy pixel. The direction of the edge at the same pixels is the angle of that the hypotenuse makes with the axis. Therefore, the edge response or magnitude is approximately given by: E ( wf ( x, y ) 2 wf ( x, y ) 2 ) ( ) wx wy (2.8) where E is edge and the direction of the edge is approximately given as Ed § § wf ( x, y ) · · ¨ ¨ wy ¸ ¸ ¨ tan ¨ ¸¸ ¨ ¨ wf ( x, y ) ¸ ¸ ¨ ¨ wx ¸ ¸ ¹¹ © © 1 (2.9) The edge magnitude will be a real number, and is usually converted to an integer by rounding. Any pixel having a gradient that exceeds a specified threshold value is said to be an edge pixel, and others are not. Usually, people use the middle value in the range of grey level as threshold. 2.5 Laplacian Edge Detection It is a second order derivative operator D12 ( x) f ( x 'x) 2 f ( x) f ( x 'x) ( 'x ) 2 (2.10) D12 ( y ) f ( y 'y ) 2 f ( y ) f ( y 'y ) ( 'y ) 2 (2.11) It is represented as one dimension form >1 2 1@ for x direction and for the y ª1º direction «« 2 »» . ¬« 1 ¼» A Laplacian kernel is obtain using 3×3 window operator ª0 0 0 º ª0 1 0 º «1 2 1 » « 0 2 0 » « » « » «¬0 0 0 »¼ «¬ 0 1 0 »¼ ª0 1 0 º «1 4 1 » « » «¬0 1 0 »¼ Another form of the kernel that include the diagonal direction is ª 0 1 0 º ª 0 0 0 º ª1 0 0 º ª 0 0 1 º «0 2 0 » «1 2 1 » «0 2 0 » «0 2 0 » « » « » « » « » «¬0 1 0 »¼ «¬0 0 0 »¼ «¬0 0 1 »¼ «¬1 0 0 »¼ ª1 1 1º «1 8 1» « » «¬1 1 1»¼ 2.6 Sobel Edge Detection A Sobel filter consists of two convolution kernel for detecting the horizontal and vertical changes in an image. The kernels make up the gradient vector S ª Sx º « S » where ¬ y¼ S x the horizontal kernel is and S y is the vertical kernel. The horizontal kernel S x is used for smoothing along the direction of the x axis, while the vertical kernel S y applies to smoothing along the y axis (Salleh, 2008). Sobel use the central difference operator in Equation 2.6 and 2.7 and smoothing operator in order to obtain the two kernels. Sy ª1º «0» 1 2 1 @ « »> ¬« 1¼» ª1 2 1º « 0 0 0 » and S x « » «¬ 1 2 1»¼ ª1 º « 2 » 1 0 1 @ « »> «¬1 »¼ ª 1 0 1 º « 2 0 2 » « » «¬ 1 0 1 »¼ S y detect change in the vertical direction that means that it has response on horizontal edges. Meanwhile, S x detect change in the horizontal direction that means that it has response on vertical edges. Panetta and Wharton, (2008) used the Sobel multidirectional kernels which apply the same standard Sobel operator with diagonal orientation. They constructed the diagonal filter using 5 u 5 block instead of 3 u 3 . The diagonal kernels are as follows: ª0 0 1 «0 0 0 « « 1 0 0 « « 0 2 0 «¬ 0 0 1 2.7 0 0º 2 0»» 0 1» » 0 0» 0 0»¼ and ª 0 0 1 « « 0 2 0 « 1 0 0 « «0 0 0 «¬ 0 0 1 0 0º 0 0»» 0 1» » 2 0» 0 0»¼ Canny Edge Detector John Canny, proposed method in detecting the edges in 1986 in his paper “A Computational Approach to Edge Detection”. That paper describes a computational approach to the edge detection. The success of this method depends on the definition of a comprehensive set of goals for the computation of edge points. Higher concentration were given to Canny edge detection because it is likely to be better at detecting weak edges and less sensitive to noise (Y.Lin et all, 2002). Canny proposed an approach to edge detection that is optimal for step edges corrupted by white noise. The optimality of the detector is related to three criteria, detection, localization and one response. Canny’s derivation of a new edge detector is based on first smoothing the image with Gaussian, find the derivatives, non maximal suppression and the novel thresholding method that is hysteresis thresholding. Canny attempts to find the filter f that maximizes the product of SNR times localization subject to multiple constraints and while the result is too complex to be solved analytically, an efficient approximation turns out to the first derivative of a Gaussian function. The next step in Canny edge detector is a non-maxima suppression step, where pixels that are not local maxima are removed. As an extra step Canny suggest thresholding using hysteresis rather than simply selecting a threshold value to apply everywhere. Hysteresis thresholding uses a high threshold Th and a low threshold Tl . Any pixel in the image that has a value greater than Th is presumed to be an edge pixel, and is marked as such immediately. Then, any pixels that are connected to this edge pixel and that have a value greater than Tl are also selected as edge pixels and are marked too. The marking of neighbors can be done recursively or by performing multiple passes through the image. The optimality of the detector is related to three criteria below (Canny, 1986): 1) Good detection: The detection criterion expresses the fact that important edges should not be missed and that there should be no spurious responses. 2) Good Localization: The localization criterion says that the distance between the actual and located position of the edge should be minimal. 3) Only response to a single edge: The one response criterion minimizes multiples responses to a single edge. This is partly covered by the first criterion, since when there are two responses to a single edge, one of them should be considered as false. The third criterion solves the problem of an edge corrupted by noise and works against non-smooth edge operators. Assumed that the image consists of the edge and subject to white Gaussian noise. Let I ( x) G ( x) n( x) where I ( x) is image in one dimension G ( x) is edge and n( x) is the additive white Gaussian noise. The edge detector was assumed to be a convolution filter f which would smooth the noise and locate the edge. The problem is to identify the one filter that optimizes the three edge detection criteria. 2.7.1 Detection Criteria Let the impulse response of the filter f ( x) and denote the edge itself by G ( x) 0 . Then the response of the filter r ( x) is the and assume that the edge is centered at x sum of the response to the signal and the noise. Then we have r ( x) f ( x) I ( x) r ( x) HG H n f ( x ) G ( x ) n( x ) f ( x ) G ( x ) f ( x ) n( x ) (2.12) where H G is given by a convolution integral: W HG ³ G ( x) f ( x) dx (2.13) W assuming the filter has a finite impulse response bounded by [W , W ] . The root mean squared response to the noise n( x) only, will be 1 Hn ªW 2 º2 n0 « ³ f ( x) dx » ¬ W ¼ (2.14) where n02 is the mean squared noise amplitude per unit length. Then, the first criterion the output single to noise ratio, as the quotient of these two responses. W SNR ³ G ( x) f ( x) dx W W n0 ³ W 2 f ( x) dx (2.15) 2.7.2 Localization Criteria The second criterion is that the edge point is well localized, means the distance between the points marked by the detector and the centre of the true edge should be minimized. For the localization criteria, used the reciprocal of the mean squared distance of the marked edge from the centre of the true edge. Let H n ( x) be the response of the filter to noise only, and H G ( x) be its response to the edge, and suppose there is a local maximum in the total response at the point x x0 . Then we have, H n' ( x0 ) H G' ( x0 ) 0 (2.16) The taylor expansion of H G' ( x0 ) about the origin gives H G' ( x0 ) H G' (0) H G'' (0) x0 O ( x02 ) From our assumption that edge centre at x (2.17) 0 , therefore H G' (0) 0 . Our next assumption is the displacement x0 of the actual maximum is assumed to be small so we will ignore quadratic and higher terms. From Equation 2.16 and 2.17 we obtain H G'' (0) x0 | H n' ( x0 ) (2.18) Now, H n' ( x0 ) is the Gaussian random quantity whose variance is the mean squared value of H n' ( x0 ) , and is given by W ' n 2 2 0 E[ H ( x0 ) ] n ³ 2 f ' ( x) dx (2.19) W From Equation 2.18 we obtain E ª¬ H G'' (0) x0 º¼ 2 E ª¬ H n' ( x0 ) º¼ 2 W E ª¬ x02 º¼ H G'' (0) 2 ³ n02 f '2 ( x) dx W W W n02 E ª¬ x02 º¼ ³ f '2 ( x) dx n02 W f '2 ( x) dx G x02 W '' G H (0) 2 W n02 E[ x02 ] | ³ ³ ª º ' « ³ G ( x) f '( x) dx » ¬ W ¼ W 2 2 f ' ( x) dx W ª º ' ' « ³ G ( x) f ( x) dx » ¬ W ¼ W 2 G x02 (2.20) where G x02 is an approximation to the standard deviation of x0 . The localization is defined as the reciprocal of G x0 . W ³ G ( x) f ' Localization ' ( x) dx W W n0 ³ W '2 f ( x) (2.21) dx 2.7.3 Only Response to Single Edge This criterion implicitly defines in first criteria, but we make explicit to eliminate multiple response. We first note that the mean distance between adjacent maxima in the output is twice the distance between adjacent zero-crossing in the derivative of the operator output. Then Canny make use of a result from Rice, (1945) that the average distance between zero crossings of the response of a function g to Gaussian noise is xave § R(0) · S¨ ¸ © R ''(0) ¹ 1 2 (2.22) where R (W ) is the autocorrelation of g . In this case we are looking for the mean zerocrossing spacing for the function f ' . Now since, f R(0) ³ f g 2 ( x) dx and R ''(0) f ³ g '2 ( x) dx (2.23) f The mean distance between zero-crossing of f ' will be § f 2 · ¨ ³ f ' ( x) dx ¸ f ¸ xzc ( f ) S ¨ f ¨ ¸ 2 ¨ ³ f '' ( x) dx ¸ © f ¹ 1 2 (2.24) The distance between adjacent maxima in the noise response of f , denoted xmax will be twice xzc and let this distance to be some fraction k of the operator width xmax ( f ) 2 xzc ( f ) kW (2.25) The expected number of noise maxima in region 2W is N n where 2W xmax Nn 2 k (2.26) If a function f satisfies the multiple response constraint in equation 2.24 for a fixed k , then the function f w will also satisfies it, assuming W scales with w . The multiple response criterion is invariant with respect to spatial scaling of f . Canny use numerical optimization for finding the optimal detector for the function f which maximizes W ³ G ( x) f ( x) dx W W ³ G '( x) f '( x) W n0 dx W ³ W 2 f ( x) dx W n0 ³ f '2 ( x) dx W subject to the multiple response constraint. He suggests using penalty method to solve the above problem. Detail about the solution of this problem is out of the scope of this project and can be found in Canny, (1986). 2.7.4 Detector for Step Edge Now, we proceed to the problem for finding a detector for step edge. Here the input G ( x) is step edge. Specifically we set G ( x) Au1 ( x) where un ( x) is the n th derivative of a delta function and A is the amplitude of the step. That is, ­0, for x 0 ® ¯1, for x t 0 u 1 ( x ) And substituting for G ( x) in Equation 2.11 and 2.17 gives 0 A ³ f ( x) dx W SNR (2.27) W n0 ³ f 2 ( x) dx W A f '(0) Localization W n0 ³ '2 f ( x) (2.28) dx W From above, we see that these criteria improve directly with the ratio A term in signal to noise ratio of the image. 0 SNR A 6( f ) n0 where 6( f ) ³ f ( x) dx W W ³ W f 2 ( x) dx n0 the Localization A / ( f ') n0 / ( f ') where f '(0) W ³ f '2 ( x) dx W The optimal filter is implicitly defined by 0 6( f )/ ( f ') ³ f ( x) dx f '(0) W W ³ W 2 f ( x) dx W ³ (2.29) 2 f ' ( x) dx W The filter found by Canny in his paper can be approximated by the first derivative of a Gaussian G '( x) , where § x2 · G ( x) exp ¨ 2 ¸ © 2V ¹ (2.30) Suppose we want to detect edge in two dimension of a particular orientation, then we create a two dimensional mask for this orientation by convolving a linear edge detection function aligned normal to the edge direction with a projection function parallel to the edge direction. Suppose we wish to convolve the image with an operator Gn which is the first derivative of a two dimensional Gaussian G in some direction n : G § x2 y 2 · exp ¨ ¸ and 2V 2 ¹ © Gn wG wn n G (2.31) n should be oriented normal to the direction of an edge to be detected and although this direction is not known, we can estimate from n (G I ) (G I ) (2.32) where denote convolution. An edge point is defined to be a local maximum in the direction of n of the operator Gn applied to the image I . At local maximum, we have w Gn I wn 0 (2.33) and substituting for Gn and associating Gaussian convolution, we obtain w2 GI wn 2 0 (2.34) At such point, the edge strength will be the magnitude of Gn I (G I ) (2.35) We can first convolve with a symmetric Gaussian and then compute directional second derivatives to locate edges and use the magnitude to estimate the edge strength. In principle, it could be used to implement an edge detector in arbitrary number of dimensions, by first convolving the image with a symmetric n dimension Gaussian. The convolution with n dimension Gaussian is highly efficient because the Gaussian is separable into n one dimensional filters. 2.8 Histogram Equalization Histogram equalization is a technique in image enhancement. The aims to create an image with equally distributed brightness levels over the whole brightness scale as in Figure 2.3. Histogram equalization enhances contras for brightness value close to histogram maxima and decreases contrast near minima. (Sonka et all, 2008). Figure 2.3: Histogram Equalization The above figure shows the effect of the histogram equalization to the image histogram. The left graph is the grey level corresponds to the pixels and the left graph show that histogram equalization equally distributes the grey level to the all pixels in the image. Denote the input histogram by H ( p ) and recall that the input grey scale is [ p0 , pk ] . The aims is to find a monotonic pixels brightness transformation q T ( p ) such that the desired output histogram G (q ) is uniform over the whole output brightness scale [q0 , qk ] . The histogram can be treated as a discrete probability density function. The monotonic property of the transform T implies k k ¦ G (q ) ¦ H ( p ) i (2.36) i i 0 i 0 The sums in Equation 2.36 can be interpreted as discrete distribution functions. Assume that the image has N rows and columns, then the equalized histogram G (q ) corresponds to the uniform probability density function f whose function value is a constant N2 qk q0 f (2.37) The value from Equation 2.37 replaces the left side of Equation 2.36. The equalized histogram can be obtained precisely only for the idealized continuous probability density, in which case the Equation 2.36 becomes q 1 ds q q0 q0 k N2 ³ N 2 (q q0 ) qk q0 p ³ H (s) ds (2.38) p0 The desired pixel brightness transformation T can be derived as q T ( p) qk q0 N2 p ³ H (s) ds q0 (2.39) p0 The integral in Equation 2.39 is called the cumulative histogram, which is approximated by a sum in digital value, so the resulting histogram is not equally ideally. The discrete approximation of the continuous pixels brightness transformation from Equation 2.39 is q T ( p) qk q0 N2 p ¦ H (i) q 0 i p0 (2.36) 2.9 Conclusion Edge is the boundary between an object and the background, and indicates the boundary between overlapping objects. Ideal step is the most common type of edges where, the edge is simply a change in grey level occurring at one specific location. Laplacian and Sobel edge detection use derivative to locate the edges and are simple to understand compared to Canny edge detection. According to Canny, 1986, the optimality of the detector is related to three criteria, detection, localization and one response. The main step in Canny edge detection is smoothing with Gaussian, find the derivative, non maxima suppression and thresholding with hysteresis. The impulse response of the optimal edge operator was shown to approximate the first derivative of a Gaussian. Canny edge detector was proposed which used adaptive thresholding with hysteresis to eliminate streaking of edge contour. Histogram is a graph showing the number of pixels in an image at each different intensity value found in that image. Histogram equalization is the technique by which the dynamic range of the histogram of an image is increased. Histogram equalization assigns the intensity values of pixels in the input image such that the output image contains a uniform distribution of intensities. It improves contrast and the goal of histogram equalization is to obtain a uniform histogram. This technique can be used on a whole image or just on a part of an image. CHAPTER 3 MEDICAL IMAGING 3.1 Introduction Medical imaging refers to the techniques and processes used to create images of the human body for clinical purposes such as for medical procedures seeking to reveal, diagnose or examine disease or including the study of normal anatomy and physiology. There are many types of medical imaging now days such as computer tomography (CT) scan, magnetic resonance imaging (MRI), ultrasonography, nuclear imaging and so on. In this chapter, concentration was given to the x-ray tomography. To obtain the final images in x-ray tomography, image reconstruction method from projection are used. One of the available methods in image reconstruction in x-ray tomography is filtered back projections (FBP). Then, Radon transform is introduced as the initial step in filtered back projection. After the image was constructed using image reconstruction method in x-ray tomography, then we can perform any edge detection method. 3.2 Image Reconstruction from Projection Reconstruction is focus on tomographic medical imaging. The image reconstruction method can be classified into analytical methods and iterative (or algebraic) method (Kudo, 2006). Bear in mind that image reconstruction method here are the reconstruction from projection not the reconstruction in image analysis. The reconstruction from projection problem is simple in principle and can be explained qualitatively in a straightforward and intuitive manner. Let consider Figure 3.1 adapted from Gonzalez, 2008. Figure 3.1: Flat region, showing a simple object in x-ray tomography. Suppose the image in Figure 3.1 is a cross-section of a three dimension region of a human body, where the background represent uniform tissue while the round object is a tumor with higher absorption characteristics. Flat beams of x-ray were passed from left to right and the absorption profile show that the energy of the beam is absorbed more by the object than the background. Now, the only information available about the object is a one dimensional absorption signal. The reconstruction is begin by creating an image based on the one dimensional absorption signals. The approach is to project the one dimensional signal back across the direction from which the beam came as in Figure 3.2. The process of back projecting of one dimensional signal across two dimensional areas sometimes referred as smearing the projection back across the area. This means duplicating the same one dimensional signal across the image perpendicularly to the direction of the beam. For example, Figure 3.2 is created by duplicating the one dimensional signal in all columns of the reconstructed images or called back projection. Figure 3.2: Result of back projection of simple object. Now, suppose the source is rotated by 90q as in Figure 3.3 left. Repeating this procedure yields a back projection image in the vertical direction as Figure 3.3 right. Continue the reconstruction by adding this result to the previous back projection resulting in Figure 3.4. Now, we can tell that the object of interest is contained in the square shown, whose amplitude is twice the amplitude of the individual back projections. Figure 3.3: The beam and detector rotated about 90q and its back projection. Figure 3.4: The sum of two back projections. Figure 3.4 shows the intensity where the back projections intersect is twice the intensity of the individual back projections. From there we can see that the image is become more exact to the original shape. What happen if we increase the number of different projection? As the number of projection increases, the reconstructed image become clearer and well approximated to the shape of the original image as in Figure 3.5, besides getting the image is blurred by a “halo” effect. Figure 3.5: Reconstruction with 1,2,3,4 and 32 back projections. In Figure 3.5 we can see that the net effect after increases the number of back projections is the brighter region will dominate the result. The back projections with few or no intersection will fade into the background as the image is scaled for display. The “halo” effect is which we can seen appears as a “star” whose intensity is lower that the object but higher than the background. Back projection is one of the methods in image reconstruction from projection in xray tomography. Other method such as iterative method, direct algebraic algorithms, three dimensional algorithm and circular harmonic algorithms can be found detail in Natterer and Wubbeling, 2001. 3.3 X-ray or Computer Tomography The CT system was invented by Godfrey Newbold Hounsfield of EMI Central Research Laboratories in Hayes, England. Hounsfield conceived the idea in 1967 and it was publicly announced in 1972. The first commercial CT machine using x-rays was limited to making tomographic sections of the brain but acquired the image data in about 4 minutes and the computation time is about 7 minutes per picture. Mathematics underlying x-ray tomography was done in 1917 by John Radon. Literally, tomography means slice imaging. X-ray or Computer Tomography (CT) provides cross sectional images of body organs. The resulting image is clearer compared to the conventional x-ray. But, the radiation doses in CT are significant higher, and due to this reason CT is not used widely unless it is really needed (Suri et all, 2002). A typical CT slice is formed from 512x512 pixels and each pixel will corresponds to an element of the cut through the patient of about (0.5x0.5)mm². This dimension determines the limiting spatial resolution of the image. In CT, an X-ray beam, which is initially generated as a point source, is collimated to form a single slice of a flat fan shaped x-ray beam. The x-ray source is rotated around the patient and the two dimensional or three dimensional distribution of the x-ray radiation densities within the structure being image are recorded as projections from many angles of view. The information obtained from the rotation is used to reconstruct the cross sectional images of the body organs using mathematical reconstruction techniques (Suri et all, 2002). Figure 3.6: CT scanner configuration with a true “fan beam” of x-ray. Figure 3.6 shows a CT scanner configuration with a true “fan beam” of x-rays. The dotted lines indicate incremental rotation. Multiple detectors are aligned along the arc of circle whose centre is the x-ray tube focal spot. The x-ray beam is collimated into a fan beam. Both the x-ray and the detectors rotated about the patient in concentric circles whose centre approximately coincide with the centre of the patient. The conventional manner in which CT images are obtained is to keep the patient stationary during the scanning time required to generate one image. Scanning is then halted while the position of the patient is incremented in the direction perpendicular to the imaging plane using a motorized table. The next image is then obtained and the procedure is repeated for the number of increments required to cover a specified section in the body. 3.3.1 A Basic Model for Tomography In x-ray tomography, we reconstruct a three dimension object from its two dimension slices. Object of interest in x-ray imaging are described by a real value function on \3 , called the attenuation coefficient. The attenuation coefficient quantifies the tendency of an object to absorb x-rays of a given energy. This function varies from point to point within the object and is usually taken to vanish outside. The average of attenuation coefficient define a linear transformation called the Radon transform (Epstein, 2003). The attenuation coefficient is useful for medical imaging because different anatomical structure have different attenuation coefficient. For example normal and cancerous tissues have slightly different attenuation coefficient. Below is the attenuation coefficient in Hounsfield units adapted from Epstein, 2003. Attenuation coefficient Table 3.1: Material in Hounsfield units Water 0 Air -1000 Bone 1086 Blood 53 Fat -61 Brain white/gray -4 Breast tissue 9 Muscle 41 Soft tissue 51 Attenuation coefficients of human tissues for 100-keV x-rays. Model for the interaction of x-rays with matter is rests on the three basic assumption below, (Epstein, 2003): 1) No refraction or diffraction: X-ray beams travel along straight lines that are not bent by the object they pass through. 2) The x-rays used are monochoramatic: The waves making up the x-ray beam are all of the same frequency. 3) Beer’s law: Each material encountered has a characteristic linear attenuation coefficient P for x-rays of a given energy. According to Quinto, 2005, if the x-rays are not monochromatic, then lower energy x-rays get attenuated more than higher energy x-rays, so the average energy of x-ray increases as they go through the object. This is called beam hardening and can create reconstruction artifacts. The intensity, I of the x-ray beam satisfies Beer’s law in Equation 3.1. dI ds P ( x) I (3.1) where s is the arclenght along the straight line trajectory of the x-ray beam and P (x) is the attenuation coefficient at x . From the first assumption the direction of I at x gives the direction of the straight line along which the beam is traveling. Suppose that one of the x-ray beams is traveling along the line given parametrically by ^x0 sv : s \` . Here v is a unit vector in \3 . The function i ( s ) I (x0 sv) gives the intensity of the beam at points along this lines, and m( s ) P (x0 sv) gives the attenuation coefficient. Beer’s law states that di ds m( s )i ( s ) or Integrating this equation from s ª i (b) º log « » ¬ i(a) ¼ d (log i ) ds m( s ) a to s b gives b ³ m( s ) ds (3.2) a Between the points x 0 av and x 0 bv , the intensity of the beam is attenuated by ª b º exp « ³ m( s) ds » ¬ a ¼ Beer’s law describes how the material attenuates each of the one dimension beams and this process is isotropic because it does not depend on the direction of the line along which the x-ray is travel. In real measurement, the x-ray source is turned on for a known period of time. The total energy I i , incident on the object along a given line l is known. The total energy I 0 emerging from the object along l is then measured by an x-ray detector. Integrating Beer’s law we obtain log I0 Ii ³ P ds l (3.3) Here ds is the arclength parameter along the straight line path l . A perfect measurement of the ratio I0 Ii would therefore furnish the line integral of the attenuation coefficient along the line l . Indeed model for measurements made in x-ray tomography as precisely this line integral. 3.4 Radon Transform A straight line in Cartesian coordinates can be described either by its slope intercept form y mx c or as in Figure 3.7 by its normal representation x cos T y sin T U (3.4) y U T Figure 3.7: x Normal representation of a straight line. The Radon transform maps the spatial domain ( x, y ) to the domain ( U , T ) . Each point in the ( U , T ) space corresponds to a line in the spatial domain ( x, y ) . Note that ( U , T ) are not the polar coordinate of ( x, y ) . In x-ray tomography each ray beam can be represented using Equation 3. 4. Figure 3.8: Geometry of a parallel x-ray beam. The projection of parallel ray beam may be modeled by a set of such lines, as in Figure 3.8. f ( x, y ) is the object of interest, g ( U j , T k ) is the result on the detector or the projection signal and L( U j , T k ) is the parallel x-ray beam. An arbitrary point in the projection signal is given by the ray sum along the line x cos T k y sin T k U j . Then, the ray sum is a line integral given by (Gonzalez, 2008) g ( U j ,Tk ) f f ³³ f ( x, y ) G ( x cos T k y cos Tk U j ) dx dy (3.5) - f f where G is the impulse function. If we consider all value of U and T , the preceding equation generalizes to g ( U ,T ) f f ³³ - f f f ( x, y ) G ( x cos T y cos T U ) dx dy (3.6) Equation 3.6 gives the line integral of f ( x, y ) along an arbitrary line in the xy plane, which is the Radon transform. The radon transform ^ f ( x, y )` is the cornerstone of reconstruction from projections in x-ray tomography. In discrete case, Equation 3.6 becomes g ( U ,T ) M 1 N 1 ¦ ¦ f ( x, y)G ( x cosT y sin T U ) (3.7) x 0 y 0 where x, y, U , and T are discrete variables. If we fix T and allow U to vary, the above equation simply the sum of the pixels of f ( x, y ) along the line defined by the specified values of these two parameters. Changing the value of any parameter in the equation yield different projection as in previous section. The function g ( U , T ) is often refered to a sinogram because the Radon transform of an off-centered point source is a sinusoid as in Figure 3.9. Figure 3.9: Shepp-Logan phantom and its sinogram(Radon transform). Figure 3.9 is a density plot of a function that models the attenuation coefficient of a slice of the human head. The densities in this plot are scaled so that the highest attenuation (the skull) is white and the lowest (surrounding air) is black. A model of this sort is called a mathematical phantom and were introduced by Larry Shepp. The right figure depicts the line integral of this function. The purpose of image reconstruction is to obtain f ( x, y ) from the Radon transform. One of method is filtered back projection or FBP. The details of this method are described in Natterer, and Wubbeling 2001. Figure 3.10 shows the flow of the filtered back projection algorithm. Ramp filter g ( U ,T ) g '( U , T ) f ( x, y ) Back projection Figure 3.10: Flow of the filter back projection algorithm. In filtered back projection algorithm, a ramp filter is used to eliminate the “halo” effect in the back projection image. There are many choices of filter, but ramp filter has proved to be the optimal filter in FBP. Then, the process is continued to find the inverse of Radon transform in order to obtain the original image. 3.5 Conclusion The x-ray tomography used image reconstruction method to obtain the two dimensional image of three dimensional object. The aim of image reconstruction is to obtain the image of original object. Radon transform play important role in image reconstruction because the attenuation coefficient of object display in this form. An increase in the number of view or projection has the effect of increasing the level of clearance of final image. Filter back projection algorithms is one of the method in image reconstruction. In FBP the Radon transform is filtered using the ramp filter and the process of back projection produces original image of the object. Then, we can apply the edge detection to the image obtain from x-ray tomography. CHAPTER 4 MODEL AND SIMULATION 4.1 Introduction This chapter presents the model of edge detection and simulation work. The simulation uses C++ programming and images from x-ray tomography to be tested. We present the algorithms for Laplacian, Sobel and Canny edge detection. Prior to edge detection, histogram equalization is applied to produce a clearer image. Histogram equalization is a method in image enhancement to create an image with equally distributed brightness levels. We deploy the liver image as the test image using the Laplacian, Sobel and Canny edge detection for detecting the location of the tumor. Results for the tested imaged will presented in Chapter 5. 4.2 Algorithms for Laplacian and Sobel Edge Detection Laplacian Edge Detection 1) Using the Equation 2.10 and 2.11, we decide to use the kernel: ª1 1 1º «1 8 1» « » «¬1 1 1»¼ 2) Read the image I of size m u n . 3) Read the threshold value enter by the user. 4) Calculate the t value m t n ¦¦ I [i][ j ] i 0 j 0 threshold 2 5) For i 1 up to m 1 and j 1 up to n 1 L I [i 1][ j 1] I [i ][ j 1] I [i 1][ j 1] I [i 1][ j ] 8 f [i ][ j ] I [i 1][ j ] f [i 1][ j 1] I [i ][ j 1] I [i 1][ j 1] 6) Take the absolute value of L . 7) If L d t then L Figure 4.1 edge (white) else L nonedge (black). Algorithm for Laplacian edge detection. Sobel Edge Detection 1) Use the kernel Sy ª1 2 1º «0 0 0» « » «¬ 1 2 1»¼ Sx and ª1 0 1º « 2 0 2 » « » «¬1 0 1»¼ 2) Read the image I of size m u n . 3) Read the threshold value enter by the user. 4) Calculate the t value m t n ¦¦ I [i][ j ] i 0 j 0 threshold 2 5) For i 1 up to m 1 and j 1 up to n 1 Sx I [i 1][ j 1] f [i 1][ j 1] 2* I [i 1][ j ] 2* I [i 1][ j ] I [i 1][ j 1] I [i 1][ j 1] S y I [i 1][ j 1] 2* I [i ][ j 1] I [i 1][ j 1] I [i 1][ j 1] 2* I [i ][ j 1] I [i 1][ j 1] 6) Calculate S Sx S y 7) If S d t then S Figure 4.2: edge (white) else S nonedge (black). Algorithm for Sobel edge detection. Figure 4.1 and 4.2 are the algorithm for Laplacian and Sobel edge detection. We use these algorithms in developing the simulation in Section 4.5. For Sobel multidirectional, the algorithm is similar to the Sobel, but we need to add another two terms in calculating the value of S , where S S x S y S d 1 S d 2 . The kernels for S d 1 and S d 2 are in Section 2.6. 4.3 Algorithm for Canny Edge detection Algorithm for smoothing with Gaussian and differentiation adapted from Parker, 1997. 1) Read in the image to be process I . 2) Create 1D Gaussian mask G to convolve with I . The standard deviation V of this Gaussian is a parameter to the edge detector. 3) Create 1D mask for the first derivatives of the Gaussian in the x and y directions, call these Gx and Gy .The same value V is used as in step 2. 4) Convolve the image I with G along the rows to give the x component image I x , and down the columns to give the y component image I y . 5) Convolve I x with Gx to give I x ' and I y with Gy to give I y ' 6) The result up to here can be view with combined the x and y component. The magnitude of the result (Equation 2.8) is computed at each pixel ( x, y ) as: M ( x, y ) Figure 4.3: I ' x ( x , y ) 2 I ' y ( x, y ) 2 Algorithm for smoothing with Gaussian and differentiation. After smoothing with Gaussian, we need to find the derivative of the image using the central differentiation formula as in Equation 2.6 and 2.7. The magnitude of the gradient image is the edge strength given in Equation 2.35. The next step is to find the local maxima or the location of edges using the algorithm in Figure 4.4 below. Algorithm for non maximal suppression of directional edge data 1) Quantize edge directions eight ways according to the 8 connectivity using Equation 2.9. 2) For each pixel with non-zero edge magnitude, inspect the two adjacent pixels indicated by the direction of its edge. 3) If the edge magnitude of either of these two exceeds that of the pixel under inspection, mark it for deletion. 4) When all pixels have been inspected, rescan the image and erase to zero all edge data marked for deletion. Figure 4.4: Algorithm for non maximal suppression. After having information about location of non maxima pixels, then we perform hysterisis thresholding to determine whether the pixels are true edges or not. We need to read the value of low threshold and high threshold enter by the user, and the final values of thresholding are calculated using the number of pixels that are local maxima. Algorithm for thresholding with hysteresis 1) Define the low threshold tlow and the high threshold thigh 2) Scan all pixel and ark all edges with magnitude greater than thigh as correct 3) Scan all pixels with edge magnitude in the range [tlow , thigh ] 4) If such a pixel below tlow mark as no edges 5) If such a pixel above tlow and connected to the edges marked as correct Figure 4.5: 4.4 Algorithm for hysterisis thresholding Histogram Equalization Histogram equalization is a technique for contrast enhancement using grey scale transformation. The aim is to create an image with equally distributed brightness levels over the whole brightness scale (Sonka et all, 2008). Figure 4.6 is an algorithm to perform histogram equalization adapted from Sonka et all, 2008. Histogram Equalization 1) For N u M image of G gray-levels(often 256), create an array H of length G initialized with 0 values. 2) Form the image histogram: Scan every pixel and increment the relevant member of H if pixel p has intensity g p , perform H[g p ] H[g p ] 1 3) Form the cumulative image histogram H C : H C [0] 0 H C [ p] H C [ p 1] H [ p], p 1, 2,..., G 1 4) Set § G 1 · T [ p ] round ¨ H C [ p] ¸ © NM ¹ 5) Rescan the image and write an output image with grey level g q , setting gq Figure 4.6: 4.5 T[g p ] Algorithm for histogram equalization. Simulation The simulation was developed using Microsoft Visual C++ version 6.0 using Microsoft Foundation Classes. Images in the simulation must use .BMP format. On Windows, a bitmap is represented by 24 bits of data, representing a total of 16 777 126 colors. An image is form from collection of total number of pixels. Each pixel is represented by a string consisting of 24 bits of binary digits in the MFC function RGB(). Bitmaps can be drawn from Microsoft Paint program. A rectangular area of a memory dc where the bitmap is located has to be copied into CClientDC device context. This is done with the CDC::BitBlt() function. BitBlt means Bitwise Block Transfer. Then, if we want to set the new value of the pixel, the function is SetPixel(). Edge detection is performed through C++ programming using MFC. Figure 4.7 is the graphic layout of the simulation. The simulation has five buttons and four edit boxes. The buttons are control for performing Laplacian, Sobel, Sobel multidirectional and Canny edge detection. The button clear deletes the previous operation and updates the current window. The first edit box is for the user to enter any desire value of threshold for the Laplacian and Sobel method. If a higher value is enter then the resulting image becomes blurred and less edges are detected compared to a lower value. Some recommended values to be tested are 500, 1000 and 1500. The user can see the differences between these values. There are three edit boxes for Canny edge detector that are sigma, low threshold, and high threshold. Figure 4.8 is the result of the simulation after we perform the Sobel multidirectional edge detection with threshold at 500. Figure 4.7: Layout of the simulation. Figure 4.8: Simulation using Sobel multidirectional with threshold=500. 4.6 X-ray Tomography Images Final images in x-ray tomography after image reconstruction were .tiff files and were 512 pixels by 512 pixels. A sample data with cancerous tumor can be seen in Figure 4.9. The image is being viewed from a top-down perspective with the back located at the top of the image. Figure 4.9 is the image with liver tumor. The liver is located on the left hand side of the image. The tumor can be seen just left of the centre of the liver as a darkish blur. This image shows through its lack of detail that there is much room for improvement. The image was cropped before any processing was performed. The main reason for doing this was to decrease computational time, besides only the liver was of interest. The image was cropped at rows 30 and 360 and column 90 and 420. Figure 4.10 is the image after cropping and it can be seen how cropping the image resulted in placing the liver as the focus of the CT scans. Figure 4.9: Sample final image in x-ray tomography. Figure 4.10: Image after cropping. 4.7 Conclusion Here we presented the algorithms for Laplacian, Sobel and Canny edge detection. We also present algorithm of the histogram equalization, one method in image enhancement. Histogram equalization chosen because the effect on equalizing the grey level in the image makes the image clearer compared to the original one. An edge detection method can be implemented effectively using Microsoft Visual C++. The simulation was develop so that the user can adjust the parameter and chooses the desired method of edge detection without write the other code. Laplacian and Sobel only have one parameter meanwhile Canny edge detector has three parameters. . CHAPTER 5 RESULTS AND DISCUSSION 5.1 Introduction Chapter 5 discusses the results from simulations and some discussion. We test and compare the Sobel, Sobel multidirectional, Laplacian and Canny edge detection methods using an image on liver. Since Canny contain four main steps in the algorithm, we also present output for each step. After some comparison on the edge detection methods, we observe that Canny is the most appropriate method for medical imaging. Lastly, we presented the result of other test image. 5.2 Results of Edge Detection Methods The results of different edge detection methods are presented and we compare these methods using visual assessment. Panette and Wharton (2008) and Zhaou et all, 2005, also used visual assessment to demonstrate the performance of method of edge detection. The user can try to run the simulation using the various value of threshold for Sobel Multidirectional, Sobel and Laplacian method. 5.2.1 Laplacian Figure 5.1: Laplacian edge detection of the liver. Figure 5.1 shows the result of applying the Laplacian edge detection to the liver image with threshold value at 800. The Laplacian edge detection method has been easily applies here but the method fails to detect the location of the tumor. 5.2.2 Sobel Edge Detection Figure 5.2: Edge detection using Sobel of the liver. Figure 5.2 shows the result when the Sobel method is applied the vertical and horizontal kernels. We run the method using threshold value of 800. The method also fails to detect the location of the tumor but is successful in detecting several main edges in the image. Sobel result well than Laplacian edge detection. The Sobel edge detector provides good performance and is relatively insensitive to noise (Pitas, 1993), but in this case Sobel edge detector show the bad performance, maybe due to the source of image. Figure 5.3: The result using Sobel multidirectional edge detection method. The above figure shows the result using Sobel multidirectional method with a threshold value at 500. Sobel multidirectional performs better than Sobel because, it manage to detect more edges. We can see that Sobel multidirectional fail to detect the existence of tumor in the image but the most successful method up to here. Besides, more edges are detected compared to Sobel two directions in Figure 5.2. Therefore, Sobel multidirectional should be given more attention in the field of edge detection. 5.2.3 Canny Edge Detection Before perform Canny edge detection, we apply the histogram equalization technique to the image. The image that we have after running the histogram equalization on the liver tumor can be seen in Figure 5.4. It can be seen that the image detail is more apparent and the tumor and blood vessel can be seen much more easily than the original image. Figure 5.4: Image after histogram equalization. Figure 5.5: Gaussian smoothing of the liver. The image is smoothed by a two dimensional Gaussian function of width specified by a user parameter to eliminate noises. In this example we use V 2 . Once the user enters the value of sigma, a mask is computed and the Gaussian smoothing is performing using standard convolution methods. Two dimensional convolution with Gaussian takes time but in practice it is common to approximate this by two one dimensional Gaussian, one aligned with the x axis and the other with y -axis. In Figure 5.5 we can see that the edges tend to be smooth and less noisy. We also can see that the location of the tumor as a darkish area at the left side of the liver. The higher value of sigma, the lower is the Gaussian’s sensitivity to noise. Figure 5.6: Image differentiation of the liver. After smoothing the image, the next step is to find the edge strength by taking the gradient of the image. The magnitude or the edge strength is approximated as the square root of the sum of the squared derivatives values. We are using the mask > 1 0 1@ to perform the derivatives. The above figure is the result of smoothing with a Gaussian and differentiation in x and y direction. From Figure 5.6 the user can differentiate the edges and non-edges. The differentiation detects the edges in the smoothed image by putting the thick white line to those edges and the rest become dark. Figure 5.7: Image after locate the local maxima pixels. Non-maxima suppression is applied to the magnitude of the gradient image to detect the location of local maxima or the edges. First this process initialized to zero of the resulting image, then for every pixel (except pixels at the borders) it will check the magnitude of eight neighbor pixels according to the direction of the gradient. The idea is the magnitude of the edge pixels should be greater than the magnitude of the gradient of the pixels on each side of the edge. This process suppresses the non maxima pixel as the black pixel and the local maxima pixel as the white pixel. In the simulation the function non_max_supp performs this task. In above figure, we can see there are many pixels with white color that indicated the pixels as the potential edges. Figure 5.8: Image after thresholding using hysterisis. The thresholding process is applied to the non maxima suppression image. Here, the potential edges in Figure 5.7 is determine whether the pixels is edges or non edges The effect of using two value of thresholds prove to be more effective compared to a single value of threshold. The value of low threshold is 0.6 and the high threshold is 1.5. The recommends value of low threshold is between 0.20 and 0.60 and for the high threshold is between 0.60 and 1.0. Actually, the input values are not original values used in the thresholding with hysterisis. From the input, the new values of low and high threshold are computed according to the number of pixels that are local maxima. Canny gave a big contribution in the image processing with introducing the hysteresis thresholding. Figure 5.8 is the final image in Canny edge detection. We can see the there exist a tumor with specific location in the image. 5.3 Histogram Equalization Figures 5.8 and 5.9 are the resulting images with and without performing histogram equalization before applying the Canny edge detection. Result from the simulation show that Figure 5.8 is much clearer and smoother than Figure 5.9. Therefore, histogram equalization improves the Canny edge detection applied to the medical image. This suggests the histogram equalization should be applied before Canny edge detection on the medical images. Figure 5.9: Canny edge detection without performing histogram equalization. 5.5 Discussion Laplacian edge detector use the second order derivative in the kernel and the resulting figure shows that this method fail to detect the main edges in the CT scan image. Sobel operator is only applied in the horizontal and vertical orientations. However, it is possible to include the diagonal kernel. By including a diagonal kernel, edges which are commonly missed by the horizontal and vertical kernel are detected. However Sobel multidirectional still do not manage to detect the existence of tumor in the image. The same result obtains from Zhao et all 2005, that Sobel and Laplacian fail to filter the noise in specific image they tested. Histogram equalization is the process to uniformly distribute the grey level in the image. It has been shown give an effect when combined with Canny edge detector. We also try to compare histogram equalization with Laplacian and Sobel but the resulting images are not much improved with the original edge detection images. Canny edge detector has been shown to be successfully detecting the location of the tumor in the liver image, but there is no guarantee that this method wills also success to other images. It is because even the source is the same, but there are many other conditions to be considered. Therefore the radiologist should study edge detection deeply. Objective measures on how the edge detection performs can be done on the synthetic images using Pratt’s Figure of Merit (PFOM). Synthetic image is an image with the known number of edges. Since the CT images are not synthetic, we can only use visual assessment to compare the performance of edge detection method, as in Paneta and Wharton, 2008 and Zhao et all, 2005. Heath et all, 1996 introduces a method based on the experimental psychology and statistics, in which human rate the output of low level vision algorithms. According to Shirai, 1994, objective evaluation of vision algorithms is difficult without specifying the purpose of a total system which includes the algorithm. One possible way is to compare the performance of an algorithm with that of human vision. The suffer in understanding Canny edge detection is worth when it success to detect the tumor in the image. The result obtain agreed with Y. Lin et all, 2002, that the Canny edge detector better at detecting weak edges and less sensitive to noise. 5.6 Conclusion Sobel and Laplacian edge detector have one parameter to be adjusted by the user that is the threshold value. With a smaller value of threshold, less edge are detected. Canny edge detector has three parameters that are sigma, low threshold and high threreshold. The higher value of sigma will make the image more blurred and the user must choose the value of parameters carefully. Canny edge detector successfully detects the location of the tumor and main edges with the help of histogram equalization. But in real world problem, this method never promises to always be successful. Therefore, the radiologist should consider as many as the edge detection method. CHAPTER 6 CONCLUSION AND RECOMMENDATIONS 6.1 Introduction In this chapter, we discuss the conclusion and recommendations. We conclude what we have discussed in previous chapter. Then, some recommendations are provided for future works. These will benefit those who have interest in edge detection, image reconstruction and medical imaging. 6.2 Conclusion Edge is the boundary between two region with huge different in grey level or intensity. By performing edge detection, we can know information such as location, and size of object in the image. Edge detection can be perform using derivative and is carried out through a liner operation call convolution. The convolution kernel is up to the edge detection method chosen. Laplacian has one kernel meanwhile Sobel has two kernel that are vertical and horizontal kernel. In Sobel multidirectional there are four kernels to avoid missing edge in diagonal orientation. The advantage of Laplacian edge detection is it is easy to understand and to implement but this edge detector is responds doubly to some edges in the image. Therefore in practice, the Laplacian edge detection is applied to an image that has first been smoothed with something such as Gaussian smoothing filter in order to reduce its sensitivity to noise. Meanwhile Sobel edge detection is slightly more complex than Laplacian but it has been shown that Sobel performs better than Laplacian edge detection. Canny edge detection is more complex compared to Sobel and Laplacian edge detection, but this method is better at detecting weak edges and smoothing process reduces the effect of noise. Parameters in Canny are allowed to be adjusted so as to meet the requirements. Canny edge detector used smoothing with Gaussian, derivative, non maxima suppression and hysterisis thresholding. Experiment result show that Canny edge detector is feasible to medical images and deserves further research. It could be used to segment the tumor tissues in CT images. We also introduce to the histogram equalization method to make the Canny edge detector obtain better result. Medical images edge detection is an important work for object recognition of the human organs and it is an important pre-pocessing step in medical image segmentation and three dimension reconstruction (Zhao,et all, 2005). In CT scan, filtered back projection method was used in image reconstruction from projection method to obtain final images. We can obtain these images, because different object have difference attenuation coefficient. 6.3 Recommendation Today, Canny edge detection receives much attention from researcher. As a suggestion, studies on the best values for the parameters in Canny edge detection can be done. Besides, we also can combine Laplacian and Sobel edge detection with hysterisis thresholding in Canny edge detection. Interested reader can also extend this project with using the numerical analysis to compare the performance of edge detection methods. In Chapter 3, we only discuss image reconstruction from projection briefly and a little introduction about Radon transform. It is because of the time constraint. Actually, this topic can be extended to discuss or introduce new or existence image reconstruction method. This project applied the edge detection methods to the medical images. The interested reader can actually expanded this topic to other images such as images from satellite or aircraft in the defense field. REFERENCES Epstein, C.L. (2003). Introduction to the Mathematics of Medical Imaging. USA. Prentice Hall. Canny, J. (1986). A Computational Approach to Edge Detection. IEEE on Pattern Analysis and Machine Intelligence. Vol. 8 , Nov. 1986 Chung, S.H, Ho, S.B.(2008). Classification of Liver Diseases MRI Images Using First Order Statistics-Grid Computing Approach. IEEE. Gonzalez, R.C. Woods, R.E. (2008). Digital Image Processing. USA. Preason. Gudmundsson, M. Essam A.El-Kwae, M.R Kabuka. (1998). Edge Detection in Medical Images Using a Genetic Algorithm. IEEE Transaction on Medical Imaging, Vol.17, No.3 June 1998. Hajnal, J.V, Hill, D.L.G, Hawkes, D.J. (2001). Medical Image Registration. USA. CRC Press. Heath, M. Sarkar, S. Sanocki, T. Browyer. K. (1998). Comparison of Edge Detection. Computer Vision and Image Understanding. Vol.69. No.1 Jan 1998. Hiroyuki Kudo, (2006). Analtical Image Reconstruction Methods for Medical Tomography-Recent Advances and A new Uniqueness Result. Mathematical Aspects of Image Processing and Computer Vision, Joshi, M.A. (2001). Digital Image Processing An Algorithmic Approach. India. Prentice Hall. Lee, M.K Leung, S.W. Pun, T.L Cheung, H.L. Edge Detection by Genetic Algorithm. IEEE. Nattere, F. Wubbeling, F. (2001). Mathematical Methods in Image Reconstruction. USA. SIAM. Panetta K.A, Wharton E.J. “ Logarithmic Edge Detection with Application”. Journal of Computer, Vol. 3. No.9 Sept 2008. Parker, J.R. (1997). Algorithms for Image Processing and Computer Vision. USA. John Wiley & Sons. Pitas,I. (1993). Digital Image Processing Algorithms. UK.Prentice Hall. (1993). Quinto, E.T , “An Inroduction to X-ray tomographic and Radon Transforms”. Proceedings of Symposia in Applied Mathematics.American Methematical Society. 2005. Rice, S.O. (1945). “Mathematical Analysis of Random Noise”. Journal of Bell Syst Tech.vo1 24. Salleh, S. (2008). “ C++ Numerical and Visual Programming”. Universiti Teknologi Malaysia. Shirai, Y. (1994). Reply to Performance Characterization in computer Vision. CVGIP. Image Understanding . Sonka, M. Hlavac, V. Boyle, R. (2008). Image Processing, Analysis, and Machine Vision. USA. Thomsan. Suri, J.S, Setarehdan, S.K. Singh, S. (2002). Advanced Algorithmic Approaches to Medical Image Segmentation. UK. Spinger. Zhao Yu-qian,G.Wei-hua,C.Zhen-cheng,T.Jing-tian,L.Ling-yun. (2005). Medical Images Edge Detection Based on Mathematical Morphology. IEEE. Engineering in Medicine and Biology 27th Annual Conference Shanghai, China. 1-4 Sept. 2005. APPENDIX A: Coding of the simulation #include"simulation.h" BEGIN_MESSAGE_MAP(Csimulation,CFrameWnd) ON_WM_PAINT() ON_BN_CLICKED(IDC_MYBUTTON1, OnMyButton1) ON_BN_CLICKED(IDC_MYBUTTON2, OnMyButton2) ON_BN_CLICKED(IDC_MYBUTTON3, OnMyButton3) ON_BN_CLICKED(IDC_MYBUTTON4, OnMyButton4) ON_BN_CLICKED(IDC_MYBUTTON5, OnMyButton5) END_MESSAGE_MAP() Csimulation::Csimulation() { Home=CPoint(10,10); sImage=CSize(M,M); threshold=0; Create(NULL,"Medical Imaging Through Histogram Equalization and Canny Edge Detection",WS_OVERLAPPEDWINDOW,CRect(0,0,1050,655)); ibmp.LoadBitmap(IDB_BITMAP1); MyButton1.Create("Clear",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON ,CRect(700,550,775,600),this,IDC_MYBUTTON1); MyButton2.Create("Laplacian",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUT TON,CRect(500,550,575,600),this,IDC_MYBUTTON2); MyButton3.Create("Sobel",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTO N,CRect(400,550,475,600),this,IDC_MYBUTTON3); MyButton4.Create("Canny",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTO N,CRect(600,550,675,600),this,IDC_MYBUTTON4); MyButton5.Create("Sobel Multidirectional",WS_CHILD|WS_VISIBLE|BS_DEFPUSHBUTTON,CRect(225,550, 375,600),this,IDC_MYBUTTON5); ex.Create(WS_CHILD|WS_VISIBLE|WS_BORDER,CRect(950,520,1000,545), this,IDC_x); ey.Create(WS_CHILD|WS_VISIBLE|WS_BORDER,CRect(950,555,1000,580), this,IDC_y); ez.Create(WS_CHILD|WS_VISIBLE|WS_BORDER,CRect(950,590,1000,615),t his,IDC_z); ew.Create(WS_CHILD|WS_VISIBLE|WS_BORDER,CRect(30,550,80,575),this ,IDC_w); ew.SetFocus(); } void Csimulation::OnPaint() { CPaintDC dc (this); CDC memDC; memDC.CreateCompatibleDC(&dc); memDC.SelectObject(&ibmp); dc.BitBlt(Home.x,Home.y,433,433,&memDC,0,0,SRCCOPY); for (int rows=0;rows<M;rows++) for (int cols=0;cols<M;cols++) { origBuf[M*rows+cols]=dc.GetPixel(cols,rows); threshold += (double)origBuf[M*rows+cols]; } dc.TextOut(850,485,"Parameters for Canny"); dc.TextOut(850,520,"Sigma"); dc.TextOut (850,555,"Low Threshold"); dc.TextOut(850,590,"High Threshold"); dc.TextOut(10,485,"Parameter for Sobel and Laplacian"); dc.TextOut(10,525,"Threshold"); } void Csimulation::OnMyButton1() { CClientDC dc(this); CBrush bkBrush(RGB(255,255,255)); CRect rc; rc=CRect(450,0,950,450); dc.FillRect(&rc,&bkBrush); Invalidate(); } void Csimulation::OnMyButton2() { CClientDC dc(this); int L; CString str1; ew.GetWindowText(str1); ew.GetWindowText(str1); NN=atof(str1); threshold /= (double)(NN*NN); for (int rows=16-1;rows<M-1;rows++) for (int cols=16-1;cols<M-1;cols++) { L=origBuf[M*rows+cols-1]+origBuf[M*(rows-1)+cols]8*origBuf[M*rows+cols]+origBuf[M*(rows+1)+cols]+origBuf[M*rows+cols+1]+origB uf[M*(rows-1)+cols-1]+origBuf[M*(rows-1)+cols+1]+origBuf[M*(rows+1)+cols1]+origBuf[M*(rows+1)+cols+1]; L=abs(L); if (L<=(int)threshold) L=RGB(0,0,0); else L=RGB(255,255,255); dc.SetPixel(500+cols,2+rows,L); } } void Csimulation::OnMyButton3() { CClientDC dc(this); int Sx,Sy,S; CString str1; ew.GetWindowText(str1); ew.GetWindowText(str1); NN=atof(str1); threshold /= (double)(NN*NN); for (int rows=16-1;rows<M-1;rows++) for (int cols=16-1;cols<M-1;cols++) { Sy=origBuf[M*(rows-1)+cols-1]-origBuf[M*(rows+1)+cols1]+2*origBuf[M*(rows-1)+cols]-2*origBuf[M*(rows+1)+cols]+origBuf[M*(rows1)+cols+1]-origBuf[M*(rows+1)+cols+1]; Sx=origBuf[M*(rows-1)+cols-1]-origBuf[M*(rows1)+cols+1]+2*origBuf[M*rows+cols-1]2*origBuf[M*rows+cols+1]+origBuf[M*(rows+1)+cols-1]origBuf[M*(rows+1)+cols+1]; S=abs(Sx)+abs(Sy); if (S<=(int) threshold) S=RGB(0,0,0); else S=RGB(255,255,255); dc.SetPixel(500+cols,2+rows,S); } } void Csimulation::OnMyButton4() { CClientDC dc(this); int i,j; CString str2,str3,str4; ex.GetWindowText(str2); sigma1=atoi(str2); histogram(origBuf,M,M); Gaussian_smoothing(HE,sigma1,M,M); derrivative_x_y(smoothedim,M,M); non_max_supp(magnitude,delta_x,delta_y,M,M); ey.GetWindowText(str3); ez.GetWindowText(str4); tlow=atof(str3); thigh=atof(str4); hysteresis(magnitude,result,M,M,thigh,tlow,aftBuf); int color; for (int rows=15;rows<M-1;rows++) for (int cols=15;cols<M-1;cols++) { color=aftBuf[M*rows+cols]; dc.SetPixel(500+cols,10+rows,COLORREF RGB(color,color,color)); } } unsigned char Csimulation::histogram(unsigned char *origBuf, int rows,int cols) { int i,j; double sum,temp1,temp[256]; double H[256],HC[256]; int T[256]; for(i=0;i<256;i++) { H[i]=0; temp[i]=0; T[i]=0; HC[i]=0; } HC[0]=0; for (i=0;i<rows;i++) for (j=0;j<cols;j++) { H[origBuf[rows*i+j]]+=1.0; } for(i=0;i<256;i++) H[i]/=(rows*cols); sum=0.0; for(i=0;i<256;i++) { sum+=H[i]; HC[i]=sum; } for(i=0;i<256;i++) { temp1=(256*H[i]); temp1=temp1-1; if (temp1>0.0) T[i]=(int)temp1; else T[i]=0; } double gmin,gmax; gmin=gmax=0; for(i=0;i<256;i++) { if (H[i]!=0) { gmin=i; break; } } gmin=gmin/255; for(i=255;i>=0;i--) { if (H[i]!=0) { gmax=i; break; } } gmax=gmax/255; for(i=0;i<256;i++) { temp[i]=((gmax-gmin)*HC[i])+gmin; T[i]=(int)(temp[i]*255); } for (i=0;i<rows;i++) for (j=0;j<cols;j++) { HE[rows*i+j]=T[origBuf[rows*i+j]]; } return *HE; } int Csimulation::Gaussian_smoothing(unsigned char *origBuf,int sigma,int rows,int cols) { int r,c,rr,cc,windowsize,center; float dot,sum; int i; { float x,fx,sum=0.0; windowsize=1+2*ceil(2.5*sigma); center=windowsize/2; for(i=0;i<windowsize;i++) { x=(float)(i-center); fx=pow(2.71828,0.5*x*x/(sigma*sigma))/(sigma*sqrt(6.2831853)); kernel[i]=fx; sum+=fx; } for(i=0;i<windowsize;i++) kernel[i]/=sum; } center=windowsize/2; for(r=0;r<rows;r++) { for(c=0;c<cols;c++) { dot=0.0; sum=0.0; for(cc=(-center);cc<=center;cc++) { if(((c+cc)>=0&&((c+cc)<cols))) { dot+=(float)origBuf[r*cols+(c+cc)]*kernel[center+cc]; sum+=kernel[center+cc]; } } tempim[M*r+c]=dot/sum; } } for(c=0;c<cols;c++) { for(r=0;r<rows;r++) { dot=0.0; sum=0.0; for(rr=(-center);rr<=center;rr++) { if(((r+rr)>=0&&((r+rr)<rows))) { dot+=tempim[(r+rr)*cols+c]*kernel[center+rr]; sum+=kernel[center+rr]; } } smoothedim[r*M+c]=(int)(dot*BOOSTBLURFACTOR/sum+0.5); if(smoothedim[r*M+c]>M-1) smoothedim[r*M+c]=M-1; } } return *smoothedim; } int Csimulation::derrivative_x_y(int *smoothedim,int rows,int cols) { int r,c,pos,sq1,sq2; for(r=0;r<M;r++) { pos=r*M; delta_x[pos]=smoothedim[pos+1]-smoothedim[pos]; pos++; for(c=1;c<M-1;c++,pos++) { delta_x[pos]=smoothedim[pos+1]-smoothedim[pos-1]; } } for(c=0;c<M;c++) { pos=c; delta_y[pos]=smoothedim[pos+M]-smoothedim[pos]; pos+=M; for(r=1;r<M-1;r++,pos+=M) { delta_y[pos]=smoothedim[pos+M]-smoothedim[pos-M]; } } for(r=0,pos=0;r<rows;r++) { for(c=0;c<cols;c++,pos++) { sq1=(int)delta_x[pos]*(int)delta_x[pos]; sq2=(int)delta_y[pos]*(int)delta_y[pos]; magnitude[pos]=(short)(50.0+sqrt((float)sq1+(float)sq2)); } } return *magnitude; } unsigned char Csimulation::non_max_supp(short *magnitude,short*delta_x,short*delta_y,int rows,int cols) { short z1,z2; short m00,gx,gy; float mag1,mag2,xperp,yperp; int rowcount,colcount,count; short *magrowptr,*magptr; short *gxrowptr,*gxptr; short *gyrowptr,*gyptr; unsigned char *resultrowptr,*resultptr; for(count=0,resultrowptr=result,resultptr=result+M*M-1; count<M;resultptr++,resultrowptr++,count++) { *resultrowptr=*resultptr=(unsigned char)0; } for(count=0,resultptr=result,resultrowptr=result+M-1; count<M;count++,resultptr+=M,resultrowptr+=M) { *resultptr=*resultrowptr=(unsigned char) 0; } for(rowcount=1,magrowptr=magnitude+M+1,gxrowptr=delta_x+M+1, gyrowptr=delta_y+M+1,resultrowptr=result+M+1;rowcount<M2;rowcount++,magrowptr+=M,gyrowptr+=M,gxrowptr+=M,resultrowptr+=M) { for(colcount=1,magptr=magrowptr,gxptr=gxrowptr,gyptr=gyrowptr,resultptr=res ultrowptr;colcount<M-2;colcount++,magptr++,gxptr++,gyptr++,resultptr++) { m00=*magptr; if(m00==0) { *resultptr=(unsigned char)NOEDGE; } else { xperp=-(gx=*gxptr)/((float)m00); yperp=(gy=*gyptr)/((float)m00); } if(gx>=0) { if(gy>=0) { if(gx>=gy) { z1=*(magptr-1); z2=*(magptr-M-1); mag1=(m00-z1)*xperp+(z2-z1)*yperp; z1=*(magptr+1); z2=*(magptr+M+1); mag2=(m00-z1)*xperp+(z2-z1)*yperp; } else { z1=*(magptr-M); z2=*(magptr-M-1); mag1=(z1-z2)*xperp+(z1-m00)*yperp; z1=*(magptr+M); z2=*(magptr+M+1); mag2=(z1-z2)*xperp+(z1-m00)*yperp; } } else { if(gx>=-gy) { z1=*(magptr-1); z2=*(magptr+M-1); mag1=(m00-z1)*xperp+(z1-z2)*yperp; z1=*(magptr+1); z2=*(magptr-M+1); mag2=(m00-z1)*xperp+(z1-z2)*yperp; } else { z1=*(magptr+M); z2=*(magptr+M-1); mag1=(z1-z2)*xperp+(m00-z1)*yperp; z1=*(magptr-M); z2=*(magptr-M+1); mag2=(z1-z2)*xperp+(m00-z1)*yperp; } } } else { if((gy=*gyptr)>=0) { if(-gx>=gy) { z1=*(magptr+1); z2=*(magptr-M+1); mag1=(z1-m00)*xperp+(z2-z1)*yperp; z1=*(magptr-1); z2=*(magptr+M-1); mag2=(z1-m00)*xperp+(z2-z1)*yperp; } else { z1=*(magptr-M); z2=*(magptr-M+1); mag1=(z1-z2)*xperp+(z2-m00)*yperp; z1=*(magptr+M); z2=*(magptr+M-1); mag2=(z2-z1)*xperp+(z2-m00)*yperp; } } else { if(-gx>-gy) { z1=*(magptr+1); z2=*(magptr+M+1); mag1=(z1-m00)*xperp+(z1-z2)*yperp; z1=*(magptr-1); z2=*(magptr-M-1); mag2=(z1-m00)*xperp+(z1-z2)*yperp; } else { z1=*(magptr+M); z2=*(magptr+M+1); mag1=(z2-z1)*xperp+(m00-z1)*yperp; z1=*(magptr-M); z2=*(magptr-M-1); mag2=(z2-z1)*xperp+(m00-z1)*yperp; } } } if((mag1>0.0)||(mag2>0.0)) { *resultptr=(unsigned char)NOEDGE; } else { if (mag2==0.0) *resultptr=(unsigned char)NOEDGE; else *resultptr=(unsigned char)POSSIBLE_EDGE; } } } return *resultptr; } unsigned char Csimulation::hysteresis(short *magnitude,unsigned char*result,int rows,int cols,float tlow,float thigh,unsigned char*aftBuf) { int r,c,pos,numedges,highcount; int lowthreshold,highthreshold,hist[32768]; short int maximum_mag; for(r=0,pos=0;r<M;r++) { for(c=0;c<M;c++,pos++) { if(result[pos]==POSSIBLE_EDGE) aftBuf[pos]=POSSIBLE_EDGE; else aftBuf[pos]=NOEDGE; } } for(r=0,pos=0;r<M;r++,pos+=M) { aftBuf[pos]=NOEDGE; aftBuf[pos+M-1]=NOEDGE; } pos=(M-1)*M; for(c=0;c<cols;c++,pos++) { aftBuf[c]=NOEDGE; aftBuf[pos]=NOEDGE; } for(r=0;r<32768;r++) hist[r]=0; for(r=0,pos=0;r<M;r++) { for(c=0;c<M;c++,pos++) { if(aftBuf[pos]==POSSIBLE_EDGE) hist[magnitude[pos]]++; } } for(r=1,numedges=0;r<32768;r++) { if(hist[r]!=0) maximum_mag=r; numedges+=hist[r]; } highcount=(int)(numedges*thigh+0.5); r=1; numedges=hist[1]; while((r<(maximum_mag-1))&&(numedges<highcount)) { r++; numedges+=hist[r]; } highthreshold=r; lowthreshold=(int)(highthreshold*tlow+0.5); for(r=0,pos=0;r<rows;r++) { for(c=0;c<cols;c++,pos++) { if((aftBuf[pos]==POSSIBLE_EDGE)&&(magnitude[pos]>=highthreshold)) { aftBuf[pos]=EDGE; follow_edges((aftBuf+pos),(magnitude+pos),lowthreshold,M); } } } for(r=0,pos=0;r<rows;r++) { for(c=0;c<cols;c++,pos++) if (aftBuf[pos]!=EDGE) aftBuf[pos]=NOEDGE; } float percent; CString str; percent=float((r*100)/M); return *aftBuf; } void Csimulation::follow_edges(unsigned char *aftBuf,short*magnitude,short lowval,int cols) { short *tempmag; unsigned char *tempaft; int i; int x[8]={1,1,0,-1,-1,-1,0,1}, y[8]={0,1,1,1,0,-1,-1,-1}; for(i=0;i<8;i++) { tempaft=aftBuf-(y[i]*M)+x[i]; tempmag=magnitude-y[i]*cols+x[i]; if ((*tempaft==POSSIBLE_EDGE)&&(*tempmag>lowval)) { *tempaft=(unsigned char)EDGE; follow_edges(tempaft,tempmag,lowval,M); } } } void Csimulation::OnMyButton5() { CClientDC dc(this); int Sx,Sy,Sd1,Sd2,S; CString str1; ew.GetWindowText(str1); ew.GetWindowText(str1); NN=atof(str1); threshold /= (double)(NN*NN); for (int rows=17-1;rows<M-2;rows++) for (int cols=17-1;cols<M-2;cols++) { Sy=origBuf[M*(rows-1)+cols-1]-origBuf[M*(rows+1)+cols1]+2*origBuf[M*(rows-1)+cols]-2*origBuf[M*(rows+1)+cols]+origBuf[M*(rows1)+cols+1]-origBuf[M*(rows+1)+cols+1]; Sx=origBuf[M*(rows-1)+cols-1]-origBuf[M*(rows1)+cols+1]+2*origBuf[M*rows+cols-1]2*origBuf[M*rows+cols+1]+origBuf[M*(rows+1)+cols-1]origBuf[M*(rows+1)+cols+1]; Sd1=origBuf[M*rows+cols-2]+2*origBuf[M*(rows+2)+cols1]+origBuf[M*(rows+2)+cols]-origBuf[M*(rows-2)+cols]-2*origBuf[M*(rows1)+cols+1]-origBuf[M*rows+cols+2]; Sd2=-origBuf[M*rows+cols-2]-2*origBuf[M*(rows-1)+cols-1]origBuf[M*(rows2)+cols]+origBuf[M*(rows+2)+cols]+2*origBuf[M*(rows+1)+cols+1]+origBuf[M*row s+cols+2]; S=abs(Sx)+abs(Sy)+abs(Sd1)+abs(Sd2); if (S<=(int) threshold) S=RGB(0,0,0); else S=RGB(255,255,255); dc.SetPixel(500+cols,2+rows,S); } }