计算 GPU 上的图像相似度 [OpenGL/OcclusionQuery] [英] Count image similarity on GPU [OpenGL/OcclusionQuery]

查看:100
本文介绍了计算 GPU 上的图像相似度 [OpenGL/OcclusionQuery]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

OpenGL.假设我已经绘制了一张图像,然后使用 XOR 绘制了第二张图像.现在我在某处有一个带有非黑色像素的黑色缓冲区,我读到我可以使用着色器来计算 GPU 上的黑色 [ rgb(0,0,0) ] 像素?

OpenGL. Let's say I've drawn one image and then the second one using XOR. Now I've got black buffer with non-black pixels somewhere, I've read that I can use shaders to count black [ rgb(0,0,0) ] pixels ON GPU?

我还读到它必须对 OcclusionQuery 做一些事情.http://oss.sgi.com/projects/ogl-示例/注册表/ARB/occlusion_query.txt

I've also read that it has to do something with OcclusionQuery. http://oss.sgi.com/projects/ogl-sample/registry/ARB/occlusion_query.txt

有可能吗?怎么可能?[任何编程语言]

Is it possible and how? [any programming language]

如果您对如何通过 OpenGL/GPU 查找相似性有其他想法,那也太棒了.

If you've got other idea on how to find similarity via OpenGL/GPU - that would be great too.

推荐答案

我不确定你是如何做 XOR 位的(至少它应该很慢;我认为当前的任何 GPU 都没有加速),但是这是我的想法:

I'm not sure how you do the XOR bit (at least it should be slow; I don't think any of current GPUs accelerate that), but here's my idea:

  1. 有两个输入图像
  2. 开启遮挡查询.
  3. 将两个图像绘制到屏幕上(即设置了两个纹理的全屏四边形),使用计算 abs(texel1-texel2) 并杀死像素的片段着色器(在 GLSL 中丢弃) 如果像素相同(差异为零或低于某个阈值).最简单的可能只是使用 GLSL 片段着色器,然后您只需读取两个纹理,计算差异的 abs() 并丢弃像素.非常基本的 GLSL 知识就足够了.
  4. 获取通过查询的像素数.对于相同的像素,查询不会通过(像素会被着色器丢弃),对于不同的像素,查询会通过.
  1. have two input images
  2. turn on occlusion query.
  3. draw the two images to the screen (i.e. full screen quad with two textures set up), with a fragment shader that computes abs(texel1-texel2), and kills the pixel (discard in GLSL) if the pixels are the same (difference is zero or below some threshold). Easiest is probably just using a GLSL fragment shader, and there you just read two textures, compute abs() of the difference and discard the pixel. Very basic GLSL knowledge is enough here.
  4. get number of pixels that passed the query. For pixels that are the same, the query won't pass (pixels will be discarded by the shader), and for pixels that are different, the query will pass.

起初我想到了一种更复杂的方法,涉及深度缓冲,但后来意识到仅仅杀死像素就足够了.这是我的原版(但上面的更简单,更有效):

At first I though of a more complex approach that involves depth buffer, but then realized that just killing pixels should be enough. Here's my original though (but the above one is simpler and more efficient):

  1. 有两个输入图像
  2. 清除屏幕和深度缓冲区
  3. 将两个图像绘制到屏幕上(即设置了两个纹理的全屏四边形),使用计算 abs(texel1-texel2) 并杀死像素的片段着色器(在 GLSL 中丢弃) 如果像素不同.绘制四边形,使其深度缓冲值接近近平面.
  4. 在此步骤之后,深度缓冲区将包含相同像素的小深度值,以及不同像素的大(远平面)深度值.
  5. 打开遮挡查询,并绘制另一个全屏四边形,其深度比远平面更近,但比前一个四边形大.
  6. 获取通过查询的像素数.对于相同的像素,查询不会通过(深度缓冲区已经接近),对于不同的像素,查询会通过.你会使用 SAMPLES_PASSED_ARB 来获得这个.CodeSampler.com 上有一个遮挡查询示例,可以帮助您入门.
  1. have two input images
  2. clear screen and depth buffer
  3. draw the two images to the screen (i.e. full screen quad with two textures set up), with a fragment shader that computes abs(texel1-texel2), and kills the pixel (discard in GLSL) if the pixels are different. Draw the quad so that it's depth buffer value is something close to near plane.
  4. after this step, depth buffer will contain small depth values for pixels that are the same, and large (far plane) depth values for pixels that are different.
  5. turn on occlusion query, and draw another full screen quad with depth closer than far plane, but larger than the previous quad.
  6. get number of pixels that passed the query. For pixels that are the same, the query won't pass (depth buffer is already closer), and for pixels that are different, the query will pass. You'd use SAMPLES_PASSED_ARB to get this. There's an occlusion query example at CodeSampler.com to get your started.

当然,这一切都需要具有遮挡查询支持的 GPU.自 2002 年左右以来,大多数 GPU 都支持这一点,但一些低端 GPU(特别是 Intel 915(又名 GMA 900)和 Intel 945(又名 GMA 950))除外.

Of course all this requires GPU with occlusion query support. Most GPUs since 2002 or so do support that, with exception of some low-end ones (in particular, Intel 915 (aka GMA 900) and Intel 945 (aka GMA 950)).

这篇关于计算 GPU 上的图像相似度 [OpenGL/OcclusionQuery]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆