提问人:StoneCodeStawny 提问时间:10/21/2019 最后编辑:StoneCodeStawny 更新时间:10/22/2019 访问量:66
const char* 转换为文件名字符串,以将 10 个不同的 .pgm 文件读入 2D 数组
const char* to an of filename strings to read in 10 diffrent .pgm files to a 2D array
问:
我一整天都在为此苦苦挣扎,我知道我的错误很简单,但我似乎无法克服它。这样做的目的是将 10 个不同的可移植灰度图 (pgm) 文件连续读取到 2D 数组中。第一个维度是实际的图像数据,一个线性读取的 28x28 数组,第二个维度是 10 个图像编号。便携式灰度图表示每个像素,数字为 x,使得 0 < x < 255 表示灰度刻度。为了让它正确读取和写入,我首先必须以整数的形式读取图像数据,然后将其转换为无符号字符。uchar images [img_size][imgNum]
好的,所以我遇到的问题是专门在我的职能中
for ( int i = 0; i < imgNum; i++){
instream.open(filename[i]); //this does not work when called in main
...并在主要中实现
int main(int argc, const char * argv[]) {
const char* filename[10] ={"digit_00.pgm","digit_01.pgm","digit_02.pgm","digit_03.pgm","digit_04.pgm","digit_05.pgm","digit_06.pgm","digit_07.pgm","digit_08.pgm","digit_09.pgm"};
uchar images[img_size][imgNum] ;
readPGMImages(images, img_size , filename , imgNum);
我似乎无法让编译器查看在哪里filename[i]
i=1
instream.open("digit_00.pgm")
这是我所有的代码。我确实需要在 readPGMImages 中规定的参数内进行操作。意思是使用任何东西,但对我不起作用。const char * filename
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
typedef unsigned char uchar ;
typedef unsigned int uint ;
using namespace std;
const int img_size = 784;
const int imgNum = 10;
void readPGMImages( uchar images [img_size][imgNum], int img_size ,
const char * filename[imgNum], int imgNum )
{
uint holder[img_size][imgNum];
int header;
ifstream instream;
for ( int i = 0; i < imgNum; i++){
instream.open(filename[i]); //this does not work
if (instream.fail()){
cout << "failed to open" << endl;
exit(1);}
instream >> header >> header >> header >> header ;
for (int j = 0; j < img_size ; j++){
instream >> holder[j][i];
holder[j][i] = static_cast<uchar>(holder[j][i]);
images[j][i]=holder[j][i] ; //edited here
cout << images[j][i] << endl;
}
instream.close() ; //edited here
}
}
int main(int argc, const char * argv[]) {
const char* filename[10] ={"digit_00.pgm","digit_01.pgm","digit_02.pgm","digit_03.pgm","digit_04.pgm","digit_05.pgm","digit_06.pgm","digit_07.pgm","digit_08.pgm","digit_09.pgm"};
//I believe I have a mistake above but I'm unsure
uchar images[img_size][imgNum] ;
readPGMImages(images, img_size , filename , imgNum); //this is where I call the function
return 0;
}
我希望我的岗位礼仪没问题。
谢谢!
答: 暂无答案
评论
ifstream instream;
ifstream
instream.close()
instream
holder[j][i]
images[j][i]
holder
int
unsigned char
=