Filtering1

Spatial Domain Filtering

Tasks:

Basic:

  1. Blur the Image. (Mean filtering)
  2. Sharpen the Image. (Laplacian filtering)

Noise Reduction:(with introduction to image restoration)

Noise modles: Gussian, Salt and Pepper.

  1. Contraharmonic mean filtering
  2. Arthmetic mean filtering
  3. Geometric mean filtering
  4. Median filtering

Basic

For a selected filter and a selected pixel, we need to calculate the as we want the convolution result of the image. We defined a function to calculate the gray value for the exactly one pixel.

Convolution(x, y, filter) → new_gray_value

def convolution(arguments):
  for i in range(filter_size):
    for j in range(filter_size):
      if out_of_range:
        center_value += 0
      else:
        center_value += filter[i][j] * image_p
  center_value /= filter_size * filter_size

Then we can calculate the center_value for one pixel in the origin image. Then we can have a nested for loop to calculate the whole image.

def filter2d(arguments):
  for i in range(height):
    for j in range(width):
      new_image[i,j] = convolution(i,j, filter)

Notice that, when I am doing laplacian_filter, the result is very wired. As a result, we need to compelete scaling after operation. What’s more, I did a gama correction as wil to have a better view.

scaling method:

s = |S(x,y)| = |\frac{f(x,y)-min(f(x,y))}{max(f(x,y)) - min(f(x,y))}| \times 255

I use the same method in histogram equalization that to set up a table for gama function.

  for i in range(256):
    val = pow(float(i)/255.0 ,scale) * 255.0
    if val>255:
      val = 255
    elif val<0:
      val = 0
    table[i]= val

I meet a problem when using the laplacian method. The noise of the image is also enhanced. So I adjust the method with:

g = G(x,y) = k\times f(x) + L(x,y)

For high_boost_filtering:

First we should calculate the Fuzzy image:

for i in range(height):
  for j in range(width):
    sub_img[i ,j] = img[i, j][0] - fuzzy_img[i][j]

And we get the mask sub_img , and then we calculate the output image:

for i in range(height):
  for j in range(width):
    enhanced_img[i, j] = img[i, j][0] + k * sub_img[i][j]

And then we get the enhanced_img by doing the formula.

g(x,y) = f(x,y) + k\times g_m(x,y)

The function of image sharpening is to make the gray contrast enhanced, so that the fuzzy image becomes more clear. The essence of the image is that the image is subjected to an average operation or an integral operation, so it can be carried out on the inverse operation, such as differential operation can highlight the image details, so that the image becomes more clear.

\nabla f^2 = [f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x, y-1)
+f(x+1, y+1)+f(x+1,y-1)+ f(x-1,x+1)+f(x-1,y-1)] - 8f(x,y)

Result

original image:

3*3 Mean Filter:

\frac{1}{9} \left[ \begin{matrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{matrix} \right]

7*7 Mean Filter:

11*11 Mean Filter:

laplacian sharpen result:

high_boost_filtering result with k = 0.8:


Bar Picture Testing

Filter input image with 3 × 3 and 9 × 9 arithmetic mean filters respectively.

Origin Image:

3*3 arithmetic mean filter:

9*9 arithmetic mean filter:

3*3 contraharmonic mean filter:

9*9 contraharmonic mean filter:

3*3 harmonic mean filter:

9*9 harmonic mean filter:


Noise Reduction

Review that:

arithmetic:

f(x,y) = \frac{\sum_{i,j\in S(x,y)} f(i,j)}{m\times n}

geometric:

f(x,y) = \sqrt[nm]{\prod_{i,j \in S(x,y)}{f(i,j)}}

harmonic: Notice that we defined

\frac{1}{0} = max(\frac{1}{f(x,y)}) = 1
if img[img_x, img_y] == 0:
  centre_gray_value += 1
else:
  centre_gray_value += 1.0 / (img[img_x, img_y] * img_filter[i][j])

contraharmonic: Calculate two converlosion

  centre_gray_value1 = Convolution(img, img_filter, x, y, method,q+1)
  centre_gray_value2 = Convolution(img, img_filter, x, y, method, q)
  if centre_gray_value2 != 0:
    return int(float(centre_gray_value1) / centre_gray_value2)
  else:
    return 0

max, median and min: Write another function to process the points, store them to new array.

  if method == "median":
    array.sort()
  if filter_size ** 2 % 2 == 0:
    return array[filter_size*filter_size / 2]
  else:
    return int(float(array[filter_size*filter_size / 2] + array[filter_size filter_size / 2 -1])/2)
  elif method == "max":
    return np.amax(array)
  elif method == "min":
    return np.amin(array)

Reduction Result:

Gussian Noise Image:

arithmetic mean:

geometric mean:

Arthmetic Mean Filter can really reduce some noise because it’s a low pass filter. But it also blurred the image. It looks OK by using 5x5 filter. 9x9 filter is bad in practical. Geometric Mean Filter seems like that it can reduce more noise. It looks OK overall. But it also bolds the edge of the black elements in the image, because one zero will lead to result 0 when filtering. Median Filter can have the effect that Arthmetic Mean Filter has. It can denois the picture and also make the picture look better. In a word, I think that median filter is better when filtering Guassian Noise.


Salt Noise Image:

contraharmonic(negative)

contraharmonic(positive)

Setting a wrong Q value can lead to terrible results because the contraharmonic mean filter can not filter both pepper noise and salt noise. When Q is positive, the filter can reduce pepper noise. Because the most significant value is big value and the pepper value is small numbers, the black points can be removed by using this way. When Q is negative, the filter can reduce salt noise. Because the most significant value is small value(Reciprocal Sum). The Salt value is small numbers, the white points can be remove by using this way. However, if you use the wrong method, terrible things will happen like using Q=1.5 to reduce salt noise, because the most significant value is big value, the white noise will become louder in this way which will lead to terrible result.


Salt and Pepper Noise Image:

arithmetic mean:

geometric:

max:

min:

median:

Median Filter looks best, and then arithmetic mean. Other filter looks very ugly. Median Filter can do the best. In image processing, such as edge detection in the further processing of this before, usually need a certain degree of reduction Median filtering is a common step in image processing. It is especially useful for speckle noise and impulse noise. Save the edge of the feature so that it does not want to appear on the edge of the occasion is also very useful. arithmetic mean filtering looks ok here. It’s the same in Guasian Noise, Arthmetic Mean Filter can really reduce some noise because it’s a low pass filter. But it also blurred the image. It looks OK by using 5x5 filter. 9x9 filter is bad in practical. Geometric mean filtering is bad, because one zero will lead to result 0 when filtering. The points near one pepper noise will lead to a big area of black, it’s very terrible. max and min is bad, because they can only reduce one kind of noise not both pepper and salt. Max can reduce pepper noise very well and Min can reduce salt noise very well.