我在PythonandC++中使用OpenCV做了图像扭曲,在我选择的角落看到了可口可乐的标志。
使用下列图像:
这是:
我需要做到这一点,但在OpenGL。我要:
有可能吗?我读了很多书,并开始了OpenGL基础教程,但能不能从我所拥有的开始呢?OpenGL实现会更快吗,比如说,大约在10 be左右?
我现在在这里玩这个教程:http://ogldev.atspace.co.uk/www/tutorial12/tutorial12.html,我在正确的方向上走吗?总OpenGL新手在这里,请承担。谢谢。
发布于 2017-02-08 02:46:19
在尝试了这里和其他地方提出的许多解决方案之后,我以编写了一个片段着色器来复制'warpPerspective‘所做的事情来解决这个问题。
片段着色器代码如下所示:
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透视图”中的同形矩阵反转,否则这段代码将无法工作。
顶点着色器除了通过坐标之外什么也不做:
// 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),这应该有效!
https://stackoverflow.com/questions/33018652
复制相似问题