Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++, Using Queues to get Image Component Labeling 1) Starter Code // image compo

ID: 3888120 • Letter: C

Question

C++, Using Queues to get Image Component Labeling

1) Starter Code

// image component labeling

#include <iostream>
#include "make2dArray.h"
#include "arrayQueue.h"
#include "position.h"

using namespace std;

// global variables
int **pixel;
int size; // number of rows and columns in the image

// functions
void welcome()
{// Not yet implemented.
}

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;
for (int i = 1; i <= size; i++)
for (int j = 1; j <= size; j++)
cin >> pixel[i][j];
}

void labelComponents()
{// Label the components.

//
// Add Your Code Here
//

}

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();
}

------------------------------------------------------------------

2). Sample Input

7
0 0 1 0 0 0 0
0 0 1 1 0 0 0
0 0 0 0 1 0 0
0 0 0 1 1 0 0
1 0 0 0 1 0 0
1 1 1 0 0 0 0
1 1 1 0 0 0 0

Implementation o The program to label component pixels uses much of the development used for the "rat in the maze" and "wire-routing" problems. To move around the image with ease, we surround the image with a wall of blank (here 0 pixels). We use the "offset" array to determine the pixels adjacent to a given pixel. o Once a seed pixel of an unlabeled component is found, the image labeling process is very similar to the process used in finding a path in the "Rat-in- the-Maze" problem, or alternatively in "Lee's Wire Routing" problem. o See starter code. Your task is simply to complete the labelComponents() method a Submit source code and output corresponding to the given input DS&A; Imoge Component Labeling Sitde # 6

Explanation / Answer

/* Please note that since question only asks us to write labelComponents() method, we are writing just the method, not the whole program. */

/* Please note that we are assuming that Position is a readily available structure having two int data values called x and y to denote x and y coordinates of any pixel in the image. */

// And we are assuming that a Queue is also readily available for use by including the file "arrayQueue.h"

void labelComponents()

{ // Declaring 2 vectors to denote directions in x and y axis

int dx[] = {+1, 0, -1, 0};

int dy[] = {0, +1, 0, -1};

/* declaring a 2D array called label[][] where each element will represent that whether the pixel at that position has been labelled or not - 0 means unlabelled and 1 means labelled */

int label[size+2][size+2];

arrayQueue<Position> PixelsToBeLabelled ; /* getting a queue object available from arrayQueue.h file and the datatype of queue is Position structure */

int component = 0; // Component number

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

for (int j = 0; j < size + 2; ++j)

if (!label[i][j] && pixel[i][j]) // if the pixel value is 1 and it's not labelled yet

{

label[i][j] = ++component; // seed pixel found

for (int direction = 0; direction < 4; ++direction)

{

Position node = new Position(i+dx[direction], j+dy[direction]);

PixelsToBeLabelled.push(node); // putting all the neighbours of the seed pixel in the queue

}

  

while(!PixelsToBeLabelled.IsEmpty())

{

Position pos = PixelsToBeLabelled.pop();

label[pos.x][pos.y] = component; /* Popping from the queue and labelling the pixels. Thus labelling them in Breadth First Search order */

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote