edge detection- morphological processing- halftonig

Page 1

EE569-Project-2 Morphological processing, Edge Detection & Digital Halftoning;

Neha Rathore 10/10/2008

ID -5994499980

NEHA RATHORE |ID- 5994499980

1


Problem 1- Edge Detection Basic Edge Detection algorithms and Advanced Edge Detection Algorithms Objective In this problem we have been given two grayscale images. One image has a low contrast and the other one is corrupted by noise. We need to detect the edges of these images and display resulting image with gray level’0’ as edges and ‘255” as background. Motivation Changes or discontinuities in an image amplitude attribute suck as luminance or tristimulus value are fundamentally important primitive characteristics of an image because they often provide an indication of the physical extent of objects within the luminance edges.1 There are primarily two methods to carry out edge detection. As we proceed we will study and analyze both the methods. As the first technique, I will carry out the first order edge detection. This is a non-parametric approach in which we calculate the gradient along the rows and columns and set a threshold to detect the edges. FIRST ORDER EDGE DETECTION: An edge in a continuous domain edge segment can be detected by forming the continuous one dimensional gradient along a line normal to the edge slope. If the gradient is sufficiently large (i.e., above some threshold value), an edge is deemed present.[1] In discrete domain, we can compute Row Gradient GR(j,k) and Column Gradient GC(j,k). Then the spatial gradient amplitude is given by

For implementing the gradient computation in the program for gray-level image, we use a 3x3 pixel edge gradient operators. For this particular problem I am using Prewitt operator, Sobel operator and Frei-Chen operator. Defining our 3x3 matrix, the figure below shows the number convention for our matrix:

Where, k=1 for prewitt operator; k=2 for Sobel and k=sqrt(2) for Frei-chen operator

1

DIP by William k.Pratt

EE 569- Fall’08 | ID- 5994499980

2


As I Implemented my program in c++, I used an array to store these 9 values of 3x3 mask and the image in 3-D array called Imagedata[256][256][0]. I then scanned through the entire image and calculated the gradient map for each 3x3 mask. An important step in this was to extend the size of the image from 256x256 to 258x258 in order to properly include the end pixels of each corner. Threshold selection2 Once you make the gradient map of the image, you need to fix a threshold value to compare to you pixel values. If the pixel is greater than or equal to the threshold we set the output pixel as “0” representing an edge and “255” otherwise. As in my case, I chose certain percentages of CDF and decided to include all the pixels within that percentage as edge pixels. For, example; if we decide to take 50% of CDF as edge, then we would take 55% of 65536 and check the value of grey level at this value in the actual histogram. In our Example, grey level 8 at 55% of cdf covered 36045 pixels. The following are the threshold values for sobel oprator:

percent of CDF percent of 65536 gray level

2

95%

85%

75%

65%

55%

45%

35%

25%

15%

5%

6225 9 65

5570 6 24

4915 2 15

4259 8 11

3604 5 8

2949 1 6

2293 8 5

1638 4 4

983 0 2

327 7 1

This method was discussed in the class by Prof.Kuo

EE 569- Fall’08 | ID- 5994499980

3


OUTPUTS: INPUT IMAGE (low Contrast)

FIG: 1.1

INPUT IMAGE (noisy)

FIG 1.2

Output with Prewitt t=25

Output with Prewitt T= 17

Output with Sobel T=8

Output with Sobel t=20

EE 569- Fall’08 | ID- 5994499980

4


OUTPUTS: Output with Frei-Chen t=8

FIG: 1.1

Output with Frei-chen t=17

FIG 1.2

Sobel with t=10

Sobel with t=25

Best Output

Best Output

EE 569- Fall’08 | ID- 5994499980

5


Procedure and algorithm:

EDGE DETECTION USING SECOND ORDER DERIVATIVE: SECOND ORDER EDGE DETECTION: This technique employs some form of spatial second-order differentiation to accentuate edges. An edge is marked if a significant spatial change occurs in the second derivative3. For this project, I am using Laplacian Generation Technique. In discrete domain, the simplest approximation to the continuous Laplacian is to compute the difference of slopes along each axis. We can use a 5-point Laplacian Operator (as shown below) to do the computation.

As in above case, I used an array to store these values. You take a 3x3 array of image and apply the above mask to it, thereby changing the center pixel. GRADIENT COMPUTATION: The gradient of the column by using the laplacian mask shown above. The laplacian mask is used in this case because it gives more weight to the center pixel. This allows the center pixel to spread more from its neighbors. Thus it acts as point spreading function which increases the possibility of actual number of edges being detected. So in order to calculate the second order derivative we follow the same procedure adopted in the first order method where the only difference is instead we use laplacian mask which calculates the gradient here. The next step is thresholding. THRESHOLDING: Having computed the gradient for all the pixels in the image , the next step is to set a threshold. If we take a look at the histogram of the gradient computed image we are vaguely able to see three different peaks and two valleys. Depending on the histogram we chose the threshold approximately and by various trial we choose our threshold to be T. Now check if the gradient of the image is lesser than the threshold,T1 if yes set the pixel in a different array to ‘-1’. If the gradient of the image s greater than threshold,T2 set the pixel in that array to ‘+1’. If the gradient of the image lies in between the threshold values T1 and T2 set the pixel in that array to ‘0’. The resulting array contains only values of ‘-1’, ‘+1’ and ‘0’. If the threshold is narrower,the dots on the edge detected image fail to clear distinguish the edges. Hence an optimum value of threshold for each image as per its histogram was chosen. ZERO CROSSING DETECTION:

3

Digital Image processing by -w.pratt

EE 569- Fall’08 | ID- 5994499980

6


The basic idea behind and for choosing zero crossing detection is that its one of the simplest if not most efficient algorithm to find the edges more clearly.The zero crossing detector looks for places in the Laplacian of an image where the value of the Laplacian passes through zero , that is the points where the Laplacian changes sign. Such points often occur at `edges' in images , that is points where the intensity of the image changes rapidly, but the disadvantage is that they also occur at places that are not as easy to associate with edges. In order to calculate the zero crossing in the image we first check the array where we have only -1’s +1’s and 0’s. The first condition is that the center pixel needs to be a ‘0’ or the product of the four diagonal elements must be -1. This implies that there is a zero crossing if the center pixel is zero or any of the corner elements in the diagonal is either a -1 or a +1 , that is their product must not be equal to +1 or 0. Now after checking for the condition if there is a zero crossing found set the output pixel to be a ‘0’, that is a object pixel. If we don’t find a zero crossing we set the output pixel to ‘255’ which is a background pixel. Thus finally we get the edge detected image with 0’s and 255’s.

EE 569- Fall’08 | ID- 5994499980

7


OUTPUTS:

2nd order t=20 for low contrast image

FIG: 1.1

2nd order noisy image t=4 for noisy image

FIG 1.2

T=15

Other outputs

Best Output

Best Output

EE 569- Fall’08 | ID- 5994499980

8


Preprocessing and post processing techniques The low contrast and noise of the above images hampers the proper edge detection of the image. To make our edge detection more efficient, we apply some pre- processing and post processing techniques to enhance the edge detections. As a pre- processing technique I applied the full scale linear equalization technique to my first image and mixed noise removal technique to my second image. As a result of which our low contrast image has became very good as compared to the input. however, the noisy image resulted in blurry image. We then apply our Edge detection techniques to these images. As a post-processing technique I applied a simple subtractive morphological processing technique to remove the isolated noisy dots. Any isolated dot appearing in the below pattern are removed by this technique. OOO OXO OOO Simply scanning the image for a match to above pattern gives us the desired result. Thse techniques can be efficiently applied to 1st order Edge detection. However, as the result of 2nd order edge detection gives us isolated dots, we cannot apply this technique to our result from second order. st

st

Another better way of applying morphological processing to our 1 order output is to apply the 1 order output image to the thinning process.

OUTPUTS: Output for low contrast with pre processing

Output of noisy image with pre processing

EE 569- Fall’08 | ID- 5994499980

9


FIG: 1.1

FIG 1.2

Edge detection t=65

Edge detection

Edge detection t=45

Best Output t=55

After post processing thinning

After post processing isolated pixel removal

EE 569- Fall’08 | ID- 5994499980

10


We can clearly see the difference between the first and second order edge detection techniques for images with contrast enhances and images with noise removal. Also, there is no difference in pre and post processing of images with mixed noise. As you see in the above Image, by applying the isolated pixel ask we were ble to remove some dots from the original picture( these removed dots are circled by gray). nd

We also observe that since 2 order method is based purely on zero crossing detection, we do not get an output result which is very pleasant to eyes. On the other hand, the result with first order techniques were much better. We see that the pre and post processing helps in case of noisy image but worsens the image in case of low contrast image.

. Problem 2 – Morphological Processing 4 Objective; We need to implement three morphological processing operations, namely, Shrinking, Thinning and Skeletonizing. We have to apply these operations to lena.raw and pattern.raw. Motivation Morphological image processing is a type of processing in which the spatial form or structure of objects within and image are modified. Dilation, erosion andskeletonization are three fundamental morphological 5 operations. These techniques are useful for extracting image components that are useful in representing and describing region shapes.

Implementation of morphological processing. As mentioned earlier, the boundary extension from 256x256 to 258x258 is to efficiently cover all the pixels of the original image. The first step,binary checking, converts the image from(0,255) to (0,1).

Bond= the bond of the center pixel with its neighboring pixels is calculates as per the following rule:

Bond=1*( 4 connected pixels) + 2*( 8 connected pixels.) 4 5

Class notes and Digital Image Processing by Pratt Digital Image processing by Pratt

EE 569- Fall’08 | ID- 5994499980

11


The pixels in following locations are consider 4-connected OIO I XI OIO The pixels in following locations are consider 8-connected 1O1 OXO 1O1 We them scan the image through a series of conditional mask patterns according to their bond. If a match to any for the mask is found we replace the center pixel by M. In my case, I have chosen a value of 128 for M. Next we compare this new image matrix with unconditional mask patterns. These mask check through the images and check if there is any catastrophic effect of erasure, in case of a hit. In the end, we check if the unconditional masking gives us 1 or 0 for the original 1 value. If there is a miss we do not erase and if there is a hit, we go on and erase that pixel. This process is repeated for Shrinking, thinning and skeletonizing. Except that in skeletonizing, we use bridging operation for one iteration. This is done to avoid any kind of discontinuity while performing skeletonizing.

Algorithm For the morphological processing, I hardcoded all the condition mask patterns. I then scanned the image using a 3x3 mask and scanned the image pixel wise. For each 3x3 mask I calculated the bond value if the center pixel was black. As per the bond values I applied the respective mask for shrinking ,thinning and skeletonizing.If there was a match with any of the given pattens, I save M=128 value in a new 256x256 array other wise if there is a miss I saved K=50 in another 2556x256 array. In a way I was saving the hit and miss patterns. All the k values tell us the pixel that have to be not deleted and the M matrix tells us possible erasures. I then applied the unconditional mask patterns to the M matrix and compared every 3x3 matrix for a hit or miss patterns. If there is a hit I stores a one in final S matrix and if there is a miss I saved a 0 in the S matrix at the location of center pixel. Later I added 1 to location in S with matched a value 50 in the K matrix.These were the pixels which didn’t need to be deleted. I then compared the S matrix with the original image and check if they were same. This was done to carry forward the iterations. If there was a match we stop any further shrinking, otherwise I copy S in the input array and run the whole process again. After around 90 iterations approximately, I found the proper output. The same process was repeated for thinning and skeletonizing. However, the conditional and unconditional masks for the both were different.

EE 569- Fall’08 | ID- 5994499980

12


SHRINKING : In this approach, we are not interested in the dimensions of the image that is for example we are not interested in the length, width or the radius of the object. Instead we would like to shrink the image to the extent that a solid object shrinks to a dot and an object with some white space in the middle shrinks to a contour. Shrinking will reduce objects in a binary image to a single point located at the geometric center of the object. This can be thought of as finding the center of mass of an object. For objects that do not have holes in them, a single point is generated. If there is a hole, the process will produce a ring of pixels that surrounds the hole and is equidistant from the nearest boundary.

Input Image

Best Output after 93 iterations

Input Image

Best Output after 93 iterations

As you see in the above images, solid objects are converted to a dot. There might be some error in this output. I tried to figure out the error in code or the masks I am applying. But couldn’t find out the correct reason for the expected error. The result in the case of PCB looks much better.

EE 569- Fall’08 | ID- 5994499980

13


THINNING : In thinning the aim is to capture the geometrical property of one important dimension (the more significant dimension). For example, a rectangle is supposed to be thinned to a single line along the longer side . The algorithm for thinning is the same as that in Shrinking except that the masks to be compared with are different and only those that correspond to Thinning are being compared. The thinning function is similar to shrinking, except that thinning generates a minimally connected line that is equidistant from the boundaries. Some of the structure of the object is maintained. Thinning also is useful when the binary sense of the image is reversed, creating black objects on a white background. If the thinning function is used on this revered image, the results, are minimally connected lines that form equidistant boundaries between the objects.

Input Image

Best Output after 93 iterations

Input Image

Best Output after 93 iterations

As opposed to the shrinking result, we were able to preserve the shape of the object here. However, in case of pcb the result looks more like skeletonizing, but we were able to create thinning results for given outputs.

SKELITONIZING:

EE 569- Fall’08 | ID- 5994499980

14


In this problem ,the objective is to capture the dominant dimension of the object as well as the sharp curves of the object. Thus a rectangle would be reduced to something as a line with two arrows pointing out on each end. The algorithm again is same as that in shrinking and thinning except that the masks to be compared with are different and only those correspond to skeletoninzing are being compared. Skeletonization also is similar to thinning, except that it maintains more information about the internal structure of objects. The classic way to think about skeletonization is to set fire (mentally, of course) to pixels around the outer edge of an object simultaneously. As the fire burns inward toward the center of the object, eventually it will meet burning pixels from the opposite direction. When two opposing fires meet they extinguish one another, leaving behind a single (or double) pixel boundary, or skeleton, of the object.

Input Image

Best Output after 93 iterations

Input Image

Best Output after 93 iterations

As you see we were successfully able to accomplish skeletonizing. The results looks very good and we were able to preserve the dimensional information of the image.

EE 569- Fall’08 | ID- 5994499980

15


Problem 3- DIGITAL HALFTONING MOTIVATION: Halftoning or analog halftoning is a process that simulates shades of gray by varying the size of tiny black dots arranged in a regular pattern. This technique is used in printers, as well as the publishing industry. If you inspect a photograph in a newspaper, you will notice that the picture is composed of black dots even though it appears to be composed of grays. This is possible because of the spatial integration performed by our eyes. Our eyes blend fine details and record the overall intensity. Digital halftoning is similar to halftoning in which an image is decomposed into a grid of halftone cells. Elements (or dots that halftoning uses in simulates shades of grays) of an image are simulated by filling the appropriate halftone cells. The 6 more number of black dots in a halftone cell,the darker the cell appears PROBLEM GOAL: Half toning is a method for creating the illusion of continuous tone output with a binary device. Digital half toning is the procedure used to convert a gray scale image into a binarized one that a printer can use to print. If we inspect a photograph in a newspaper, we will notice that the picture is composed of black dots even though it appears to be composed of gray scale images. This is possible because of the spatial integration performed by our eyes. Our eyes blend fine details and record the overall intensity. Digital half toning is similar to half toning in which an image is decomposed into a grid of halftone cells. In this problem, a binary image needs to be converted to a gray level image of the same spatial dimension. The main reason of keeping the output image size the same as that of the input image is to observe the 7 effectiveness of the algorithm applied in the conversion process.

Procedure and Results

FIXED THRESHOLD DITHERING :

In this approach ,the input image pixels are compared with the threshold value of 127.5.If the input image pixel is less than 127.5,the corresponding pixel in the output image is set to 0 ,else if the input image pixel is greater than or equal to 127.5,the corresponding pixel in the output image is set to 255. This seems to be a very basic approach which doesn’t care about the information in the image. This approach suffers from the disadvantage that the input image information is lost in such a discrete binarization or fixed threshold approach. The variation of the pixel intensity values is lost in this process. The below output may seem continuous and clean but the detail is totally lost. The image detail can have an error of up to 50%. This is not the kind of half toning we look for. We expect to see some detail in the picture. So, the algorithm is an inefficient one.

6 7

Wikipedia Wikipedia

EE 569- Fall’08 | ID- 5994499980

16


ORDERED DITHERING : The main disadvantage of fixed thresholding is that the variations in the input image aren’t propagated into the output image. One way to achieve this task is to add noise and use a fixed threshold to convert the gray level image into binary image. Another approach would be not to add noise and instead use an adaptive threshold and dithering is one such approach. Basically Dithering is a “Adaptive threshold Approach” where the threshold values are selected based on “Bayer’s Matrix” for a given dimension. In ordered dithering where a 2x2 index matrix is given a 4x4 or 8x8 index matrix can be generated by a recursive formula. If I (i,j) is the index matrix then the threshold matrix T (i,j) can be calculated using the formula,

T(i,j) = 255 * ( (I(i,j) + 0.5 ) / N^2) ) Where, N: number of pixels in the matrix used. If the input pixel intensity is less than the threshold returned (T),then the corresponding pixel in the output image is set to ‘0’ else if the input pixel intensity is greater than the threshold returned then the corresponding pixel in the output image is set to ’255’. Dithering matrix - Two matrixes are used in this algorithm to do dithering. First is an Index Matrix, which indicates how likely a dot within the matrix position will be turn on. The second one is a Threshold Matrix, which indicates the threshold values for each pixel at the corresponding matrix position. For this project, the given 2-by-2 Index Matrix is

4-by-4 and 8-by-8 Index Matrix can be determined iteratively by using following formula. For this project, we will use 4-by-4 Index Matrix as well.

Threshold Matrix can be calculated from the Index Matrix by using the following formula:

Where N2 denotes the number of pixels in the matrix. FLOYD STEINBERGS SERPENTINE SCANNING: This approach is based on the concept of adding noise to the input image and using a fixed threshold. However noise is not added explicitly to the input image .Instead the noise added to the input image is the difference of the input pixel and the quantized output pixel of the algorithm .This noise is diffused to the input pixels’ neighbors according to the matrix

EE 569- Fall’08 | ID- 5994499980

17


[000 007 3 5 1] * 1/16 In order to make sure that the noise doesn’t accumulate the image boundaries, the image is scanned along the opposite direction . That is for row 1 we scan the image from left to right and distribute the error with respect to the above matrix. In order to properly diffuse the error, the order of scanning has to be a serpentine scan. This means for all the even rows, we scan from left to right; for all the odd rows, we scan form right to left. When we scan through the second row we go from right to left where the following matrix is used to distribute the error. This results in continuous serpentine scanning.

[000 700 1 5 3] * 1/16 The basic idea behind the Floyd-Steinberg error diffusion is to spread the error to the surrounding neighbors. This could be illustrated by the following diagram.

Algorithms 1. I compared the image pixel wise for a value of 128. If the value was greater then 128 I gave the output image a value of 255 and 0 otherwise. My input image was stored in a 3-d array Imagedata[x][y][0]. 2. For the dithering procedure, I used the given I matrix and calculated the respective 2x2,4x4 and 8x8 matrix from the given formula for thresholding. I then applied these masks on every 3x3 selected matrix and compared for the values of threshold pixelwise. For this procedure we used a stamping technique rather than running over the image pixel wise. For this I had to set offsets in my for-loop for image scanning. 3. For Floyd stienberg, we had to apply the serpentine scanning algorithm. For this I check the value of x and scanned the image from left to right if the value was odd and right to left if the value was even. This was done by incrementing or decrementing the values of y. I then compared each pixel for a threshold value of 128 and gave a value of 0 if the pixel had value lesser then 128 and 255otherwise. I then calculated the error but subtracting this new value by original value. Then to

EE 569- Fall’08 | ID- 5994499980

18


disperse this error, I calculated the value of z=error*coefficients( given above) and in the 23,31,32,33 pixel of 3x3 matrix inputed the value k= original value+Z. This way I was able to diffuse the error in 4 neighboring pixel. I continued the same procedure for all the pixels.

Outputs Input Image

Tresholding at t=128

We see great loss of details here. Input Image

Output O.D

Since the image is small,that is, 128x128, ordered dithering produces better results Then 4x4 and 8x8. Input Image

Output 2x2

Output for bayer’s 2x2 dithering matrix is similar as ordered dithering for n=2. Input Image

Output 4x4

We see loss of details and the image might have lot of quantization errors. EE 569- Fall’08 | ID- 5994499980

19


Input Image

Output 8x8

Input Image

Floyd Stienberg Serpentine scanning

Input Image

Minimum squared error method

Input Image

Result for dithering for 512x512 image

EE 569- Fall’08 | ID- 5994499980

20


Input Image

Result for 4 level Intensity Dithering

We see that the serpentine scanning produces the best printer friendly result. However, The larger the image, the better out we get. --------------End of report------------------

EE 569- Fall’08 | ID- 5994499980

21


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.