- #1
- 12
- 0
Homework Statement
I'm making a program in C++ to take an image in .ppm format and either create its inverse or binary threshold.
2. The attempt at a solution
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib> //for exit()
#include <string>
#include <vector>
using namespace std;
vector<vector<int> >binaryThreshold(vector<vector<int> > A);
vector<vector<int> >inverse(vector<vector<int> > A);
int main()
{
string m_n;
int i,j,w,h,maxvalue;
int selection=0;
// Filenames
string inFilename = "";
string outFilename = "";
// For file I/O
ifstream fin; //input file stream
ofstream fout; //output file stream
cout<<"Enter the name of the input file: ";
cin>>inFilename;
/**
* Handle file opening for reading
*/
fin.open(inFilename.c_str()); //open the input file
// check for validity.
if (!fin.is_open())
{
cout << "Problem opening file " << inFilename << " for reading." << endl;
exit(1);
}
// read from input PPM file
fin>>m_n>>w>>h>>maxvalue;
// declare a vector of vectors of read-in dimensions
vector<vector<int> >img(h,w);
// fill vector of vectors with read-in pixel values
for(i=0; i<=(h); i++)
{
for(j=0; j<=(w); j++)
{
fin>>img[i][j];
}
}
// close in-file
fin.close();
// Ask user for type of transformation
while (selection<1 || selection>2)
{
cout<<"Transformation Options: "<<endl<<"1)Binary Threshold"<<endl<<"2)Inverse Transformation"<<endl;
cout<<"Your selection: "<<endl;
cin>>selection;
if (selection==1)
{
vector<vector<int> > binaryThreshold(vector< vector<int> > A);
}
if (selection==2)
{
vector<vector<int> > inverse(vector< vector<int> > A);
}
}
/**
* Handle file opening for writing
*/
fout.open(outFilename.c_str()); //open the input file
if (!fout.is_open())
{
cout << "Problem opening file " << outFilename << " for writing." << endl;
exit(1);
}
// file opened okay... start writing
// close file
fout.close();
return 0;
}
//your function definitions here
vector<vector<int> >binaryThreshold(vector<vector<int> > A)
{
int i,j,w,h;
for(i=0; i<=h; i++)
{
for(j=0; j<=w; j++)
{
if (A[i][j]<128)
{
return A[i][j]=0;
}
else
{
return A[i][j]=255;
}
}
}
}
vector<vector<int> >inverse(vector<vector<int> > A)
{
int i,j,w,h;
for(i=0; i<=h; i++)
{
for(j=0; j<=w; j++)
{
A[i][j]=A[i][j]-255;
}
}
}
I'm not really sure how to use functions properly (thats probably VERY obvious). I'm just looking for a push in the right direction here, the rest of the program seems to run just dandy.
Pretty much, I'm confused about how to take the variables from main () and apply them to the function, is it necessary to re-declare them in the function itself?
Additionally, I can read the original image vector just fine (I think at least, I tested the read of the magic number, dimensions, etc by outputting them.) However, I think my modification process is incorrect in terms of reading the original vector in the function, and assigning the new values to a new vector, or do i just re-assign the values within the same original vector of "img"?
I think those are the main problem areas within the program itself, any other tips would be great, thanks!