主函数:
for (int i = 0; i < Num; i++) {
for (int j = 0; j < num; j++) {
stringstream str_l, str_r;
string sL, sR;
//读取PGM图像文件
str_l << "data/Daimler/" << i + 1 << "/Left/left_" << j + 1 << ".pgm";
str_r << "data/Daimler/" << i + 1 << "/Right/right_" << j + 1 << ".pgm";
cout << str_l.str() << endl;
cout << str_r.str() << endl;
sL = str_l.str();
sR = str_r.str();
const char* sl = sL.c_str();
const char* sr = sR.c_str();
process(sl, sr);
}
}
process函数
void process(const char* file_1, const char* file_2) {
cout << "Processing: " << file_1 << ", " << file_2 << endl;
// load images
image<uchar>* I1, * I2;
I1 = loadPGM(file_1);
I2 = loadPGM(file_2);
.........
loadPGM函数
void pnm_read(std::ifstream &file, char *buf) {
char doc[BUF_SIZE];
char c;
file >> c;
while (c == '#') {
file.getline(doc, BUF_SIZE);
file >> c;
}
file.putback(c);
file.width(BUF_SIZE);
file >> buf;
file.ignore();
}
image<uchar> *loadPGM(const char *name) {
char buf[BUF_SIZE];
// read header
std::ifstream file(name, std::ios::in | std::ios::binary);
pnm_read(file, buf);
if (strncmp(buf, "P5", 2)==0) {
std::cout << "ERROR: Could not read file " << name << std::endl;
throw pnm_error();
}
pnm_read(file, buf);
int width = atoi(buf);
pnm_read(file, buf);
int height = atoi(buf);
pnm_read(file, buf);
if (atoi(buf) > UCHAR_MAX) {
std::cout << "ERROR: Could not read file " << name << std::endl;
throw pnm_error();
}
// read data
image<uchar> *im = new image<uchar>(width, height);
file.read((char *)imPtr(im, 0, 0), width * height * sizeof(uchar));
return im;
}
每次一运行到read header时,就显示无法读取内存,然后抛出pnm_error()错误,显示无法读取文件,具体内存区如下: