首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用OpenGL模拟OpenCV的warpPerspective功能(透视转换)

如何使用OpenGL模拟OpenCV的warpPerspective功能(透视转换)
EN

Stack Overflow用户
提问于 2015-10-08 14:27:38
回答 3查看 3.1K关注 0票数 5

我在PythonandC++中使用OpenCV做了图像扭曲,在我选择的角落看到了可口可乐的标志。

使用下列图像:

这是:

带过渡图片和描述的完整专辑

我需要做到这一点,但在OpenGL。我要:

  • 在角落里,我要映射扭曲的图像
  • 将徽标图像转换为最终图像中的徽标图像(使用OpenCV的warpPerspective)的同形矩阵,如下所示: [ 2.59952324e+00,3.33170976e-01,-2.17014066e+02,8.64133587e-01,1.82580111e+00,-3.20053715e+02,2.78910149e-03,4.47911310e-05,1.00000000e+00]
  • 主图像(这里的运行轨迹映像)
  • 重叠图像(这里的可口可乐图像)

有可能吗?我读了很多书,并开始了OpenGL基础教程,但能不能从我所拥有的开始呢?OpenGL实现会更快吗,比如说,大约在10 be左右?

我现在在这里玩这个教程:http://ogldev.atspace.co.uk/www/tutorial12/tutorial12.html,我在正确的方向上走吗?总OpenGL新手在这里,请承担。谢谢。

EN

Stack Overflow用户

发布于 2017-02-08 02:46:19

在尝试了这里和其他地方提出的许多解决方案之后,我以编写了一个片段着色器来复制'warpPerspective‘所做的事情来解决这个问题。

片段着色器代码如下所示:

代码语言:javascript
运行
复制
varying highp vec2 textureCoordinate;

uniform sampler2D inputImageTexture;

// NOTE: you will need to pass the INVERSE of the homography matrix, as well as 
// the width and height of your image as uniforms!
uniform highp mat3 inverseHomographyMatrix;
uniform highp float width;
uniform highp float height;

void main()
{
   // Texture coordinates will run [0,1],[0,1];
   // Convert to "real world" coordinates
   highp vec3 frameCoordinate = vec3(textureCoordinate.x * width, textureCoordinate.y * height, 1.0);

   // Determine what 'z' is
   highp vec3 m = inverseHomographyMatrix[2] * frameCoordinate;
   highp float zed = 1.0 / (m.x + m.y + m.z);
   frameCoordinate = frameCoordinate * zed;

   // Determine translated x and y coordinates
   highp float xTrans = inverseHomographyMatrix[0][0] * frameCoordinate.x + inverseHomographyMatrix[0][1] * frameCoordinate.y + inverseHomographyMatrix[0][2] * frameCoordinate.z;
   highp float yTrans = inverseHomographyMatrix[1][0] * frameCoordinate.x + inverseHomographyMatrix[1][1] * frameCoordinate.y + inverseHomographyMatrix[1][2] * frameCoordinate.z;

   // Normalize back to [0,1],[0,1] space
   highp vec2 coords = vec2(xTrans / width, yTrans / height);

   // Sample the texture if we're mapping within the image, otherwise set color to black
   if (coords.x >= 0.0 && coords.x <= 1.0 && coords.y >= 0.0 && coords.y <= 1.0) {
       gl_FragColor = texture2D(inputImageTexture, coords);
   } else {
       gl_FragColor = vec4(0.0,0.0,0.0,0.0);
   }
}

请注意,我们在这里传递的同形矩阵是逆homography矩阵!,您必须将要传递到“Warp透视图”中的同形矩阵反转,否则这段代码将无法工作。

顶点着色器除了通过坐标之外什么也不做:

代码语言:javascript
运行
复制
// Vertex shader
attribute vec4 position;
attribute vec4 inputTextureCoordinate;

varying vec2 textureCoordinate;

void main() {
   // Nothing happens in the vertex shader
   textureCoordinate = inputTextureCoordinate.xy;
   gl_Position = position;
}

传递未更改的纹理坐标和位置坐标(即textureCoordinates = (0,0),(0,1),(1,0),(1,1)和positionCoordinates = (-1,-1),(-1,1),(-1,1),(1,1),(1,1),这应该有效!

票数 5
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33018652

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档