0

これまで検索機能を使ってやってきたので、これは私の最初の投稿です。しかし今、私は次の問題で丸一日を無駄にしました:

12 ビット (16 ビットとして書き込まれる) グレースケール ビデオを記録し、バイナリ ストリーム ファイル (ヘッダーなどなし) に直接書き込みました。

ここでのタスクは、ファイルを読み取り、すべてのフレームを 16 ビット pgm として出力することです。

次の抜粋は、私が試したことを示しています。出力は有効な pgm ですが、「ホワイト ノイズ」が含まれています。

    ...
    imageBufferShort = new short[imageWidth*imageHeight* sizeof(short)];
    ...
    streamFileHandle.read(reinterpret_cast<char*>(imageBufferShort),2*imageWidth*imageHeight); //double amount because 8bit chars!
    // As .read only takes chars, I thought, that I just read the double amount of char-bytes and when it is interpreted as short (=16bit) everything is ok?!?

    ...now the pgm output:

    std::ofstream f_hnd(fileName,std::ios_base::out |std::ios_base::binary |std::ios_base::trunc);
    // write simple header
    f_hnd.write("P5\n",3);
    f_hnd << imageWidth << " " << imageHeight << "\n4095\n";  //4095 should tell the pgm to use 2 bytes for each pixel

    f_hnd.write(reinterpret_cast<char*>(imageBufferShort),2*imageWidth*imageHeight);
    f_hnd.close();

繰り返しますが、ファイルは生成されて表示可能ですが、ゴミが含まれています。最初の推測は大丈夫ですか?2つの「文字」を読み取り、それらを1つの「短い」として処理しますか? また、すべての行の後に空白を入れますが、これは何も変わらないので、この短いコードを投稿することにしました。

助けてくれてありがとう!

4

2 に答える 2

1

@Domi と @JoeZ が指摘したように: あなたのエンディアンはおそらくめちゃくちゃです。つまり、バイトの順序が間違っています。

問題を解決するには、すべてのピクセルを反復処理し、ファイルに書き戻す前にそのバイトを交換する必要があります。

于 2013-11-29T08:24:11.507 に答える
0

問題が解決しました。どうもありがとうございました。エンディアンネスは確かに問題でした。解決策を以下に示します。

    f_hnd << "P5" << " " << imDimensions.GetWidth() << " " << imDimensions.GetHeight() << " " << "4095\n";

    // convert imageBufferShort to Big-Endian format
    unsigned short imageBufferShortBigEndian[imDimensions.GetWidth()*imDimensions.GetHeight()];

    for (int k=0 ; k<imDimensions.GetWidth()*imDimensions.GetHeight() ; k++)
    {
        imageBufferShortBigEndian[k] = ( (imageBufferShort[k] << 8) | (imageBufferShort[k] >> 8) );
    }

    f_hnd.write(reinterpret_cast<char*>(imageBufferShortBigEndian),2*imDimensions.GetWidth()*imDimensions.GetHeight());
    f_hnd.close();

imageBufferShort は unsigned short-array にも対応しています。符号付きの型を使用すると、ビットシフト変換が少し難しくなります。

再度、感謝します!

于 2013-12-02T09:20:22.183 に答える