Reputation: 1
I'm doing an assignment on Image Filtering using gray scale numbers (0-256). I've created an array of 16x16 and I need to evenly space the columns so they format as more of pixels on an image much like a table. I'm newer to coding so not sure what code I should include as there is a decent bit. Formatting on here might change the look of the code but anything helps. Towards the end at for print_image is where I assume I could enter std::cout or std:setw() but I'm not sure.
#include <iostream>
#include <string>
#include <cstdlib> // Random support
#include <iomanip> // I/O Manipulation support
#define MIN_RANDOM_NUMBER 0
#define MAX_RANDOM_NUMBER 256
#define ROWS 16
#define COLS 16
using namespace std;
void print_image(unsigned char image[][COLS], int nRows)
{
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++){
cout << (int) image[row][col] << " ";
}
cout << endl;
}
}
void initialize_random_image(unsigned char image[][COLS], int nRows, int min_value, int max_value)
{
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++){
image[row][col] = rand() % (MAX_RANDOM_NUMBER-MIN_RANDOM_NUMBER) + MIN_RANDOM_NUMBER;
}
}
}
void filter_image(unsigned char ImageIn[][COLS], int nRows,
unsigned char ImageOut[][COLS],
char ApplyFilter[3][3])
{
for (int row = 1; row < ROWS-1; row++) {
for (int col = 1; col < COLS-1; col++){
int sum = ImageIn[row-1][col-1] * ApplyFilter[0][0]+
ImageIn[row-1][col] * ApplyFilter[0][1]+
ImageIn[row-1][col+1] * ApplyFilter[0][2]+
ImageIn[row][col-1] * ApplyFilter[1][0]+
ImageIn[row][col] * ApplyFilter[1][1]+
ImageIn[row][col+1] * ApplyFilter[1][2]+
ImageIn[row+1][col-1] * ApplyFilter[2][0]+
ImageIn[row+1][col] * ApplyFilter[2][1]+
ImageIn[row+1][col+1] * ApplyFilter[2][2];
if (sum > 255)
sum; 255;
ImageOut[row][col] = (unsigned char) sum;
}
}
}
int main()
{
unsigned char ImageInput[ROWS][COLS];
unsigned char ImageOutput[ROWS][COLS];
char Filter[3][3] = { {0,-1,0}, {-1,5,-1}, {0,-1,0} };
srand(13);
cout << setw(25) << "Original" << endl;
print_image(ImageInput,ROWS);
filter_image(ImageInput, ROWS, ImageOutput, Filter);
cout << setw(25) << "Filtered" << endl;
print_image(ImageOutput,ROWS);
}
Upvotes: 0
Views: 274
Reputation: 4641
Change your print_image function and use setw
to print the image pixel values in proper tabular format. Also, use static_cast
instead of C-style casts.
void print_image(unsigned char image[][COLS], int nRows)
{
for (int row = 0; row < ROWS; row++) {
for (int col = 0; col < COLS; col++) {
// set the width of the next field to 4 (one space and three digits)
cout << setw(4) << static_cast<int>(image[row][col]);
}
cout << endl;
}
}
Upvotes: 1