0

C ++でpgm画像のピクセル値を読み取る関数を実装する理由がないことは理解していますが、割り当てのために実行する必要があります。

精度の理由から、ピクセル値を読み取った後、imread(file) を使用して matlab で読み取ったピクセル値と比較しましたが、一致する値と一致しない値があり、理由がわかりません。

以下は c++ の関数で、画像はバイナリ形式です。

int Read_File_PGM(string filename){

    int row = 0, col = 0, numrows = 0, numcols = 0, bits;

    string filename;
    ifstream infile(filename.c_str(), ios::binary);

    stringstream ss;
    string inputLine = "";

    // First line : version
    getline(infile,inputLine);
    if(inputLine.compare("P5") != 0) cerr << "Version error" << endl;
    //else cout << "Version : " << inputLine << endl;

    // Second line : width and height
    ss << infile.rdbuf();
    ss >> numrows >> numcols;

    int max_bits;
    ss >> max_bits;
    unsigned char pixel;
    unsigned int pixel_value[numrows][numcols];
    //double sum = 0;

    // Following lines : data


            for(row = 0; row < numrows; ++row){
                    for (col = 0; col < numcols; ++col){
                            infile.read(pixel, 1);
                            ss >> pixel;
                            pixel_value[row][col] = (int)pixel;
            }

            }

    infile.close();


    }
    return 0;

}

4

1 に答える 1