我正在尝试在 OpenGL 着色器中实现 OpenCV 鱼眼相机模型以提高渲染速度,我正在关注 OpenCV 的文档。但是我得到的结果仍然有些失真。
这是我从我的 OpenGL 着色器得到的结果(不用担心比例):
这是我从 OpenCV initUndistortRectifyMap 得到的结果,并使用完全相同的相机矩阵重新映射。
很容易发现shader的结果还是有点变形,如果我继续做透视变换变形会更明显,下面是OpenGL shader透视结果和OpenCV warpPerspective结果的对比
我的着色器中的透视变换通过将 OpenCV 无失真图像传递给着色器证明是正确的。所以问题应该出在我的着色器的不失真部分,这是我的着色器的不失真部分:
void main() {
float i = textureCoords.x * windowWidth;
float j = textureCoords.y * windowHeight;
float xc = (i - cameraMatrix[0][2]) / cameraMatrix[0][0];
float yc = (j - cameraMatrix[1][2]) / cameraMatrix[1][1];
float r = sqrt(xc*xc + yc*yc);
float degree = atan(r);
float rd = degree + distortVector[0]*pow(degree,3) + distortVector[1]*pow(degree,5) + distortVector[2]*pow(degree,7) + distortVector[3]*pow(degree,9);
float scale = rd / r;
float x = xc * scale;
float y = yc * scale;
float u = cameraMatrix[0][0]*(x+0.1*y) + cameraMatrix[0][2];
float v = cameraMatrix[1][1]*y + cameraMatrix[1][2];
vec2 coords = vec2(u/textureWidth, v/textureHeight);
if (coords.x>=0.0 && coords.x<=1.0 && coords.y>=0.0 && coords.y<=1.0) {
gl_FragColor = texture(inputTexture, coords);
} else {
gl_FragColor = vec4(0.0,0.0,0.0,0.0);
}
}
有什么想法吗?请让我知道