Top Banner
Lecture 5 Sept 10 Goals: •2-d arrays •Image representation •Image processing examples •Stack data structure
19

Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

Lecture 5 Sept 10

Goals:

•2-d arrays

•Image representation

•Image processing examples

•Stack data structure

Page 2: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

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 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

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 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

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 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

Another kind of filter – cosine filter

Helpful in removing periodic noise.

Page 6: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

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 7: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

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 8: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

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 9: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

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.

Better than selection sorting. Why?

Page 10: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

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 11: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

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. Additional 9 data movements are involved in copying the data into temp array.

Total number of operations is ~ 300 n2.

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

Typical case is likely to be much better. Why?

On a fast computer, this will only take a few seconds.

Page 12: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

EasyBMP package

RGBApixel A class that represents a single colored pixel. Each pixel is determined by four color components -- red, green, blue, and alpha. Each of these color components is an unsigned char variable (i.e. an integer between 0 and 255), and is accessed as follows: if v is a pointer to an RGBApixel variable, then the relevant color components can be accessed using v->Red, v->Green, v->Blue, and v->Alpha, respectively.

BMP A class that holds a bitmap image, i.e. a rectangular array of colored pixels (i.e. RGBApixel objects).

Page 13: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

EasyBMP package

• bool BMP::ReadFromFile(const char* FileName) Given the name of an image file, this function loads the image into the BMP class, so that your program can then access the image using member functions listed below. The function returns a bool value indicating whether the read was successful.

• bool BMP::WriteToFile(const char* FileName) If you want to save the image in your program to disk, this function allows you to do so; you specify the desired filename as the argument to the function. The function returns a bool value indicating whether the write was successful.

• int BMP::TellWidth() This member function takes no inputs, and returns the number of pixels in the horizontal direction.

Page 14: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

EasyBMP package

• int BMP::TellHeight() This member function takes no inputs, and returns the number of pixels in the vertical direction.

• RGBApixel* BMP::operator()(int i, int j) This is an example of an overloaded operator. Here the parentheses have been overloaded, so that given img is an object of type BMP, the expression img(i,j) returns a pointer to the color information stored at the pixel whose x-coordinate is i and whose y-coordinate is j.

The upper-left-hand pixel is given by i = 0, j = 0

and the positive x-axis is West to East while the positive y-axis is from North to South.

Page 15: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

A program with EasyBMP package

Problem: Convert a given color image into a black and white image.

As the above example shows, the algorithm is not very good. (It does better on gray scale images.)

Page 16: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

Algorithm:

• our algorithm is called thresholding.

• Recall that each color pixel is defined by three color components (R, G, B), each taking a value between 0 and 255. R = G = B = 255 is white, R = G = B = 0 is black.

• In thresholding, each color component of a pixel is multiplied by some weights c1, c2, c3 (s.t. c1 + c2 + c3 = 1)i.e., compute c1 R + c2 G + c3 B• if this weighted average exceeds 127, this pixel to mapped to white, otherwise it is mapped to black.

• In the code below, we take a weighted average by weighting the colors by 0.3, 0.6 and 0.1 (since green is the most sensitive and blue is the least sensitive.)

Page 17: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

Code implementing thresholding algorithm

#include "EasyBMP.h"using namespace std;

int main( int argc, char* argv[] ){ BMP Background; Background.ReadFromFile(argv[1]); BMP Output; int picWidth = Background.TellWidth(); int picHeight = Background.TellHeight(); Output.SetSize(picWidth, picHeight); Output.SetBitDepth(1);

Page 18: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

for (int i = 1; i < picWidth-1; ++i) for (int j = 1; j < picHeight-1; ++j) { Output(i,j)->Red = 0; Output(i,j)->Blue = 0; Output(i,j)->Green = 0;

}

for (int i = 1; i < picWidth-1; ++i) for (int j = 1; j < picHeight-1; ++j) { int col = 0.1* Background(i, j)->Blue + 0.6*Background(i,j)->Green +0.3* Background(i,j)->Red; if (col > 127) {

Output(i,j)->Red = 255; Output(i,j)->Blue = 255;

Output(i,j)->Green = 255; } }Output.WriteToFile(argv[2]);return 0;}

Page 19: Lecture 5 Sept 10 Goals: 2-d arrays Image representation Image processing examples Stack data structure.

Example 2: Write a program to shrink a 2m by 2n image to half its size. (i.e., reduce it to a m by n image.)

Algorithm: Take a group of 2 x 2 pixels and replace it by a single pixel by averaging the values.

for (int i = 0; i < width; ++i)for (int j = 0; j < height; ++j) { Output(i,j)->Blue = (img(2*i, 2*j)->Blue + img(2*i+1, 2*j)->Blue + img(2*i+1, 2*j+1)->Blue + img(2*i, 2*j+1)->Blue) / 4; Output(i,j)->Red = (img(2*i, 2*j)->Red + img(2*i+1, 2*j)->Red + img(2*i+1, 2*j+1)->Red + img(2*i, 2*j+1)->Red) / 4; Output(i,j)->Green = (img(2*i, 2*j)->Green + img(2*i+1, 2*j)->Green + img(2*i+1, 2*j+1)->Green + img(2*i, 2*j+1)->Green) / 4;};