gpt4 book ai didi

c++ - 鱼眼图像不失真的正确方法

转载 作者:搜寻专家 更新时间:2023-10-31 01:32:58 25 4
gpt4 key购买 nike

我已经完成了一些示例程序,可以消除棋盘上的镜头失真,例如鱼眼图像,它工作正常,这是屏幕截图

enter image description here

接下来,我想使用鱼眼棋盘图案(上图右图)来消除相同的镜头畸变,但从真实图像中去除并没有成功——曲率仍然保留在未畸变的图像上,因此我得到了这个

enter image description here

代码

void getObjectPoints(cv::Size, std::vector<std::vector<cv::Point3f>>&);

bool getImagePoints(cv::Mat&, cv::Size&, std::vector<std::vector<cv::Point2f>>&);

void runCalibration(cv::Mat& image, cv::Matx33d&, cv::Vec4d&);

cv::Mat removeFisheyeLensDist(cv::Mat&, cv::Matx33d&, cv::Vec4d&);

// ... definitions
void getObjectPoints(cv::Size patternSize, std::vector<std::vector<cv::Point3f>>& objectPoints)
{
const float squareSize = 0.0015f;
std::vector<cv::Point3f> knownBoardPositions;
for (int i = 0; i < patternSize.height; ++i)
{
for (int j = 0; j < patternSize.width; ++j)
{
knownBoardPositions.push_back(cv::Point3f(j*squareSize, i*squareSize, 0.0f));
}
}
if (knownBoardPositions.size() > 0)
objectPoints.push_back(knownBoardPositions);
}

bool getImagePoints(cv::Mat& image, cv::Size& patternSize, std::vector<std::vector<cv::Point2f>>& imagePoints)
{
bool patternFound = false;
while (!patternFound)
{
std::vector<cv::Point2f> corners;
for (int i = 7; i <= 30; ++i)
{
int w = i;
int h = i - 2;

patternFound = cv::findChessboardCorners(image, cv::Size(w, h), corners,
cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_NORMALIZE_IMAGE);
if (patternFound)
{
patternSize.width = w;
patternSize.height = h;
imagePoints.push_back(corners);
break;
}
}
}

return patternFound;
}

void runCalibration(cv::Mat& image, cv::Matx33d& K, cv::Vec4d& D)
{
std::vector< std::vector<cv::Point2f> > imagePoints;
std::vector< std::vector<cv::Point3f> > objectPoints;
cv::Size patternSize;
bool patternFound = getImagePoints(image, patternSize, imagePoints);

if (patternFound)
{
getObjectPoints(patternSize, objectPoints);

std::vector<cv::Vec3d> rvecs;
std::vector<cv::Vec3d> tvecs;
cv::fisheye::calibrate(
objectPoints,
imagePoints,
image.size(),
K,
D,
rvecs,
tvecs,
cv::fisheye::CALIB_FIX_SKEW | cv::fisheye::CALIB_RECOMPUTE_EXTRINSIC
| cv::fisheye::CALIB_FIX_K1 | cv::fisheye::CALIB_FIX_K2
| cv::fisheye::CALIB_FIX_K3 | cv::fisheye::CALIB_FIX_K4
// cv::TermCriteria(3, 20, 1e-6)
);
}
}

cv::Mat removeFisheyeLensDist(cv::Mat& distorted, cv::Matx33d& K, cv::Vec4d& D)
{
cv::Mat undistorted;
cv::Matx33d newK = K;
cv::fisheye::undistortImage(distorted, undistorted, K, D, newK);
return undistorted;
}

int main(int argc, char* argv[])
{
cv::Mat chessBoardPattern = //..
cv::Mat distortedImage = //...
cv::imshow("distorted", distortedImage);

cv::Matx33d K; cv::Vec4d D;
runCalibration(chessBoardPattern, K, D);
cv::Mat undistoredImage = removeFisheyeLensDist(distortedImage, K, D);
cv::imshow("undistored", undistoredImage);
cv::waitKey(0);
return 0;
}

因为我认为带有塔的图像具有与右侧棋盘非常相似的曲率,所以相同的模式应该适用于塔图像 ...

我在那里做错了什么?为什么它不修复塔图像的镜头失真?

最佳答案

不幸的是你的假设

if images have same curvatures thus the camera parameters should be approximately same and so I can undistort fisheye emage with a chessboard pattern

错了。即使是同一型号的相机,在焦距、镜头几何形状和位置等方面也会存在差异,需要单独进行校准。此外,在相机使用过程中,这些参数可能会因加热、振动和其他影响而发生变化(在实践中通常忽略这一点)。

要在不使用相机的情况下消除图像失真,您所能做的就是选择一些简单的鱼眼相机模型并尝试手动估计参数,尝试使直线看起来笔直(例如,使用带有所有参数 slider 的 GUI)。这可能很乏味,但我不知道有更好的选择。此外,一些图像编辑软件可能有相应的工具(如果我没记错的话 GIMP 有)

关于c++ - 鱼眼图像不失真的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42188989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com