Programming Assignment #1

Anonymous
timer Asked: Feb 23rd, 2019
account_balance_wallet $120

Question Description

Important Design Requirement:

Your design must be based on Modularity and "Separation of Concerns".

The Stack and Queue Data Structure implementations must be based on "Information Hiding" and "Encapsulation".

The Application Code (e.g. Rat In Maze, Wire Router, Image Component Labeling, ...) know about the Data Structures only through their Interfaces (APIs).

Remember that interfaces represent behavior, while classes represent implementation.

Related Resources:

You do not have to use the following C++ code. In fact, it is probably better to develop your own code by adapting the given code, rather than using it "as is", because this code has a long chain of dependencies (#include files). So, please use at your own discretion, the given code is only intended to help you get started with ideas, not with code.

starting code is here:

/*

* Image Component Labeling

* Project 1

* < Your Name >

* < Date >

*

* Purpose and usage of this application

* . . .

* . . .

*

*/

// . . .

// . . .

// . . .

// global variables

/*

* pixel should be redesigned as an object, instead of int, with two

fields,

* as described in the Assignment Specification document

*/

int **pixel;

int size; // number of rows and columns in the image

// functions

void welcome()

{

// Optional code goes here

}

void inputImage()

{// Input the image.

cout << "Enter image size" << endl;

cin >> size;

// create and input the pixel array

make2dArray(pixel, size + 2, size + 2);

cout << "Enter the pixel array in row-major order" << endl;

/*

* Either ask user for input grid or generate random numbers (zeros

and ones)

*/

for (int i = 1; i <= size; i++)

for (int j = 1; j <= size; j++)

cin >> pixel[i][j];

}

void labelComponents()

{// Label the components.

// initialize offsets

position offset[4];

offset[0].row = 0; offset[0].col = 1; // right

offset[1].row = 1; offset[1].col = 0; // down

offset[2].row = 0; offset[2].col = -1; // left

offset[3].row = -1; offset[3].col = 0; // up

// initialize wall of 0 pixels

for (int i = 0; i <= size + 1; i++)

{

// Your code goes here

}

int numOfNbrs = 4; // neighbors of a pixel position

// scan all pixels labeling components

arrayQueue<position> q;

position here, nbr;

int id = 1; // component id

for (int r = 1; r <= size; r++) // row r of image

for (int c = 1; c <= size; c++) // column c of image

if (pixel[r][c] == 1)

{

/*

* New image Component

*

* Your code goes here

*/

} // end of if, for c, and for r

}

void outputImage()

{// Output labeled image.

cout << "The labeled image is" << endl;

for (int i = 1; i <= size; i++)

{

for (int j = 1; j <= size; j++)

cout << pixel[i][j] << " ";

cout << endl;

}

}

void main()

{

welcome();

inputImage();

labelComponents();

outputImage();

}

Data Structures and Algorithms Programming Assignment #1 – Image Component Labeling An application of DFS and BFS with Stacks and Queues Due Date: Sunday, February 24, 2019 Image Component Labeling ❏ A digitized image is an m x m matrix of pixels. ➺ ❏ ❏ ❏ “pixel” is a word invented from "picture element” In a binary image, each pixel is either 0 or 1. ➺ A 0 pixel represents image background, while a 1 represents a pixel on an image component. ➺ We will refer to pixels whose value is 1 as component pixels. Two pixels are adjacent if one is to the left, above, right, or below the other. Two component pixels that are adjacent are pixels of the same image component. The objective of component labeling is to label the component pixels so that two pixels get the same label if and only if they are pixels of the same image component. DS&A Image Component Labeling Slide # 2 Solution Strategy ❏ ❏ ❏ The components are determined by scanning the pixels by rows, from top to bottom, and within each row by columns, from left to right. Accordingly, the scanning order is similar to reading a page of English text. When an unlabeled component pixel is encountered, it is given a component identifier/label (new color). Labels are assigned starting with 2, because 1 denotes foreground and 0 denotes background. This pixel forms the seed of a new component. DS&A ➺ We determine the remaining pixels in the component by identifying and labeling all component pixels that are adjacent to the seed. ➺ Labeling adjacent component pixels allows to discover more adjacent component pixels (neighbor of neighbor is a neighbor). ➺ This exploration can be directed in “Depth First Search” using a stack as in the Rat-in-the-Maze problem or in “Breadth First Search” using a queue as in Lee’s Wire Routing problem. ➺ This process continues until no new unlabeled adjacent component pixels are found. Image Component Labeling Slide # 3 Image Component Labeling Example 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 DS&A Image Component Labeling Slide # 4 Image Component Labeling Example 1 2 2 1 1 3 3 1 3 1 1 2 2 1 2 1 1 3 2 1 2 1 1 2 1 3 3 1 3 1 3 1 1 4 4 1 1 4 4 1 1 2 1 5 5 1 5 1 5 1 1 5 5 1 1 5 5 1 1 5 5 1 5 1 5 1 1 4 1 4 4 1 1 4 4 1 1 4 1 4 1 4 4 1 1 4 4 1 1 4 1 5 5 1 1 4 4 1 4 1 1 6 6 1 6 1 1 6 6 1 6 1 1 6 6 1 6 1 1 6 6 1 1 4 1 6 1 6 6 1 6 1 6 1 1 6 6 1 1 6 1 6 6 1 6 1 DS&A Image Component Labeling Slide # 5 Implementation ❏ ❏ ❏ The program to label component pixels uses much of the development used for the “Rat in the Maze” and “Lee’s Wire-Routing” problems. ➺ To move around the image with ease, we surround the image with a wall of blank (here 0 pixels), unlike Rat in the Maze and Lee’s Wire (Why?). ➺ We use the “offset” array to determine the pixels adjacent to a given pixel. See starter code on Assignment 1 Canvas webpage... Your task is to complete the labelComponents( ) method and adapt the DFS and BFS algorithms from “Rat in Maze” and “Lee’s Wire Router” in order to implement Depth First Search and Breadth First Search. DS&A Image Component Labeling Slide # 6 Important Design Requirement ❏ ❏ ❏ ❏ Your design must be based on Modularity and "Separation of Concerns". The Stack and Queue Data Structure implementations must be based on "Information Hiding" and "Encapsulation". The Application Code (e.g. Rat In Maze, Wire Router, Image Component Labeling, ...) know about the Data Structures only through their Interfaces (APIs). Remember that interfaces represent behavior, while classes represent implementation. DS&A Image Component Labeling Slide # 7 General Strategy 1) Prompt the user for two values: ➺ The DIMENSION of the image in pixels (square grid of pixels) Must be an integer between 5 and 20 inclusive (default is 15) ➺ The DENSITY of foreground pixels Must be a floating number between 0.0 and 1.0 (default is 0.33) For example, DENSITY = 0.25, means that 25% of the pixels should be foreground and the remaining 75% should be background. 2) Your program maintains a 2-D grid (array) of objects, where each object keeps track of two values, the image component label (to be assigned by DFS or BFS), and the order in which the pixel was discovered, which depends on the search strategy (DFS or BFS). Accordingly, each pixel is represented by an object that encapsulates a label (component label: 2, 3, 4, ...) and (order of discovery: 1, 2, 3, ...). DS&A Image Component Labeling Slide # 8 General Strategy – continue ❏ ❏ ❏ ❏ For the purpose of illustration here, assume only for now, that the user chooses a DIMENSION of 15x15 pixels. Create a 2-dimensional array of size 17 by 17 (15 + 2 for the surrounding walls). Generate a grid of 15x15 cells, in the inner part of the array (i.e. excluding the surrounding walls), where each cell is either 0 (zero) for background, or 1 (one) for foreground. See slide 10 below. Important Observation: Notice that for the “Image component Labeling”, we ignore the background 0’s and trace through the foreground 1’s, which is the opposite of “Rat in Maze” and “Lee’s Wire Router”, where we avoid the 1’s which are walls or transistors, and trace through the 0’s, which are open paths. DS&A Image Component Labeling Slide # 9 General Strategy – continue 3) Create imageDFS by randomly generating the pixel values. The generateImage operation will populate the pixel [ ] [ ] square array that represents the image with 1’s and 0’s for foreground and background, respectively. Don’t forget the artificial wall around the image. Initially, the 0/1 randomly generated number may be stored in each pixel[row][col].label (i.e. the label field of each pixel[row][col] object) DS&A Image Component Labeling Slide # 10 Generating a Random Images ❏ ❏ Let R be the DENSITY, where 0 ≤ R 9 1 The following pseudocode is used to populate the pixel [ ] [ ] square array (image): for (int row = 1; row <= DIMENSION; row++) for (int col = 1; col <= DIMENSION; col++) { // generate a Random number R between 0 and 1; if (R < DENSITY) pixel[row][col].label = 1; // foreground else pixel[row][col].label = 0; // background } ➺ DS&A If for example, DENSITY = R = 0.25, then the image will be 75% background and 25% foreground. Image Component Labeling Slide # 11 General Strategy – continue 4) Create imageBFS as an identical copy of imageDFS. ➺ After populating the grid with pixel values and surrounding walls, clone the grid so that the first copy is used for DFS and the identical second copy for BFS. 5) Apply the Depth First Search algorithm to imageDFS Apply the Breadth First Search Algorithm to imageBFS Proceed as in the adapted “Rate in Maze” for DFS and then as in the adapted “Lee’s Wire Router” for BFS. The image components are labeled in the order discovered by the respective search algorithm. All pixels of the same component will have the same label value and an order number which starts at 1 and keeps counting per image component pixel. 6) Printout the two resulting images (see slides 13 and 14 below). DS&A Image Component Labeling Slide # 12 Example – Initial (After random pixel generation) ❏ ❏ Suppose the user entered: ➺ DIMENSION = 7 ➺ DENSITY = 0.33 (default) Then your program may generate the following image with 16 foreground pixels out of the 49 image pixels. (This doesn’t account for the surrounding wall.) PS: The Red/Black colors are just for ease of reading here. The x values are unset ORDER ivars, to be set by the DFS or BFS algorithm. ❏ Submit source code and output corresponding to DFS and BFS traversals (see next 2 slides). DS&A Image Component Labeling Slide # 13 Example – Corresponding DFS ❏ The Depth First Search algorithm will printout the following corresponding grid of pixels: DS&A Image Component Labeling Slide # 14 Example – Corresponding BFS ❏ And the Breadth First Search algorithm will printout the following corresponding grid of pixels: DS&A Image Component Labeling Slide # 15 Have Fun!

Tutor Answer

Prof_Befly
School: UC Berkeley

This is the final answer w...

flag Report DMCA
Review

Anonymous
Thanks, good work

Similar Questions
Hot Questions
Related Tags
Study Guides

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors