Write a CPP program? · Define and initialize a 3D array to keep RGB pixels struc
ID: 3761924 • Letter: W
Question
Write a CPP program?
· Define and initialize a 3D array to keep RGB pixels structured.
· Read pixels from three different files into your 3D array (you read the data from file number-by-number).
· Find average values for each layer R (red), G (green), B (blue) (Array[i][j][0] + Array[i][j][1] + Array[i][j][2] ) /3, which is called intensity of a pixel. In the notation of Array[i][j][c], c is the color band, i anc j are location of the pixel.
· Find maximum and minimum intensity pixels of the image. Also, print out those pixel addresses of min an max values. Intensity is average of R, G, and B values. In case of multiple minimum and maximum values, you should show all of the pixel locations.
· List address (x, y location) of pixels meeting if pixel's Red value is less than pixel's Blue value.
How do I upload the test files to this question?
Explanation / Answer
Complete Program:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
const int SIZE = 3;
int RGB[SIZE][SIZE][SIZE];
double averages[SIZE * SIZE];
ifstream red("RedPixelsInfo.txt");
ifstream green("GreenPixelsInfo.txt");
ifstream blue("BluePixelsInfo.txt");
if(!red || !green || !blue)
{
cout << "Unable to open the data files." << endl;
system("pause");
exit(EXIT_FAILURE);
}
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
for(int c = 0; c < SIZE; c++)
{
if(red && green && blue)
{
if(c == 0)
red >> RGB[i][j][c];
else if(c == 1)
green >> RGB[i][j][c];
else
blue >> RGB[i][j][c];
}
}
}
}
red.close();
green.close();
blue.close();
int k = 0;
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
double total = RGB[i][j][0] + RGB[i][j][1] + RGB[i][j][2];
averages[k] = total / 3.0;
k++;
}
}
double minimum = averages[0];
double maximum = averages[0];
for(int i = 1; i < k; i++)
{
if(averages[i] < minimum)
minimum = averages[i];
if(averages[i] > maximum)
maximum = averages[i];
}
cout << "R G B Intensity" << endl;
cout << "----------------------------------" << endl;
k = 0;
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
for(int c = 0; c < SIZE; c++)
{
cout << RGB[i][j][c] << " ";
}
cout << averages[k] << endl;
k++;
}
}
cout << " The minimum intensity " << minimum << " is at the (x, y) location(s):" << endl;
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
double avg = (RGB[i][j][0] + RGB[i][j][1] + RGB[i][j][2]) / 3.0;
if(avg == minimum)
cout << "(" << i << ", " << j << ")" << endl;
}
}
cout << " The maximum intensity " << maximum << " is at the (x, y) location(s):" << endl;
for(int i = 0; i < SIZE; i++)
{
for(int j = 0; j < SIZE; j++)
{
double avg = (RGB[i][j][0] + RGB[i][j][1] + RGB[i][j][2]) / 3.0;
if(avg == maximum)
cout << "(" << i << ", " << j << ")" << endl;
}
}
cout << endl;
system("pause");
return 0;
}
Sample Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.