12
Lecture 5 Feb 14, 2011 Goals: •Image representation using arrays •Image processing examples • image filtering • mean and median filters

Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

  • View
    218

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

Lecture 5 Feb 14, 2011

Goals:

•Image representation using arrays

•Image processing examples• image filtering

• mean and median filters

Page 2: Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

Array representation of images

Image: A pixel is a square region on a display device that can be illuminated with one of the color combinations.

A wide range of colors can be specified using 3 bytes – one for each color R, G and B.

• R = 255, G = 255, B = 255 represents White.

• R = 0, G = 0, B = 0 represents Black.

Bitmap format: uncompressed, each pixel information stored.

Header + each pixel description

Page 3: Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

Image processing problems

• image storage and access problems• format conversion• rotate, combine and other edit operations• compress, decompress• image enhancement problems

•Remove noise•Extract features•Identify objects in image•Medical analysis (e.g. tumor or not)

Page 4: Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

An image filtering problem

A small percentage (~ 5%) of pixels have become corrupted – randomly changed to arbitrary value. (salt and pepper noise). How should we remove this noise?

Original image image with noise after filtering

The image has become blurred, but this can be corrected.

Page 5: Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

Mean filtering

For each pixel, consider its eight neighbors.

Replace its color value by the average of the 9 color values, itself and the 8 neighbors.

Example: Suppose all the neighbors were blue pixels (0, 0, 150), but the center was red (noise), say (200, 0, 0). Then, the average value = (22, 0, 133).

This color is much closer to blue, so the noise has been removed.

Page 6: Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

Algorithm for mean filtering I = input image; O = output image; w = width of the image; h = height of the image; for j from 1 to w-2 do for k from 1 to h-2 do O(j,k)->Blue = (I(j,k)->Blue + I(j-1,k)->Blue + I(j+1,k)->Blue + I(j,k-1)->Blue + I(j-1,k-1)->Blue+ I(j+1,k-1)->Blue +I(j,k+1)->Blue + I(j-1,k+1)->Blue+ I(j+k+1)->Blue)/9; . . . . // similarly for other colors end do; end do;

On a 1024 x 1024 pixel image, how many operations does this perform? More generally, on an n x n image?

Answer: O(n2) which is linear since the size of the input is O(n2). More precisely, ~ 30 n2 operations.

Page 7: Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

Algorithm for mean filtering

I = input image; O = output image; w = width of the image; h = height of the image; for j from 0 to w-1 do O(0,j)->Blue = I(0,j)->Blue; // etc. for all colors end for; for k from 0 to h-1 do O(k,0)->Blue = I(k,0)->Blue; // etc. for all colors end for; for j from 1 to w-2 do for k from 1 to h-2 do O(j,k)->Blue = (I(j,k)->Blue + I(j-1,k)->Blue + I(j+1,k)->Blue + I(j,k-1)->Blue + I(j-1,k-1)->Blue+ I(j+1,k-1)->Blue +I(j,k+1)->Blue + I(j-1,k+1)->Blue+ I(j+k+1)->Blue)/9; . . . . // similarly for other colors end for; end for;

Page 8: Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

Median filter

A problem with mean filter is that the image loses its sharpness. Median filter does a better job.

Median filter examines the neighborhood pixel values and sort them, replace the current pixel value by the median value.

Example: 37 41 39

40 234 38

42 38 44

Sorted sequence: 37, 38, 38, 39, 40, 41, 42, 44, 234

A good choice to find the median is insertion sorting.

Which is better - Insertion sorting or selection sorting?

Page 9: Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

Median filter algorithm

I = input image with random noiseO = output image by median filtering I w = width of the image; h = height of the image; for j from 0 to w-1 do O(0,j)->Blue = I(0,j)->Blue; // etc. for all colors end for; for k from 0 to h-1 do O(k,0)->Blue = I(k,0)->Blue; // etc. for all colors end for;for j from 1 to w-2 do for k from 1 to h-2 do copy { I(j-1,k)->Blue, I(j+1,k)->Blue, I(j,k-1)->Blue, I(j-1,k-1)->Blue, I(j+1,k-1)->Blue, I(j,k+1)->Blue, I(j-1,k+1)->Blue, I(j+k+1)->Blue)} into a temp array of size 9; sort(temp) using insertion sorting; O(j,k)-> Blue = temp[4]; . . . . // similarly for other colors end for; end for;

Page 10: Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

Median filter output - example

Page 11: Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

Time complexity of median filtering

• Worst-case: for each color component, sorting an array of size 9 involves about 45 comparisons and about 45 data movements.

Total number of operations is ~ 270 n2.

For a 1024 x 1024 image, the total number of operations is ~ 270 million.

Page 12: Lecture 5 Feb 14, 2011 Goals: Image representation using arrays Image processing examples image filtering mean and median filters

Summary

• algorithm analysis involves measuring the number of operations performed by an algorithm before implementing it. This will provide some guideline on which one to implement.

• many ways to measure efficiency of algorithms:• average vs. best case vs. worst-case• time, storage, bandwidth etc.• which operations? +, == , assignment, or all of them?

• O notation is used to approximate the time complexity. (gives just the order of magnitude).