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
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 */
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.