2016-06-19 8 views
1

は次のとおりです。glReadPixelsは非正方形の幅と高さでは機能しませんか?私のビューポート上で現在何

enter image description here

はここに私の輸出画像法です。

void exportImage() 
{ 
    int width = 200; 
    int height = 100; 
    GLubyte *data = new GLubyte[4*width*height]; 
    glReadPixels(0,0,width,height,GL_RGBA,GL_UNSIGNED_BYTE, data); 
    cv::Mat imageMat(width, height, CV_8UC4, data); 
    cv::flip(imageMat, imageMat, 0); 
    cv::imwrite("ok.jpg",imageMat); 
} 

使用時800×800、(青になってきて黄色気にしないでください)

enter image description here

使用200×200、

enter image description here

でも使用200x100、

void exportImage() 
{ 
    int width = 200; 
    int height = 100; 
    GLubyte *data = new GLubyte[4*width*height]; 
    glReadPixels(0,0,width,height,GL_RGBA,GL_UNSIGNED_BYTE, data); 
    cv::Mat imageMat(width, height, CV_8UC4, data); 
    cv::flip(imageMat, imageMat, 0); 
    cv::imwrite("ok.jpg",imageMat); 
} 

enter image description here

まずはすべての幅が高さになり、画像が間違っています。それは配列のインデックスのシフトの問題のように見えますが、私は割り当てがなぜコードによって幅と高さに応じて変わるのか理解できませんでした。

私はglReadPixelsに幅と高さを交換しようとしたとき:

void exportImage() 
{ 
    int width = 200; 
    int height = 100; 
    GLubyte *data = new GLubyte[4*width*height]; 
    glReadPixels(0,0,height,width,GL_RGBA,GL_UNSIGNED_BYTE, data); 
    cv::Mat imageMat(width, height, CV_8UC4, data); 
    cv::flip(imageMat, imageMat, 0); 
    cv::imwrite("ok.jpg",imageMat); 
} 

enter image description here

画像が正しく見えますか?しかし、幅と高さは依然としてスワップしていましたか?

+0

あなたは "180x200" と言うが、スクリーンショットは "190x200" を言います。どうしたの? –

+0

ああ、ミスタイプです。スクリーンショットの情報は正しいです。 – 5argon

+0

ちょっと待って、実際には「高さ」に190を入れましたが、なぜそれが幅として出てきましたか?たぶんこれが問題です。 – 5argon

答えて

0

私は非常にばかげた過ちを犯しました。 glReadPixelsは実際には問題ありませんが、cv :: Matの初期化は高さx幅を意味する行x colsに対応しています。だから、スワップされて、出力は今よく見えます。

void exportImage() 
{ 
    int width = 200; 
    int height = 100; 
    GLubyte *data = new GLubyte[4*width*height]; 
    glReadPixels(0,0,width,height,GL_RGBA,GL_UNSIGNED_BYTE, data); 
    cv::Mat imageMat(height, width, CV_8UC4, data); 
    cv::flip(imageMat, imageMat, 0); 
    cv::imwrite("ok.jpg",imageMat); 
} 

enter image description here

関連する問題