Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Assignment 2 CS 302 Data Structures Dr. George Bebis.

Similar presentations


Presentation on theme: "Programming Assignment 2 CS 302 Data Structures Dr. George Bebis."— Presentation transcript:

1

2 Programming Assignment 2 CS 302 Data Structures Dr. George Bebis

3 Objective: Objective: Use Stacks and Queues to Count and Label Regions output image input image (1) labeled image (2) number of regions 2. Assign a distinct gray-level value to each region (i.e., for visualization purposes) 1. Extract regions and count them Output

4 Project Goals Improve your skills with manipulating stacks and queues (use array-based stacks/queues). Illustrate how to convert a recursive algorithm to an iterative one. Learn more about image processing.

5 Application: automatically count and label galaxies in images captured by NASA’s Hubble telescope http://hubble.nasa.gov/ NASA’s Hubble Telescope Webpage

6 Labeling and counting regions (1) Add a new option to the driver called “Count/Label Regions” (2) The steps shown in the diagram should be executed when the user selects this option. Thresholding Remove Small Holes & Smooth Boundaries Count Regions Input Image Display labeled image & report number of regions

7 Thresholding Separate most important galaxies from background. Precursor step to any high-level image analysis system.

8 threshold(T) – member function Each pixel in the input image is compared against a threshold. Values greater than the threshold are set to 255, while values less than the threshold are set to 0.

9 Results using different thresholds original threshold=128 threshold=10 threshold=230

10 Labeling and counting regions Thresholding Remove Small Holes & Smooth Boundaries Count Regions Input Image Display labeled image & report number of regions

11 Remove Small Holes and Smooth Boundaries Typically, further processing is required to improve the results of thresholding: –Some regions in the thresholded image might contain small holes –Boundaries might be too noisy We will use dilation/erosion to remove small holes.

12 dilate() – member function at least one neighbor is 255 otherwise

13 dilate() - example Dilation “expands” the size of a region –Adds a layer of boundary pixels originalthresholdeddilated

14 erode() – member function at least one neighbor is 0 otherwise

15 erode() - example Erosion “shrinks” the regions (i.e., removes a layer of boundary pixels) originalthresholdederoded

16 Removing holes and smoothing boundaries Threshold, then apply dilation to fill in the holes. Apply erosion on the dilated image to restore the size of the regions. original threshold dilate erode results of dilation followed by erosion

17 Labeling and counting regions: flowchart Thresholding Remove Small Holes & Smooth Boundaries Count Regions Input Image Display labeled image & report number of regions

18 Connected Components Algorithm Finds the connected components in an image Assigns a unique label to all the pixels in the same component.

19 Connected Components 1. Scan the thresholded image to find an unlabeled white (255) pixel and assign it a new label L. 2. Recursively assign the label L to all of its white neighbors. 3. Stop if there are no more unlabeled white pixels. 4. Go to step 1 thresholded image (i.e., after dilation/erosion) output (i.e., labeled) image 1 st 2 nd

20 8-neighbors

21 int computeComponents(inputImage, outputImage) initialize outputImage --> 255 (white) connComp=0; for (i=0; i<N; i++) for(j=0; j<M; j++) if(inputImage[i][j] == 255 && outputImage[i][j]==255) { ++connComp; label = connComp * 10; // label findComponent // recursively // or iteratively – user specified findComponentBFS(inputImage, outputImage, i, j, label); findComponentDFS(inputImage, outputImage, i, j, label); } return connComp; (client function) Extra credit if you implement the recursive version too.

22 Breadth-First-Search (BFS) - Queues The main structure used used by BFS is the queue. BFS uses a queue to “remember” the neighbors of pixel (i,j) that need to be labeled in future iterations. –The closest neighbors of (i,j) are labeled first. BFS will first label all pixels at distance 1 from (i,j), then at distance 2, 3, etc.

23 findComponentBFS(inputImage, outputImage, i, j, label) Queue.MakeEmpty(); Queue.Enqueue((i,j)); // initialize queue while(!Queue.IsEmpty()) { Queue.Dequeue((pi,pj)); outputImage[pi][pj] = label; // label this pixel for each neighbor (ni,nj) of (pi,pj) // push neighbors if(inputImage[ni][nj] == 255 && outputImage[ni][nj] == 255) { Queue.Enqueue((ni,nj)); outputImage[ni][nj] = -1; // mark this pixel to avoid duplicates } Extra credit if the queue is templated! (client function)

24 P1 p2 p10 p3 p4 p10 p3 p4 p3 p4 p4 p5 p5 dequeue P10 255 1 1 111 1 continue..

25 Depth-First-Search (DFS) - Stacks The main structure used used by DFS is the stack. “remember”DFS uses a stack to “remember” the neighbors of pixel (i,j) that need to be labeled in future iterations. –The most recently visited pixels are visited first (i.e., not the closest neighbors) DFS follows a path as deep as possible in the image. When a path ends, DFS backtracks to the most recently visited pixel.

26 findComponentDFS(inputImage, outputImage, i, j, label) Stack.MakeEmpty(); Stack.Push((i,j)); // initialize stack while(!Stack.IsEmpty()) { Stack.Pop((pi,pj)); outputImage[pi][pj] = label ; // label this pixel for each neighbor (ni,nj) of (pi,pj) // push neighbors if(inputImage[ni][nj] == 255 && outputImage[ni][nj] == 255) { Stack.Push((ni,nj)); outputImage[ni][nj] = -1; // mark this pixel to avoid duplicates } Extra credit if the stack is templated! (client function)

27 P1 P10 255 P2 P10 P3 P4 1 1 pop 1 P2 P10 P3 P5 P2 P10 P3 P6 P2 P10 P3 P7 P2 P10 P3 pop 1 1 1 1 etc.


Download ppt "Programming Assignment 2 CS 302 Data Structures Dr. George Bebis."

Similar presentations


Ads by Google