简体   繁体   中英

treating image from glReadPixels with OpenCV and return it as a texture

I am trying to start with some basic operations with OpenCV and GLES20 on Android using C++.

I use CameraGLSurfaceView and its callback onCameraTexture(...) which calls I pass into my native library.

Calls are flowing well, I can read frame buffer to vector and pass it to texture without changing and it works as expected.

But when I try to work with pixels I get image broken.

破碎的图像

My C++ code:

    cv::Mat in(w,h,CV_8UC4);
    cv::Mat out(w,h,CV_8UC4);

    glReadPixels(0, 0, w, h, GL_RGBA, GL_UNSIGNED_BYTE, in.data);

    // following operations break image >>
    cv::cvtColor(in, out, CV_RGBA2BGRA);
    cv::flip(out, in, 0);
    cv::cvtColor(in, out, CV_BGRA2RGBA);
    // << prev operations break image

    glBindTexture(GL_TEXTURE_2D, (GLuint) tex2);

    glTexImage2D(GL_TEXTURE_2D,
                 0,
                 GL_RGBA,
                 w,
                 h,
                 0,
                 GL_RGBA,
                 GL_UNSIGNED_BYTE,
                 out.ptr());

    glBindTexture(GL_TEXTURE_2D, 0);

    in.release();
    out.release();

Without signed operations picture goes to texture and is displayed well.

I understand that my mistake is in converting formats between OpenGL and OpenCV.

How to convert formats properly?

It's my mistake with sizes of Mat:

cv::Mat in( w , h ,CV_8UC4) should be cv::Mat in( h , w ,CV_8UC4)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM