I want to read the pixel values from a pgm file, then compute the integral image and save the result to a text file. At first I used Dev C++ to compile my code, but the pixel values have segmentation faults(Only the first three rows are correct). My instructor said there were two small bugs in my code which caused segmentation faults, but I don't know where the bugs are. She also said that she cannot compile my code on visual studio, she wants me to send her a C++11 compliant version, what does that mean?
Code:
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
usingnamespace std;
int main()
{
int row =0, col =0, num_of_rows =0, num_of_cols =0, bits;
ifstream infile("testfile.pgm", ios::binary);
stringstream ss;
string inputLine ="";
getline(infile,inputLine);// read the first line : P5
if(inputLine.compare("P5")!=0) cerr <<"Version error"<< endl;
cout <<"Version : "<< inputLine << endl;
getline(infile,inputLine);// read the second line : comment
cout <<"Comment : "<< inputLine << endl;
ss << infile.rdbuf();//read the third line : width and height
ss >> num_of_cols >> num_of_rows;
cout << num_of_cols <<" columns and "<< num_of_rows <<" rows"<< endl;
int max_val;//maximum intensity value : 255
ss >> max_val;
cout<<max_val;
unsignedchar pixel;
unsignedint pixel_value[num_of_rows][num_of_cols];
unsignedint integral[num_of_rows][num_of_cols];
for(row =0; row < num_of_rows; row++){//record the pixel values
for(col =0; col < num_of_cols; col++){
ss >> pixel;
pixel_value[row][col]= pixel;
}
cout << endl;
}
for(int i=0; i<num_of_cols;i++){//compute integral image
integral[0][i]=integral[0][i-1]+pixel_value[0][i];
}
for(int i=0;i<num_of_rows; i++){
integral[i][0]=integral[i-1][0]+pixel_value[i][0];
}
for(int i =1; i < num_of_rows; i++){
for(int j =1; j < num_of_cols; j++){
integral[i][j]= integral[i -1][j]+ integral [i][j -1]- integral[i -1][j -1]+ pixel_value[i][j];
}
}
ofstream output1("pixel_value.txt");// output the intensity values of the pgm file
for(int k=0; k<num_of_rows; k++)
{
for(int r=0; r<num_of_cols; r++)
{
output1 << pixel_value[k][r]<<" ";
}
output1 <<";"<< endl;
}
ofstream output2("integral_value.txt");// output the integral image
for(int a=0; a<num_of_rows; a++)
{
for(int b=0; b<num_of_cols; b++)
{
output2 << integral[a][b]<<" ";
}
output2 <<";"<< endl;
}
infile.close();
return0;
}
Besides, when I used visual studio 2012 to compile the above code, I used dynamic array to redefine the two arrays:
Code:
int**pixel_value;
pixel_value =newint*[num_of_rows];
for(int i=0; i<num_of_rows;i++)
pixel_value[i]=newint[num_of_cols];
int**integral;
integral =newint*[num_of_rows];
for(int i=0; i<num_of_rows;i++)
integral[i]=newint[num_of_cols];
for(row =0; row < num_of_rows; row++){//record the pixel values
for(col =0; col < num_of_cols; col++){
ss >> pixel;
pixel_value[row][col]= pixel;
}
cout << endl;
}
After I changed the code, the output text file was completely blank! It couldn't read any values from the pgm file, does anyone know how to solve the problems? I really need to compile the code on visual studio 2012.