1

opencv を使用して画像を表示する際に問題が発生しています。私のコードは現在動作しているので、サイズが 710X710 の unsigned short の 78 個の画像を単一の配列にロードする関数があります。データをファイルに書き込んでimageJで読み取ることで、これが機能することを確認しました。現在、配列から単一の画像フレームを抽出し、それを Mat にロードして処理を実行しようとしています。今、私はこれを行うために2つの方法を試しました。出力を読み取ろうとしない場合、コードはコンパイルおよび実行されますが、cout<

私の質問は、サイズが 710*710 の 78 個の画像の大きな 1 次元配列からデータを単一の Mat 画像に抽出するにはどうすればよいかということです。または、画像を寸法 710X710X78 の 3-D マットにロードし、必要に応じて各 710X710 スライスで操作できる、より効率的な方法はありますか?

int main(int argc, char *argv[])
{
    Mat OriginalMat, TestImage;

    long int VImageSize = 710*710;
    int NumberofPlanes = 78;
    int FrameNum = 150;

    unsigned short int *PlaneStack = new unsigned short int[NumberofPlanes*VImageSize];
    unsigned short int *testplane = new unsigned short int[VImageSize];

    /////Load PlaneStack/////
    Load_Vimage(PlaneStack, Path, NumberofPlanes); 

    //Here I try to extract a single plane image to the mat testplane, I try it two    different ways with the same results
    memcpy(testplane, &PlaneStack[710*710*40], VImageSize*sizeof(unsigned short int));
    //copy(&PlaneStack[VImageSize*40],&PlaneStack[VImageSize*41], testplane);

    // move single plane to a mat file
    OriginalMat = Mat(710,710,CV_8U, &testplane) ;
    //cout<<OriginalMat;

    namedWindow("Original");
    imshow("Original", OriginalMat);

}
4

1 に答える 1

2

問題は、コンストラクターMat::Mat(int rows, int cols, int type, void* data)を 16 ビット データ (unsigned short int) へのポインターと共に使用しているが、型CV_8U(8 ビット) を指定していることです。したがって、16 ビット ピクセルの最初のバイトは OriginalMat の最初のピクセルになり、最初のピクセルの 2 番目のバイトは OriginalMat の 2 番目のピクセルになります。

16 ビットの Mat を作成し、表示する場合は 8 ビットに変換する必要があります。たとえば、次のようになります。

int main(int argc, char *argv[])
{
    long int VImageSize = 710*710;    
    int NumberofPlanes = 78;
    int FrameNum = 150;

    /////Load PlaneStack/////
    unsigned short int *PlaneStack = new unsigned short int[NumberofPlanes*VImageSize];      
    Load_Vimage(PlaneStack, Path, NumberofPlanes); 

    // Get a pointer to the plane we want to view
    unsigned short int *testplane = &PlaneStack[710*710*40];

    // "move" single plane to a mat file
    //  actually nothing gets moved, OriginalMat will just contain a pointer to your data.
    Mat OriginalMat(710,710,CV_16UC1, &testplane) ;

    double scale_factor = 1.0 / 256.0;
    Mat DisplayMat;
    OriginalMat.convertTo(DisplayMat, CV_8UC1, scale_factor);

    namedWindow("Original");
    imshow("Original", DisplayMat);
}
于 2013-11-11T12:43:57.393 に答える