Dither Removal
Total Page:16
File Type:pdf, Size:1020Kb
This article has not been updated for Mathematica 8. The Mathematica® Journal Dither Removal Bart M. ter Haar Romeny Mathematica is ideal for explaining the design of seemingly complex mathe- matical methods. In this article we explain the use of the 2D Fourier transform to remove unwanted dithering artifacts from images. All steps are visualized, so the reader can get a good idea of what the Fourier trans- form of an image looks like, the location of the origin, the artifacts and their extent, and how geometric reasoning works in the Fourier domain. The method leads to a marked clean up of images deteriorated by dither. ‡ Image Dithering Image dithering is often used for commercial print products like newspapers and magazines. Many printers (e.g., laser and inkjet printers) can only put a series of single black dots on paper. Intensity is then regulated by the local density of the dots. This process is called dithering. However, when a dithered image is scanned, the interaction between the discrete scanning process and dithering may seriously degrade the image. Dithering and scanning are often both peri- odic, leading to periodic interference artifacts. These can be removed by proper filtering in the spatial 2D Fourier domain. We first give an example of how dithering can be applied to an image. It is simplest to replace each pixel with a small array of random black dots, with the number of dots proportional to the intensity. Some general settings: In[1]:= SetOptionsListDensityPlot, Mesh False, ImageSize 200, AspectRatio Automatic, PlotRange All; LinearAlgebra‘MatrixManipulation‘; SetDirectoryDirectoryNameToFileName "FileName" . NotebookInformationEvaluationNotebook; The Mathematica Journal 10:2 © 2006 Wolfram Media, Inc. Dither Removal 433 In[4]:= url "http:www.bmi2.bmt.tue. nlimageanalysisPeopleBRomenyimages"; pixels Importurl "mr64.gif"1, 1; ListDensityPlotpixels 60 50 40 30 20 10 0 0 10 20 30 40 50 60 In[7]:= min, max Minpixels, Maxpixels Out[7]= 0, 214 The dithering function creates a small 16 µ 16 array, which enables us to make 256 different intensities. int In[8]:= ditherint_ : TableIfRandom ,1,0 , 16, 16 ; 214. We map this onto every pixel and get an image that is 16 times larger with dithered points. The function BlockMatrix (available in LinearAlgebra‘Ma trixManipulation‘) combines the different submatrices. The Mathematica Journal 10:2 © 2006 Wolfram Media, Inc. 434 Bart M. ter Haar Romeny In[9]:= dithered Mapdither, pixels, 2; ListDensityPlotBlockMatrixdithered 1000 800 600 400 200 0 0 200 400 600 800 1000 There are many sophisticated ways to dither an image. For a good overview see [1]. One of the best methods is the Floyd–Steinberg dithering method, based on error-diffusion [2]. The following graphic is an example. In[11]:= Show Import"http:www.webstyleguide.comgraphicsgraphics7.09.gif" ‡ Scanning a Dithered Image A problem occurs when the dithering mask is not made up of randomly placed black-and-white pixels but has some regularity. The dithered image then con- tains many highly regular spatial frequencies, which may give rise to interference patterns when the image is sampled with a regular scanner, which likely has a different sampling frequency. The result is a range of extra harmonics (i.e., frequencies that are the result of the interference). They come as the products of The Mathematica Journal 10:2 © 2006 Wolfram Media, Inc. Dither Removal 435 the frequencies involved. An example of the period of the frequencies that emerge follows. In[12]:= PlotSinx Sin1.1 x, x, 0, 100 Π 1 0.5 50 100 150 200 250 300 -0.5 -1 This occurs because the product signal is actually the addition of the sum and the difference of the base frequencies. In[13]:= TrigReduceSinx Sin1.1 x 1 Out[13]= Cos0.1 x Cos2.1 x 2 The lower frequencies are rarely a problem. The higher frequencies can be removed by proper low-pass filtering. ‡ Dither Removal by Filtering A “large” dithered and scanned input image follows (3045 µ 1017 pixels, 1.9MB). Note the periodic artifacts. In[14]:= im Importurl "AIA0019Etiger.gif"; Showim, ImageSize 300 Figure 1. Image of a tiger, produced by printing a stock-art photo with an 85 lines per inch dot screen printer and then scanning. The image is imported as a structure, in which the pixels and the header data are embedded. The pixels can be extracted as the 1,1 element. The Mathematica Journal 10:2 © 2006 Wolfram Media, Inc. 436 Bart M. ter Haar Romeny In[16]:= pixels im1, 1; Dimensionspixels Out[17]= 1017, 3045 To develop the method, we do not need the whole image. For speed and lower memory use, we take a subimage. We can see the periodic dithering more clearly by zooming in. In[18]:= ListDensityPlotsubim Takepixels, 500, 780, 380, 700 250 200 150 100 50 0 0 50 100 150 200 250 300 In[19]:= ydim, xdim Dimensionssubim; Because the dithering has a high spatial frequency, we suspect these frequencies to be higher than most of the other frequencies in the image. We compute the Fourier spectrum of the input image and plot its absolute value. Because the origin is really large, the image is automatically scaled with a large factor, which reduces the contrast. We zoom in on a limited range (0 – 100) of spectral values. In[20]:= ListDensityPlotAbsfft Fouriersubim, PlotRange 0, 100 250 200 150 100 50 0 0 50 100 150 200 250 300 The Mathematica Journal 10:2 © 2006 Wolfram Media, Inc. Dither Removal 437 The significant image frequencies are around the origin (i.e., in the corners of the periodic spectrum plot). The marked peaks at other locations are proof of the existence of strong periodic signals due to dithering. We will use a mask that allows only frequencies near the origin to be included: a low-pass filter for spatial frequencies. The mask is most easily created by an elliptic Gaussian weighting function in the center (different standard deviations in the x and y directions), which is then shifted over half the image size in each dimension to put the kernel at the origin of the Fast Fourier Transform (FFT) domain, which is in the lower left. Note that the Fourier domain is periodic in either dimension, so we actually see a single tile of a whole tiled 2D space in the x and y directions. The standard deviations have to be estimated from the 2D plot of the spectrum. For speed, we exploit the separability of the 2D Gaussian kernel. We first make two 1D arrays of Gaussian values and then multiply them into a matrix with Outer in a com- piled function. This is about 140 times faster than a 2D Table command. Σxs and Σys are the standard deviations of the Gaussian in the x and y directions of the subimage. In[21]:= Σxs 40.0; Σys 35.; mask Compile , Outer Times, 2 2 Rangeydim ydim Rangexdim xdim Exp 2 , Exp 2 ; 2 Σys2 2 Σxs2 maskr RotateLeftmask, Floorydim 2, xdim 2; In[24]:= ListDensityPlotmaskr 250 200 150 100 50 0 0 50 100 150 200 250 300 We also can inspect the amplitude spectrum and the low-pass Gaussian filter (both functions are scaled for plotting) along the interpolated diagonal positions of the rectangular image. In[25]:= interpolated ListInterpolationafft Absfft Fouriersubim; The Mathematica Journal 10:2 © 2006 Wolfram Media, Inc. 438 Bart M. ter Haar Romeny Here we view the mask as a height plot. In[26]:= ListPlot3Dmaskr, Mesh False, PlotRange All 1 0.75 0.5 0.25 200 0 100 100 200 300 We multiply the Fourier spectrum with the mask and study it visually. In[27]:= ListDensityPlotmaskr Absfft, PlotRange 0, 10 250 200 150 100 50 0 0 50 100 150 200 250 300 We mask out the unwanted frequencies by multiplying the spectrum with the mask, and we get the resulting image by the inverse Fourier transform. The Gaussian filtering is still letting some periodic frequencies through (we only like to keep the information near the corners), so we make it steeper by raising it to the third power. The Mathematica Journal 10:2 © 2006 Wolfram Media, Inc. Dither Removal 439 3 In[28]:= ListDensityPlotmaskr Absfft, PlotRange 0, 10 250 200 150 100 50 0 0 50 100 150 200 250 300 3 In[29]:= restored AbsInverseFourierfft maskr ; The result is a markedly improved image, without the disturbing dithering frequencies. In[30]:= ListDensityPlot subim, restored 250 200 150 100 50 0 0 50 100 150 200 250 300 The Mathematica Journal 10:2 © 2006 Wolfram Media, Inc. 440 Bart M. ter Haar Romeny 250 200 150 100 50 0 0 50 100 150 200 250 300 The dither is clearly removed, and details, such as the local hair bundles, can now clearly be discriminated. The following reads the whole image, processes the data in the Fourier domain (note that sx and sy are proportionally rescaled), and writes the restored image to disk, in this case as a GIF file. In[31]:= pixels im1, 1; ydim, xdim Dimensionspixels; fft Fourierpixels; Dimensionspixels Σy, Σx Σys, Σxs ; Dimensionssubim mask Compile , Outer Times, 2 2 Rangeydim ydim Rangexdim xdim Exp 2 , Exp 2 ; 2 Σy2 2 Σx2 maskr RotateLeftmask, Floorydim 2, xdim 2; restored AbsInverseFourierfft maskr3 ; In[38]:= Display"restored.gif", ListDensityPlot restored, ImageSize ReverseDimensionsrestored, DisplayFunction Identity, "GIF"; ‡ Conclusion This example shows the use of Fast Fourier Transform to remove periodic artifacts from an image. Despite the large size of the input image, the numerical performance of Mathematica is good. The Mathematica Journal 10:2 © 2006 Wolfram Media, Inc. Dither Removal 441 ‡ References [1] R. L. Adler, B. P. Kitchens, M. Martens, C. P. Tresser, and C.