Image Enhancement in Spatial Domain Dr. Sanjay Kumar Singh Associate Professor, Department of Communication Engineering, School of Electronics Engineering, Vellore Institute of Technology, Vellore ‐ 632014, Tamil Nadu [India] Fall 2022‐23 Image Enhancement • Image Enhancement refers to accentuation, or sharpening, of image features such as edges, boundaries, or contrast to make a graphic display more useful for analysis & display. • Image enhancement includes gray level and contrast manipulation, noise reduction, edge crispening and sharpening, filtering, interpolation and magnification, pseudocoloring, and so on. • Image enhancement remains a very important process because of its usefulness in virtually all image processing applications. Image Enhancement Point Operations Spatial Operations Transform Operations • Contrast stretching • Noise smoothing • Linear filtering • Noise clipping • Median filtering • Root filtering • window slicing • Unsharp masking • Homomorphic filtering • Histogram modeling • Low-pass, bandpass high-pass filtering • Zooming Point Operations • Zero memory operations u 0, L f () v 0, L Contrast Contrast Equation? Many different equations for contrast exist Examples: Michalson’s equation for contrast Basic Grey Level Transformations 3 most common gray level transformation: Linear Logarithmic Negative/Identity Log/Inverse log Power law nth power/nth root Logarithmic Transformations Maps narrow range of input levels => wider range of output values Inverse log transformation does opposite transformation The general form of the log transformation is New pixel value Old pixel value s = c * log(1 + r) Log transformation of Fourier transform shows more detail s = log(1 + r) Power Law Transformations Power law transformations have the form Power γ s=c*r Old pixel value New pixel value Constant Map narrow range of dark input values into wider range of output values or vice versa Varying γ gives a whole family of curves Power Law Example Original Magnetic Resonance (MR) image of fractured human spine s = r 0.6 s = r 0.4 Different power values highlight different details Contrast Stretching • Low contrast images occur often due to poor or nonuniform lighting conditions or due to nonlinearity or small dynamic range of the imaging sensor. • The gray scale intervals where pixels occur most frequently would be stretched most to improve the overall visibility of a scene. u, v u a va , u b v , b 0ua aub bu L v vb For dark region stretch a L 3 2 midregion stretch β >1, b 3 L bright region stretch γ 1 va 0 a b L u Clipping and Thresholding Clipping • A special case of contrast stretching where α γ 0 • Useful for noise reduction when the input signal is known to lie in the range a, b . v Image Histogram v u a b u v u Thresholding • Special case of clipping where a b t • Goal is to segment an image into object and background. • Eliminates unimportant shading variation. Hence, result is always a binary image. v v u u Digital Negative • A negative image can be obtained by reverse scaling of the gray levels according to the transformation, v L u • Producing negative prints of images. v L 0 L u Intensity Level Slicing • These transformations permit segmentation of certain gray level regions from the rest of the image. This technique is useful when different features of an image are contained in different gray levels. v v L a b Without background L, v 0, u aub otherwise 45 a b L With background L, v u, u aub otherwise Histogram Processing Modify an image so that its histogram has a desired shape. Histogram: Discrete function p(rk ) nk where, rk : k th gray level k : 0,1, 2,..., L -1 nk : No. of pixels in the image having gray level rk Histogram: Continuous function rk rk 1 0 Histogram pixels 130 36 36 22 0 Image 16x14 = 224 pixels 1 2 3 level p rk nk Example: rk p(0) 18 p(1) 6 p(2) 2 p(3) p (4) p (5) p(6) 0 p(7) 10 Normalized Histogram: Discrete Function nk ; p rk n k 0,1, 2,..., L 1 where, rk : k th gray level nk : No. of pixels with k thgray level in the image n : total No. of pixels in the image p rk : probability of occurrence of gray level rk Example: nk p rk n rk 18 1 p(0) 36 2 2 1 p(2) 36 18 10 5 p(7) 36 18 6 1 p(1) 36 6 p(3) p(4) p(5) p(6) 0 f f x x Very Different Images May Have Same Histogram! Histogram reflects the pixel intensity distribution, not the spatial distribution! Histogram Modeling • Histogram‐modeling techniques modify an image contrast and brightness so that its histogram has a desired shape and range. • Histogram modeling has been found to be a powerful technique for image enhancement. • Common Techniques: Histogram Stretching, Histogram Shrinking, Histogram Sliding, Histogram Equalization The histogram of an image represents the relative frequency of occurrence of the various gray levels in the image. Stretching S MAX S MIN I ' x, y I x, y I MIN S MIN I MAX I MIN I MAX : Largest gray level in the image I x, y I MIN : Smallest gray level in the image I x, y S MAX : Maximum possible gray level value (For an 8‐bit image this is 255) S MIN : Minimum possible gray level value (For an 8‐bit image this is 0) Original Image Image after contrast stretching Histogram of original Image Histogram after contrast stretching Example: Stretching S MAX S MIN I ' x, y I x, y I MIN S MIN I MAX I MIN 210 10 y x 50 10 100 50 Shrinking S MAX S MIN I ' x, y I x, y I MIN S MIN I MAX I MIN I MAX : Largest gray level in the image I x, y I MIN : Smallest gray level in the image I x, y S MAX : Maximum desired in the compressed histogram S MIN : Minimum desired in the compressed histogram Original Image Histogram after Shrinking to the range [50, 175] Histogram of original Image Histogram of shrinked Image Sliding S x, y I x, y Offset Offset: Amount to slide the histogram Original Image Histogram of original Image Positive value after histogram Sliding Histogram of the Image after Sliding Histogram Equalization Aim: To improve the contrast of an image To transform an image in such a way that the transformed image has a nearly uniform distribution of pixel values Equalized Histogram Original Histogram r 0 s T (r ) 1 s 0 1 In practice, the equalized histogram might not be completely flat. Transformation: Assume r has been normalized to the interval [0,1], with r = 0 representing black and r = 1 representing white s T (r ) ; 0 r 1 The transformation function satisfies the following conditions: (a). T r : (i). Monotonically increasing i.e. d T r 0 : Positive Slope dr (ii). Single Valued: if s T r then, r T 1 s (b). 0 T (r ) 1 for 0 r 1 s 1 T(r) sk=T(rk) 0 rk 1 r The requirement in (a) that T(r) be single valued is needed to guarantee that the inverse transformation will exist, and the monotonicity condition preserves the increasing order from black to white in the output image. Condition (b) guarantees that the output gray levels will be in the same range as the input levels. The inverse transformation from s back to r is denoted s T 1 (r ) where 0 r 1 Note: There are some cases that even if T(r) satisfies conditions (a) and (b), it is possible that the corresponding inverse T 1 ( s ) may fail to be single valued. PDF vs CDF PDF (probability density function) is the probability of each element CDF (cumulative distribution function) is summation of the probability of the element that value less than or equal this element p x P x CDF PDF 1/6 1 x 1/6 x PDF vs CDF f f a . Histogram b . Cumulative histogram x x Modified histogram Linear cumulative histogram by modification Relationship between PDF and CDF PDF can be found from the following equation d [ P( x)] p ( x) P( x) dx CDF can be found from the following equation P( x xn ) xn x x 0 p ( x)dx Idea of Histogram Equalization: The gray levels in an image may be viewed as random variables in the interval [0,1]. Let pr r denote the pdf of random variable r and ps s denote the pdf of random variable s; if pr r and T(r) are known and T 1 s satisfies condition (a), the formula should be dr p s ( s ) pr ( r ) ds r s T (r ) ( L 1) pr ( w)dw 0 By Leibniz’s rule that the derivative of a definite integral w.r.t. its upper limit is simply the integrand evaluated at that limit. ds dT (r ) dr dr r d ( L 1) pr ( w)dw ( L 1) pr (r ) dr 0 Substituting into the first equation dr ps ( s ) pr (r ) ds 1 1 , 0 s 1 pr (r ) ( L 1) pr (r ) L 1 The probability of occurrence of gray level rk in an image is approximated by nk pk (rk ) n k 0,1,2,..., L 1 k sk T (rk ) ( L 1) pr (rj ) k nj j 0 n ( L 1) j 0 ; k 0,1, 2,..., L 1 (a). (b). FIGURE (a). An arbitrary PDF (b). Result of applying all transformation to all intensity levels r. The resulting intensities s, have a uniform PDF, independent of the form of PDF of the r's. Example: p r r Solution: Steps for Histogram Equalization 1. Normalize the gray value to make the range between 0 and 1. 2. Find pdf values of individual gray value (normalized). 3. Find the CDF of normalized gray value. 4. Perform r to s mapping. Example: An 8‐level 64 64 image has a gray level distribution, nk shown in the table below, rk indicates a gray level in the image. (a). Draw the histogram of the original Image (b). Draw the histogram based on histogram equalization. Show all steps. Steps: Step 1: Calculate total no. of pixels n 64 64 4096 pixels Step 2: Find nk pr rk n k Step 3: Calculate sk T rk L 1 pr rj ; j 0 Step 4: Map gray level Step 5: Assign o/p level sk Step 6: Calculate ps sk Step 7: Plot graph k 0,1, 2,..., 7 Normalized Histogram k sk T rk L 1 pr rj ; k 0,1, 2,..., 7 j 0 0 s0 T r0 7 pr r0 7 0.19 1.33 1 j 0 1 s1 T r1 7 pr rj 7 pr r0 pr r0 7 0.19 0.25 3.08 3 j 0 2 s2 T r2 7 pr rj s1 7 pr r2 3.08 7 0.21 4.55 5 j 0 3 s3 T r3 7 pr rj s2 7 pr r3 4.55 7 0.16 5.67 6 j 0 4 s4 T r4 7 pr rj s3 7 pr r4 5.67 7 0.08 6.23 6 s5 T r5 s4 7 0.06 6.65 7 s6 T r6 s5 7 0.03 6.86 7 s7 T r7 s6 7 0.02 7.00 7 j 0 Transfer function: CDF Assigning o/p level sk Mapping gray level Equalized Histogram Histogram Matching (Specification) Objective: To convert the image so that it has a particular histogram that can be arbitrarily specified. Input Image Reference Image Resulted Image Input Image Histogram Equalization Cumulative Distribution Mapping Function /Look‐up Table Reference Image Histogram Equalization Cumulative Distribution Steps: 1. Equalize the histogram of the input image. 2. Equalize the specified histogram. 3. Relate the two equalized histograms. Modified Image r s T (r ) ( L 1) pr ( w)dw 0 z G ( z ) ( L 1) p z (t )dt s 0 z G 1 ( s ) G 1[T (r )] p z (z ) is the desired PDF k k nj j 0 j 0 n sk T (rk ) ( L 1) pr (rj ) ( L 1) , k 0,1, 2,..., L 1 k vk G ( z k ) ( L 1) p z ( zi ) sk , k 0,1,2,..., L 1 i 0 1 z k G [T (rk )], k 0,1,2,..., L 1 (b). (a). (a). Graphical interpretation of mapping from rk to sk via T(r) (b). Mapping of zq to its corres‐ ponding value vq via G(z). (c). (c). Inverse mapping from sk to its corresponding value of zk . Steps for histogram matching: – Obtain the histogram of the given image, T(r) – Precompute a mapped level s k for each level rk – Obtain the transformation function G from the given p z (z ) – Precompute z k for each value of s k – Map rk to its corresponding level s k ; then map level s k into the final level z k . (a). Histogram of the 3‐bit image (b). Specified histogram (c). Transformation function obtained from the specified histogram (d). Result of performing histogram specification, Compare (b) and (d). pr rk Input Image sk pz zq Reference Image vq i sk vq j Mapping/ Look‐up Table Histogram Statistics (Global) Mean (average intensity): L 1 1 m ri p ( ri ) MN i0 M 1 N 1 f ( x, y ) x0 y 0 Variance (2nd moment): 2 L 1 1 2 2 ( r ) ( ri m ) p ( ri ) MN i0 M 1 N 1 f ( x, y ) m x0 y 0 nth moment around mean: L 1 n (r ) (ri m) n p (ri ) i 0 2 Example σ is useful for estimating image contrast! 14.3 31.6 49.2 Local Enhancement Pixel-to-pixel translation Nonoverlapping region Histogram using a local neighborhood, for example 33 neighborhood Local Histogram Statistics – rs ,t is the gray level at coordinates (s, t) in the neighborhood – p(rs ,t ) is the neighborhood normalized histogram component – average local intensity (mean): mS xy r s ,t ( s ,t )S xy p(rs ,t ) where, sxy denotes a neighborhood – local variance 2 S xy [r ( s ,t )S xy s ,t mS xy ] p(rs ,t ) 2 Global vs Local Histogram Enhancement using Arithmetic/Logic Operations AND OR NOT Subtraction Addition Multiplication Division (a). Original Image (b). Gaussian Noise FIGURE: Image Addition (a) + (b) (a) 2 (a). Original Image FIGURES: Image Multiplication & Division (a) 2 Image subtraction and Change Detection The difference between two “similar” images is computed as g ( x, y ) f1 ( x, y ) f 2 ( x, y )