简体   繁体   English

树莓派:校准相机和图像不失真

[英]Raspberry pi:calibrate camera and undistortion of image

I want to remove fisheye effect from image, so I need camera matrix and distortion cofficient. 我想从图像中去除鱼眼效果,因此需要相机矩阵和失真系数。 That's why I created cal.py file. 这就是为什么我创建了cal.py文件。

When I run that file it give me camera matrix and distortion efficient which I had put in undist.py file to undistort the image. 当我运行该文件时,它会为我提供相机矩阵和有效的失真效果,这些效果已放入undist.py文件中,以使图像不失真。 now every time i got the same RMS,camera matrix,distortion coefficient. 现在每次我得到相同的RMS,相机矩阵,失真系数。

but when i put this parameters in undist.py file that is give me blank image.. how to solve this problem?PLz help me...Thanks in advance.i have capture 8 image and one of the sample image of chessboard pattern. 但是当我将此参数放在undist.py文件中时,该文件会给我空白图像。如何解决此问题?PLz帮助我...谢谢。我已经捕获了8张图像和棋盘图案的示例图像之一。

I will do an answer to summarize all the problems solved in the comments (this way the future readers do not have to read all of them). 我将做一个答案,以总结评论中解决的所有问题(这样,将来的读者就不必阅读所有问题)。

  1. Follow the tutorial from here to have a better understanding of what you should do and which functions to use. 此处开始阅读本教程,以更好地了解您应该做什么以及使用哪些功能。

  2. Check that your camera matrix have the correct form: 检查您的相机矩阵格式是否正确:

     [ f_x s c_x 0 f_y c_y 0 0 1 ] 

    the s is a skew value that I think in opencv it always gives 0. s是一个偏斜值,我认为在opencv中它始终为0。

  3. Make sure in every step that the images are loading correctly and that they are doing exactly what is intended (use imshow function to debug) 确保每一步中的图像都正确加载,并且确实按照预期运行(使用imshow函数进行调试)

  4. This part of your code 这部分代码

     newcamera, roi = cv2.getOptimalNewCameraMatrix(K, d, (w,h), 0) newimg = cv2.undistort(img, K, d, None, newcamera) 

is faulty. 有毛病。 Why, well from the opencv documentation we have that: 为什么呢,从opencv 文档中我们知道:

cv2.getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, alpha[, newImgSize[, centerPrincipalPoint]]) → retval, validPixROI

and

The function computes and returns the optimal new camera matrix based on the free scaling parameter. 该函数根据自由缩放参数计算并返回最佳的新相机矩阵。 By varying this parameter, you may retrieve only sensible pixels alpha=0 , keep all the original image pixels if there is valuable information in the corners alpha=1 , or get something in between. 通过更改此参数,您可以只检索有意义的像素alpha = 0,如果在拐角处alpha-1 = 1中有有价值的信息,则保留所有原始图像像素,或者在两者之间获取一些信息。 When alpha>0 , the undistortion result is likely to have some black pixels corresponding to “virtual” pixels outside of the captured distorted image. 当alpha> 0时,不失真结果可能会具有一些黑色像素,这些像素与捕获的失真图像外部的“虚拟”像素相对应。 The original camera matrix, distortion coefficients, the computed new camera matrix, and newImageSize should be passed to initUndistortRectifyMap() to produce the maps for remap() . 原始摄影机矩阵,失真系数,计算出的新摄影机矩阵和newImageSize应该传递给initUndistortRectifyMap()以生成remap()的映射。

This means that the new camera matrix also gives you a new valid size!! 这意味着新的相机矩阵也为您提供了新的有效尺寸! To be more specifically: 更具体地说:

the computed new camera matrix, and newImageSize should be passed to initUndistortRectifyMap() 计算的新相机矩阵,并将newImageSize传递给initUndistortRectifyMap()

But the undistort function does the initUndistortRectifyMap automatically.... and it doesn't have a way to pass the newImageSize from this function. 但是undistort函数会自动执行initUndistortRectifyMap ....,并且它没有从该函数传递newImageSize方法。 So basically you have 2 options. 所以基本上您有2个选择。

  1. Use your newcamera matrix, but instead of doing undistort , you should do everything manually.... this means that you must do initUndistortRectifyMap and remap using the new sizes and new camera matrix. 使用您的newcamera矩阵,但是应该手动进行所有操作,而不是执行undistort 。...这意味着您必须执行initUndistortRectifyMap并使用新的尺寸和新的摄像机矩阵进行remap
  2. Use the original camera matrix obtained in the calibrateCamera function. 使用在calibrateCamera函数中获得的原始相机矩阵。 This way it will not have this zoom effect, but you may have some extra black pixels representing the areas that you may not see due to rectification. 这样,它将不会具有这种缩放效果,但是您可能会有一些额外的黑色像素,这些像素代表由于校正而可能看不到的区域。

If not, it will always give you this zoom effect, because it will not show the invalid pixels (black pixels) of the undistorted areas. 如果没有,它将始终为您提供这种缩放效果,因为它将不会显示未失真区域的无效像素(黑色像素)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM