我正在尝试在 OpenGL 片段着色器中实现 OpenCV warpPerspective 函数,以实现 360 度环绕视图的使用。我在这里找到了相关答案,片段代码如下:
mat3 inverseProjectMatrix2 = mat3(
-2.69544232e+00, 7.12768849e+00, -2.42274679e+03,
4.16355568e-01, 3.79979565e+00, -2.70127515e+03,
4.51824274e-04, 1.40540181e-02, -8.16333507e+00
);
float c11 = inverseProjectMatrix2[0][0];
float c12 = inverseProjectMatrix2[0][1];
float c13 = inverseProjectMatrix2[0][2];
float c21 = inverseProjectMatrix2[1][0];
float c22 = inverseProjectMatrix2[1][1];
float c23 = inverseProjectMatrix2[1][2];
float c31 = inverseProjectMatrix2[2][0];
float c32 = inverseProjectMatrix2[2][1];
float c33 = inverseProjectMatrix2[2][2];
void main() {
vec3 frameCoords = vec3(gl_FragCoord.x, gl_FragCoord.y, 1.0);
vec3 m = inverseProjectMatrix2[2] * frameCoords;
float zed = 1.0 / (m.x + m.y + m.z);
frameCoords = frameCoords * zed;
float xTrans = c11 * frameCoords.x + c12 * frameCoords.y + c13 * frameCoords.z;
float yTrans = c21 * frameCoords.x + c22 * frameCoords.y + c23 * frameCoords.z;
vec2 coords = vec2(xTrans / width, yTrans / height);
gl_FragColor = texture(inputImageTexture, coords);
}
我使用 stb 在 OpenGL 中加载纹理,并且启用了如下所示的纹理翻转:
stbi_set_flip_vertically_on_load(true);
我在OpenCV中测试过透视矩阵,应该是正确的
这是原始图像(我将其作为纹理加载到 OpenGL 中): 这是我期待的结果图像(使用 OpenCV warpPerspective 函数生成)
这是我得到的错误结果图像。
计算看起来很简单,我不知道我哪里做错了,请帮忙
抱歉我没注意到你提到了this。 问题是在 OpenGL 纹理坐标范围从 0 到 1。当使用逆矩阵扭曲图像时,您应该使用像素坐标,这是纹理图像的实际大小。所以 frameCoords 应该是这样的:
vec3 frameCoords = vec3(gl_FragCoord.x * width, gl_FragCoord.y * height, 1.0);