使用 Chessboard 进行 OpenCV 转换

我刚刚开始尝试使用 OpenCV.我有一个带有静态位置的 LCD 设置,我想从图像中提取屏幕上显示的内容.我见过用于校准相机的棋盘图案,但它似乎是用来使图像不失真的,这不是我想要做的.

I have only just started experimenting with OpenCV a little bit. I have a setup of an LCD with a static position, and I'd like to extract what is being displayed on the screen from the image. I've seen the chessboard pattern used for calibrating a camera, but it seems like that is used to undistort the image, which isn't totally what I want to do.

我想我会在 LCD 上显示棋盘,然后找出将 LCD 图像直接在头顶上并裁剪成棋盘的理想视图所需的转换.然后我会存储转换,更改 LCD 显示的内容,拍照,执行相同的转换,并获得当前显示内容的理想视图.

I was thinking I'd display the chessboard on the LCD and then figure out the transformations needed to convert the image of the LCD into the ideal view of the chessboard directly overhead and cropped. Then I would store the transformations, change what the LCD is displaying, take a picture, perform the same transformations, and get the ideal view of what was now being displayed.

我想知道这听起来是不是个好主意?有没有更简单的方法来实现我想要做的事情?以及我应该使用哪些函数来找出转换、执行转换、存储转换(也许只是将转换矩阵保存在内存中或将它们写入文件)等方面的任何提示?

I'm wondering if that sounds like a good idea? Is there a simpler way to achieve what I'm trying to do? And any tips on the functions I should be using to figure out the transformations, perform them, store them (maybe just keep the transform matrices in memory or write them to file), etc?

推荐答案

我不确定我是否正确理解了您尝试做的所有事情,但请耐心等待.

I'm not sure I understood correctly everything you are trying to do, but bear with me.

有些相机的镜头会导致图像稍微失真,为此 OpenCV 提供了帮助 相机校准过程.

Some cameras have lenses that cause a little distortion to the image, and for this purpose OpenCV offers methods to aid in the camera calibration process.

实际上,如果您想编写一个可以自动校正图像失真的应用程序,首先,您需要发现需要哪些神奇的值用于撤消此效果.这些值来自正确的校准程序.

Practically speaking, if you want to write an application that will automatically correct the distortion in the image, first, you need to discover what are the magical values that need to be used to undo this effect. These values come from a proper calibration procedure.

棋盘图像 与 校准相机的应用程序.因此,在您拥有相机设备拍摄的棋盘图像后,将此图像传递给校准应用程序.该应用程序将识别正方形的角并计算失真值并返回您需要用来抵消失真效果的神奇值.此时,您对 calibrateCamera 返回的 2 个变量感兴趣():它们是cameraMatrixdistCoeffs.打印出来,把数据写在一张纸上.

The chessboard image is used together with an application to calibrate the camera. So, after you have an image of the chessboard taken by the camera device, pass this image to the calibration app. The app will identify the corners of the squares and compute the values of the distortion and return the magical values you need to use to counter the distortion effect. At this point, you are interested in 2 variables returned by calibrateCamera(): they are cameraMatrix and distCoeffs. Print them, and write the data on a piece of paper.

最后,您正在开发的系统需要有一个函数/方法来使图像不失真,这两个变量将在函数内部进行硬编码,然后调用 cv::undistort()(如果你使用的是 OpenCV 的 C++ API):

At the end, the system you are developing needs to have a function/method to undistort the image, where these 2 variables will be hard coded inside the function, followed by a call to cv::undistort() (if you are using the C++ API of OpenCV):

cv::Mat undistorted;
cv::undistort(image, undistorted, cameraMatrix, distCoeffs);

就是这样.

自动检测旋转可能有点棘手,但首先要做的是找到您感兴趣的对象的坐标.但如果相机处于固定位置,这将很容易.

Detecting rotation automatically might be a bit tricky, but the first thing to do is find the coordinates of the object you are interested in. But if the camera is in a fixed position, this is going to be easy.

有关 OpenCV 的透视变化和旋转的更多信息,我建议查看以下其他问题:

For more info on perspective change and rotation with OpenCV, I suggest taking a look at these other questions:

在一组 cv::Point 上执行 cv::warpPerspective 以进行伪校正

仿射变换、简单旋转和缩放或其他完全不同的东西?

使用 cv::warpAffine 偏移目标图像旋转 cv::Mat

相关文章