OpenCV 透视图

时间:2023-02-28
本文介绍了OpenCV 透视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,每当我使用 OpenCV 的 warpPerspective() 函数时,最终的变形图像都不包含原始图像中的所有内容.图像的左侧部分似乎被切断了.我认为发生这种情况的原因是扭曲的图像是在画布的最左侧位置为 warpPerspective() 创建的.有没有办法解决这个问题?谢谢

For some reason whenever I use OpenCV's warpPerspective() function, the final warped image does not contain everything in the original image. The left part of the image seems to get cut off. I think the reason why this is happening is because the warped image is created at the leftmost position of the canvas for the warpPerspective(). Is there some way to fix this? Thanks

推荐答案

出现问题是因为单应性将图像的一部分映射到负的 x,y 值,这些值在图像区域之外,因此无法绘制.我们希望做的是将扭曲的输出偏移一定数量的像素,以将整个扭曲的图像分流"到正坐标(因此在图像区域内).

The problem occurs because the homography maps part of the image to negative x,y values which are outside the image area so cannot be plotted. what we wish to do is to offset the warped output by some number of pixels to 'shunt' the entire warped image into positive coordinates(and hence inside the image area).

可以使用矩阵乘法组合单应性(这就是它们如此强大的原因).如果 A 和 B 是单应性,则 AB 表示先应用 B 再应用 A 的单应性.

Homographies can be combined using matrix multiplication (which is why they are so powerful). If A and B are homographies, then AB represents the homography which applies B first, and then A.

因此,我们需要做的就是通过一些偏移为翻译创建单应矩阵,然后将其与原始单应矩阵相乘

Because of this all we need to do to offset the output is create the homography matrix for a translation by some offset, and then pre-multiply that by our original homography matrix

二维单应矩阵如下所示:

A 2D homography matrix looks like this :

[R11,R12,T1]
[R21,R22,T2]
[ P , P , 1]

其中 R 表示旋转矩阵,T 表示平移,P 表示透视扭曲.所以一个纯粹的翻译单应性看起来像这样:

where R represents a rotation matrix, T represents a translation, and P represents a perspective warp. And so a purely translational homography looks like this:

[ 1 , 0 , x_offset]
[ 0 , 1 , y_offset]
[ 0 , 0 ,    1    ]

因此,只需将您的单应性乘以类似于上述的矩阵,您的输出图像就会发生偏移.

So just premultiply your homography by a matrix similar to the above, and your output image will be offset.

(确保使用矩阵乘法,而不是元素乘法!)

(Make sure you use matrix multiplication, not element wise multiplication!)

这篇关于OpenCV 透视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:通过套接字发送 C++ OpenCV 图像 下一篇:如何在 OpenCV 中将 Float Mat 写入文件

相关文章

最新文章