Programming Assignment 3

Slides:



Advertisements
Similar presentations
Programming Assignment 2 CS 302 Data Structures Dr. George Bebis.
Advertisements

CSE 390B: Graph Algorithms Based on CSE 373 slides by Jessica Miller, Ruth Anderson 1.
Stacks and Queues Sections 3.6 and 3.7. Stack ADT Collections:  Elements of some proper type T Operations:  void push(T t)  void pop()  T top() 
1 Graphs: Traversal Searching/Traversing a graph = visiting the vertices of a graph by following the edges in a systematic way Example: Given a highway.
GRAPHS AND GRAPH TRAVERSALS 9/26/2000 COMP 6/4030 ALGORITHMS.
1 Breadth First Search AB F I EH DC G FIFO Queue - front.
Programming Assignment 2 CS 308 Assignments 2&3: Build a Simple System to Recognize Coins labeled image original image quarters nickel pennies dime dollar.
Programming Assignment 2 CS308 Fall Goals Improve your skills with using templates. Learn how to compile your code when using templates. Learn more.
Programming Assignment 3 CS Data Structures Dr. George Bebis.
Breadth First Search (BFS) Part 2 COMP171. Graph / Slide 2 Shortest Path Recording * BFS we saw only tells us whether a path exists from source s, to.
Programming Assignment 4 CS 308. Recognize Coins (Regions) labeled image original imagethresholded image quarters nickel pennies dime dollar $1.67.
1 Chapter 18 Trees Objective To learn general trees and recursion binary trees and recursion tree traversal.
CS 6825: Binary Image Processing – binary blob metrics
1 Trees A tree is a data structure used to represent different kinds of data and help solve a number of algorithmic problems Game trees (i.e., chess ),
Trees – Part 2 CS 367 – Introduction to Data Structures.
Programming Assignment 4 CS 308 Fall Recognize Coins (Regions) labeled image original imagethresholded image quarters nickel pennies dime dollar.
COMP261 Lecture 6 Dijkstra’s Algorithm. Connectedness Is this graph connected or not? A Z FF C M N B Y BB S P DDGG AA R F G J L EE CC Q O V D T H W E.
Graphs – Part II CS 367 – Introduction to Data Structures.
10 Copyright © William C. Cheng Data Structures - CSCI 102 Graph Terminology A graph consists of a set of Vertices and a set of Edges C A B D a c b d e.
Applications of Stacks and Queues Stacks Linear list. One end is called top. Other end is called bottom. Additions to and removals from the top end only.
Chapter 16 – Data Structures and Recursion. Data Structures u Built-in –Array –struct u User developed –linked list –stack –queue –tree Lesson 16.1.
Where’s the title? You gotta search for it!. PotW Solution String s = new Scanner(System.in).next(); int[] prev = new int[s.length() * 2 + 2]; Arrays.fill(prev,
Breadth-first and depth-first traversal CS1114
CSE 3358 NOTE SET 8 Data Structures and Algorithms.
1 Trees General Trees  Nonrecursive definition: a tree consists of a set of nodes and a set of directed edges that connect pairs of nodes.
Course 3 Binary Image Binary Images have only two gray levels: “1” and “0”, i.e., black / white. —— save memory —— fast processing —— many features of.
Graphs - II CS 2110, Spring Where did David leave that book? 2.
Brute Force and Exhaustive Search Brute Force and Exhaustive Search Traveling Salesman Problem Knapsack Problem Assignment Problem Selection Sort and Bubble.
Review Graph Directed Graph Undirected Graph Sub-Graph Spanning Sub-Graph Degree of a Vertex Weighted Graph Elementary and Simple Path Link List Representation.
Data Structures & Algorithm Analysis lec(8):Graph T. Souad alonazi
Breadth-First Search (BFS)
Graphs A New Data Structure
Trees Saurav Karmakar
Graphs Representation, BFS, DFS
CSE373: Data Structures & Algorithms Lecture 13: Topological Sort / Graph Traversals Kevin Quinn Fall 2015.
Csc 2720 Instructor: Zhuojun Duan
Graph Searching.
Graph Search Lecture 17 CS 2110 Fall 2017.
Breadth-First Searches
CC 215 Data Structures Graph Searching
Depth-First Search.
Depth First Search—Backtracking
Chapter 5.
Data Structures and Algorithms for Information Processing
Graph Search Lecture 17 CS 2110 Spring 2018.
Graphs Representation, BFS, DFS
Data Structures – Stacks and Queus
Search Related Algorithms
Breadth-First Searches
Elementary Graph Algorithms
Depth-First Searches Introduction to AI.
Graph Traversals Depth-First Traversals. Algorithms. Example.
Tree Searching.
CSE 373 Data Structures Lecture 16
Subgraphs, Connected Components, Spanning Trees
Depth-First Search D B A C E Depth-First Search Depth-First Search
COMP171 Depth-First Search.
Breadth First Search - A B C D E F G H I front FIFO Queue.
Graph Algorithms "A charlatan makes obscure what is clear; a thinker makes clear what is obscure. " - Hugh Kingsmill CLRS, Sections 22.2 – 22.4.
Depth-First Search CSE 2011 Winter April 2019.
Algorithms: Design and Analysis
Graph Implementation.
Depth-First Search CSE 2011 Winter April 2019.
Chapter 14 Graphs © 2011 Pearson Addison-Wesley. All rights reserved.
Applications of BFS CSE 2011 Winter /17/2019 7:03 AM.
David Kauchak CS51A – Spring 2019
Analysis and design of algorithm
INTRODUCTION A graph G=(V,E) consists of a finite non empty set of vertices V , and a finite set of edges E which connect pairs of vertices .
Depth-First Searches.
Presentation transcript:

Programming Assignment 3 CS 308 Fall 2001

Label and Count Regions original image thresholded image labeled image 7 regions

Project Objectives Improve your skills with manipulating stacks and queues. Improve your understanding of recursion. Illustrate how to convert a recursive algorithm to an iterative one. Compare recursive with iterative solutions. Learn more about image processing. Learn to document and describe your programs

Flowchart for labeling and counting regions (1) add a new option to your menu called “Count/Label Regions” (2) the steps given in the diagram should be executed when the user selects this option.

How to choose a good threshold? corresponds to the bottom of the valley

Connected Components Algorithm Finds the connected components in an image and assigns a unique label to all the points in the same component.

Connected Components Algorithm (cont’d) 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 255 neighbors. 3. Stop if there are no more unlabeled 255 pixels. 4. Go to step 1 Print number of regions found.

8-neighbors of (i,j)

int computeComponents(inputImage, outputImage) (client function) int computeComponents(inputImage, outputImage) set outputImage --> 255 (white) // initialization 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; // new label findComponent( parameters ); // recursive function // non-recursive functions // findComponentDFS(inputImage, outputImage, i, j, label); // findComponentBFS(inputImage, outputImage, i, j, label); } return connComp;

findComponent(parameters) Implement this as a recursive function. Think what the parameter list should be ...

Breadth-First-Search (BFS) 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.

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; for each neighbor (ni,nj) of (pi,pj) // push neighbors if(inputImage[ni][nj] == inputImage[pi][pj] && outputImage[ni][nj] == 255) { outputImage[ni][nj] = -1; // mark this pixel Queue.Enqueue((ni,nj)); }

1 1 1 1 1 1 dequeue dequeue P2 p10 p3 p4 p10 p3 p4 dequeue dequeue 255 1 1 1 1 dequeue dequeue P2 p10 p3 p4 p10 p3 p4 P1 dequeue dequeue dequeue dequeue p3 p4 p4 p5 p5

Depth-First-Search (DFS) The main structure used used by DFS is the stack. 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.

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; for each neighbor (ni,nj) of (pi,pj) // push neighbors if(inputImage[ni][nj] == inputImage[pi][pj] && outputImage[ni][nj] == 255) { outputImage[ni][nj] = -1; // mark this pixel Stack.Push((ni,nj)); }

1 1 1 1 1 1 1 etc. pop pop pop pop pop pop P10 255 P4 P5 P6 P7 P3 P3

Timing a function #include <sys/time.h> void main() { struct timeval start, finish; float seconds; gettimeofday(&start, NULL); // function call here gettimeofday(&finish,NULL); seconds = (((finish.tv_sec * 1000000.0) + finish.tv_usec) - ((start.tv_sec * 1000000.0) + start.tv_sec)) / 1000000.0; }