PDA

View Full Version : Fast pixel change detection



niyodanie
07-21-2012, 03:30 PM
Hi everyone,

I'm trying to implement a really fast method of pixel change detection.
This has two main parts: capturing and comparing screenshots.


For capturing:
I'm using PBO so that glReadPixels() will return after the call.
Extract of this part of the code is here:

if(pboUsed){ //with PBO
// OpenGL performs asynch DMA transfer, so glReadPixels() will return immediately.
glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB,pboIds[index]);
glReadPixels(posx,posy,500,500, GL_BGR_EXT, GL_UNSIGNED_BYTE,0);// posx and posy are given by the mouse position
GLubyte* screenshot = (GLubyte*)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB);
//ptr does now point to my screenshot..}

For two screenshots, I'll basically have two GLubyte* screenshot1 and screenshot2

For comparison :
I tried to use simple and efficient memcmp: memcmp(screenshot1,screenshot2,500*500*3) but it doesn't always detect differences.
And I'd like to detect any single pixel color change.

Are there any OGL methods that can help me achieve this ?

Thanks

aqnuep
07-21-2012, 04:34 PM
Why do you want to perform the comparison with the CPU? If you do it on the GPU there is no real need for a capture (you can directly render to a texture through FBO). Also, you don't have to do a readback or any unnecessary copy, just use a fragment shader to compare the pixel values.

niyodanie
07-21-2012, 07:39 PM
Thanks for your reply.
You are right, it would be great if everything was done on the GPU.
Got the part for rendering to a texture through FBO. But I have questions about the fragment shader part.
Is there anyway to compare pixels that are currently being rendered with pixels from a previous texture (holding the previous frame) using a fragment shader?
Or should the fragment shader have two textures as inputs ?

Sorry, I'm not familiar with fragment shaders so I may need guidance here.

P.S: I also did some research and found these threads: http://www.opengl.org/discussion_boards/showthread.php/159272-image-comparison?highlight=compare+images and http://www.opengl.org/discussion_boards/showthread.php/162066-comparing-two-textures-in-gpu/page2
I will try to implement these methods and see which one has the best performance.

aqnuep
07-21-2012, 10:50 PM
Or should the fragment shader have two textures as inputs ?
This is exactly what you have to do. Render both images to a texture through FBO and then use both textures as input to your comparison fragment shader.

niyodanie
07-22-2012, 12:11 PM
All right, thanks aqnuep. I also have another concern. For the case above, we were picking the whole frame. What if I need to monitor only specific portions of the frame? Let's say the user can select three different regions or portions of the screen. So here, we would have coordinates (x,y and width, height) of these regions like rectangles in windows programming. Can I use the same approach that is : using one texture with those three portions and same fragment shader ? How would I redirect those selected regions to the texture? And here, I need to be able to tell which specific region had its pixels changed.

niyodanie
07-28-2012, 11:39 AM
For the case above, we were picking the whole frame. What if I need to monitor only specific portions of the frame? Let's say the user can select three different regions or portions of the screen. So here, we would have coordinates (x,y and width, height) of these regions like rectangles in windows programming. Can I use the same approach that is : using one texture with those three portions and same fragment shader ? How would I redirect those selected regions to the texture? And here, I need to be able to tell which specific region had its pixels changed.
Is there anyone who can help?