#include <iostream> #include <fstream> //file read/write #include <string> //str
ID: 3843867 • Letter: #
Question
#include <iostream>
#include <fstream> //file read/write
#include <string> //string library
using namespace std;
{
// application to image processing
int row = 0, col = 0, numrows = 0, numcols = 0;
ifstream infile("deep.pgm", ios::in | ios::binary);
string inputLine = "";
// Read image header
// First line : version
getline(infile, inputLine); // read one line
if (inputLine.compare("P5") != 0) cerr << "Version error" << endl;
else cout << "Version : " << inputLine << endl;
// Second line: comments
getline(infile, inputLine); // skip comments
// Second line : size
infile >> numcols >> numrows;
cout << numcols << " columns and " << numrows << " rows" << endl;
//maximum intensity value : 255 (8bit gray)
int max_val;
infile >> max_val;
cout << max_val;
unsigned char* data1D = new unsigned char[numrows*numcols]; // for data read
infile.read(reinterpret_cast(data1D), numrows*numcols * sizeof(unsigned char));
// Now print the array and process the data
for (row = 0; row < numrows; ++row) {
for (col = 0; col < numcols; ++col) {
cout << (int)data1D[row*numcols + col] << ' ';
if(data1D[row*numcols + col] > 100) // image processing: binarization
data1D[row*numcols + col] = 255;
else
data1D[row*numcols + col] = 0;
}
cout << row << endl;
}
infile.close();
// save to image
ofstream ofp;
string fname = "processed.pgm";
ofp.open(fname, ios::binary );
if (!ofp) {
cout << "Can't open file: " << fname << endl;
exit(1);
}
// write image head
ofp << "P5 " << endl;
ofp << numcols << " " << numrows << endl;
ofp << max_val < ofp.write(reinterpret_cast(data1D), numcols*numrows);
if (ofp.fail()) {
cout << "Can't write image " << fname << endl;
exit(0);
}
ofp.close();
delete(data1D);
system("pause");
return 0;
}
Explanation / Answer
#ifndef IMAGE_H
#define IMAGE_H
class Image
{
public:
Image(); //constructor
Image(int numRows, int numCols, int grayLevels); //constructor
~Image(); //destructor
Image(const Image& oldImage); //constructor
void operator=(const Image&); //overloaded assignment operator
void setImageInfo(int numRows, int numCols, int maxVal);
void getImageInfo(int &numRows, int &numCols, int &maxVal);
int getPixelVal(int row, int col);
void setPixelVal(int row, int col, int value);
bool inBounds(int row, int col); //checks to see if a pixel is in bounds
void getSubImage(int upperLeftRow, int upperLeftCol,
int lowerRightRow, int lowerRightCol, Image& oldImage);
void enlargeImage(int value, Image& oldImage);
void shrinkImage(int value, Image& oldImage);
void reflectImage(bool flag, Image& oldImage);
void translateImage(int value, Image& oldImage);
void rotateImage(int theta, Image& oldImage);
Image operator+(const Image &oldImage); //overloaded + operator
Image operator-(const Image& oldImage); //overloaded - operator
void negateImage(Image& oldImage);
private:
int N; // number of rows
int M; // number of columns
int Q; // number of gray levels
int **pixelVal; //2D array
};
#endif
Image::Image(int numRows, int numCols, int grayLevels)
/* Creates an Image of numRows x numCols and creates the arrays for it*/
{
N = numRows;
M = numCols;
Q = grayLevels;
pixelVal = new int *[N];
for(int i = 0; i < N; i++)
{
pixelVal[i] = new int [M];
for(int j = 0; j < M; j++)
pixelVal[i][j] = 0;
}
}
int readImage(char fname[], Image& image)
{
int i, j;
int N, M, Q;
unsigned char *charImage;
char header [100], *ptr;
ifstream ifp;
ifp.open(fname, ios::in | ios::binary);
if (!ifp) //error checking
{
cout << "Can't read image: " << fname << endl;
exit(1);
}
// read header
ifp.getline(header,100,' '); //magic number
if ( (header[0]!=80) || (header[1]!=53) ) //if not P5
{
cout << "Image " << fname << " is not PGM" << endl;
exit(1);
}
ifp.getline(header,100,' ');
while(header[0]=='#') //file name line in file starts with #
ifp.getline(header,100,' ');
M=strtol(header,&ptr,0); //number of colums
N=atoi(ptr); //number of rows
ifp.getline(header,100,' ');
Q=strtol(header,&ptr,0); //max gray value
charImage = (unsigned char *) new unsigned char [M*N]; //creates 2D array
ifp.read( reinterpret_cast<char *>(charImage), (M*N)*sizeof(unsigned char)); //reads in 2D array
if (ifp.fail())
{
cout << "Image " << fname << " has wrong size" << endl;
exit(1);
}
ifp.close();
// Convert the unsigned characters to integers
int val;
for(i=0; i<N; i++)
for(j=0; j<M; j++)
{
val = (int)charImage[i*M+j];
image.setPixelVal(i, j, val);
}
delete [] charImage;
return (1);
}
int readImageHeader(char fname[], int& N, int& M, int& Q, bool& type)
{
int i, j;
unsigned char *charImage;
char header [100], *ptr;
ifstream ifp;
ifp.open(fname, ios::in | ios::binary);
if (!ifp)
{
cout << "Can't read image: " << fname << endl;
exit(1);
}
// read header
type = false; // PGM
ifp.getline(header,100,' ');
if ( (header[0] == 80) && (header[1]== 53) )
{
type = false;
}
else if ( (header[0] == 80) && (header[1] == 54) )
{
type = true;
}
else
{
cout << "Image " << fname << " is not PGM or PPM" << endl;
exit(1);
}
ifp.getline(header,100,' ');
while(header[0]=='#')
ifp.getline(header,100,' ');
M=strtol(header,&ptr,0);
N=atoi(ptr);
ifp.getline(header,100,' ');
Q=strtol(header,&ptr,0);
ifp.close();
return(1);
}
int main(int argc, char *argv[])
{
int M, N, Q; // rows, cols, grayscale
int val;
bool type;
// read image header
readImageHeader(argv[1], N, M, Q, type);
// allocate memory for the image array
Image image(N, M, Q);
// read image
readImage(argv[1], image);
}
void Image::rotateImage(int theta, Image& oldImage)
/*based on users input and rotates it around the center of the image.
{
int r0, c0;
int r1, c1;
int rows, cols;
rows = oldImage.N;
cols = oldImage.M;
Image tempImage(rows, cols, oldImage.Q);
float rads = (theta * 3.14159265)/180.0; //converts the degree given by user into radians
//find midpoints
r0 = rows / 2;
c0 = cols / 2;
// goes through the array of the oldImage, uses the formulas to find where the pixel should go
// then puts the old pixel value into the new pixel position on the tempImage
for(int r = 0; r < rows; r++)
{
for(int c = 0; c < cols; c++)
{
r1 = (int) (r0 + ((r - r0) * cos(rads)) - ((c - c0) * sin(rads)));
c1 = (int) (c0 + ((r - r0) * sin(rads)) + ((c - c0) * cos(rads)));
if(inBounds(r1,c1)) // makes sure the new pixel location is in bounds,
{
tempImage.pixelVal[r1][c1] = oldImage.pixelVal[r][c];
}
}
}
for(int i = 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
if(tempImage.pixelVal[i][j] == 0)
tempImage.pixelVal[i][j] = tempImage.pixelVal[i][j+1];
}
}
oldImage = tempImage;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.