Compare two images in JOGL

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Compare two images in JOGL

Pepe1
I'm working on a hobby project in which two images get compared to each other and the similarity between these two images is calculated. My current implantation is pretty straightforward; The two images are drawn on screen and then glReadPixels() is used to get the bytebuffer for each image. Using the bytebuffers, the error value for each pixel is computed.

This approach works, but the problem is that two calls to glReadPixels absolutely kills performance. I did some googling and came across two solutions: 1. Use pixel buffer objects, but performance doesn't seem to be great for these either. 2. Use EGLImage, problem with this approach is that one of the images isn't really an image but rather a collection of polygons drawn directly on screen.

So now my question is, what would be the best approach to comparing two images and still have an acceptable performance?

For reference, my current code:

        public void evaluate()
        {
                ByteBuffer  bbOrigin    = getBytebuffer(imageBounds[0][0], imageBounds[0][1], imageBounds[0][2], imageBounds[0][3]);
                ByteBuffer  bbCurrent   = getBytebuffer(imageBounds[1][0], imageBounds[1][1], imageBounds[1][2], imageBounds[1][3]);
                int         error       = 0;

                while(bbOrigin.hasRemaining() && bbCurrent.hasRemaining())
                {
                        error += Math.abs(bbOrigin.get() - bbCurrent.get());
                }
        }
        private ByteBuffer getBytebuffer(int x1, int y1, int x2, int y2)
        {
                int         width   = (x2 - x1);
                int         height  = (y2 - y1);
                ByteBuffer  bb      = ByteBuffer.allocate(4 * width * height);

                gl.glReadPixels(x1, y1, width, height, GL2.GL_RGBA, GL2.GL_BYTE, bb);

                return bb;
        }
Reply | Threaded
Open this post in threaded view
|

Re: Compare two images in JOGL

gouessej
Administrator
Hi

Use TextureIO to create 2 textures or 2 TextureData objects, use these textures to display your images in a JOGL canvas (GLWindow, GLCanvas, GLJPanel, NewtCanvasAWT, ...) and call TextureData.getBuffer() to get the data.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Compare two images in JOGL

Pepe1
Thanks for the reply, I'll try that. One question though: how would I go about creating a texture from a pre-defined square on a GLWindow? I'm asking because I'm drawing polygons within this pre-defined square and I want to compare it to an actual image drawn elsewhere on the GLWindow.

I apologize if that sounds vague, but I'm basically just doing this: http://alteredqualia.com/visualization/evolve/ in java.
Reply | Threaded
Open this post in threaded view
|

Re: Compare two images in JOGL

gouessej
Administrator
Use GLReadBufferUtil, there are some examples in the Github project jogl-demos.

However, I fear it calls glReadPixels under the hood :(
Julien Gouesse | Personal blog | Website