Trying to implement OpenCV warpPerspective function in OpenGL fragment shader

I’m trying to implement OpenCV warpPerspective function in OpenGL fragment shader for 360 surround view usage. I found a relevant answer here and the fragment code is below:

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);

I use stb to load texture in OpenGL, and I have enabled the texture flip like below:


I have tested the perspective matrix in OpenCV, it should be correct.

Here is the original image (I load it as texture in OpenGL):

Here is the result image I am expecting (generated by using OpenCV warpPerspective function)

And here is the wrong result image I get.

The calculation looks easy, I don’t know where I did wrong, please help

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.