首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >OpenCV鱼眼校准切割了太多的结果图像

OpenCV鱼眼校准切割了太多的结果图像
EN

Stack Overflow用户
提问于 2015-12-16 15:47:56
回答 5查看 20.6K关注 0票数 11

我用OpenCV校准用鱼眼镜头拍摄的图像.

我所使用的功能如下:

  • findChessboardCorners(...);找出刻度图案的角点。
  • cornerSubPix(...);来细化找到的角点。
  • fisheye::calibrate(...);对摄像机矩阵和畸变系数进行标定。
  • fisheye::undistortImage(...);使用从校准中获得的摄像机信息来对图像进行反失真。

虽然得到的图像看起来确实不错(直线等等),但我的问题是,函数切断了图像的太多部分。

这是一个真正的问题,因为我用的是四台相机,它们之间有90度的距离,当很多边被切断时,它们之间就没有重叠的区域,因为我要缝制图像。

我研究了使用fisheye::estimateNewCameraMatrixForUndistortRectify(...),但是我无法得到好的结果,因为我不知道我应该把什么作为R输入,因为fisheye::calibrate的旋转矢量输出是3xN (其中N是校准图像的数量),fisheye::estimateNewCameraMatrixForUndistortRectify需要一个1x3或3x3。

下面的图像显示了我不失真结果的图像,以及我理想中想要的结果的例子。

不失真:

通缉结果实例:

EN

回答 5

Stack Overflow用户

发布于 2017-05-16 19:02:03

我想我遇到了一个类似的问题,在getOptimalNewCameraMatrix中寻找鱼眼的"alpha“结。

原始拍摄:

我用cv2.fisheye.calibrate校准它,得到K和D参数

代码语言:javascript
运行
复制
K = [[ 329.75951163    0.          422.36510555]
 [   0.          329.84897388  266.45855056]
 [   0.            0.            1.        ]]

D = [[ 0.04004325]
 [ 0.00112638]
 [ 0.01004722]
 [-0.00593285]]

这就是我想要的

代码语言:javascript
运行
复制
map1, map2 = cv2.fisheye.initUndistortRectifyMap(K, d, np.eye(3), k, (800,600), cv2.CV_16SC2)
nemImg = cv2.remap( img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)

我觉得它切得太多了。我想看看整个魔方

我用

代码语言:javascript
运行
复制
nk = k.copy()
nk[0,0]=k[0,0]/2
nk[1,1]=k[1,1]/2
# Just by scaling the matrix coefficients!

map1, map2 = cv2.fisheye.initUndistortRectifyMap(k, d, np.eye(3), nk, (800,600), cv2.CV_16SC2)  # Pass k in 1st parameter, nk in 4th parameter
nemImg = cv2.remap( img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)

塔达!

票数 6
EN

Stack Overflow用户

发布于 2018-11-27 12:59:23

正如保罗·伯克 这里所提到的

鱼眼投影不是“扭曲的”图像,也不是“扭曲”的过程。鱼眼和其他投影一样,是将3D世界映射到2D平面的多种方法之一,它与包括矩形透视投影在内的其他投影一样或多或少是“扭曲”的。

要获得不需要图像裁剪的投影(并且您的相机有~180 ),您可以使用这样的方法将鱼眼图像投影到一个正方形中:

源代码:

代码语言:javascript
运行
复制
#include <iostream>
#include <sstream>
#include <time.h>
#include <stdio.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/highgui/highgui.hpp>

// - compile with:
// g++ -ggdb `pkg-config --cflags --libs opencv` fist2rect.cpp -o fist2rect
// - execute:
// fist2rect input.jpg output.jpg

 using namespace std;
 using namespace cv;
 #define PI 3.1415926536

 Point2f getInputPoint(int x, int y,int srcwidth, int srcheight)
 {
    Point2f pfish;
    float theta,phi,r, r2;
    Point3f psph;
    float FOV =(float)PI/180 * 180;
    float FOV2 = (float)PI/180 * 180;
    float width = srcwidth;
    float height = srcheight;

    // Polar angles
    theta = PI * (x / width - 0.5); // -pi/2 to pi/2
    phi = PI * (y / height - 0.5);  // -pi/2 to pi/2

    // Vector in 3D space
    psph.x = cos(phi) * sin(theta);
    psph.y = cos(phi) * cos(theta);
    psph.z = sin(phi) * cos(theta);

    // Calculate fisheye angle and radius
    theta = atan2(psph.z,psph.x);
    phi = atan2(sqrt(psph.x*psph.x+psph.z*psph.z),psph.y);

    r = width * phi / FOV;
    r2 = height * phi / FOV2;

    // Pixel in fisheye space
    pfish.x = 0.5 * width + r * cos(theta);
    pfish.y = 0.5 * height + r2 * sin(theta);
    return pfish;
}
int main(int argc, char **argv)
{
    if(argc< 3)
        return 0;
    Mat orignalImage = imread(argv[1]);
    if(orignalImage.empty())
    {
        cout<<"Empty image\n";
        return 0;
    }
    Mat outImage(orignalImage.rows,orignalImage.cols,CV_8UC3);

    namedWindow("result",CV_WINDOW_NORMAL);

    for(int i=0; i<outImage.cols; i++)
    {
        for(int j=0; j<outImage.rows; j++)
        {

            Point2f inP =  getInputPoint(i,j,orignalImage.cols,orignalImage.rows);
            Point inP2((int)inP.x,(int)inP.y);

            if(inP2.x >= orignalImage.cols || inP2.y >= orignalImage.rows)
                continue;

            if(inP2.x < 0 || inP2.y < 0)
                continue;
            Vec3b color = orignalImage.at<Vec3b>(inP2);
            outImage.at<Vec3b>(Point(i,j)) = color;

        }
    }

    imwrite(argv[2],outImage);

}
票数 6
EN

Stack Overflow用户

发布于 2020-03-04 08:15:00

您需要使用fisheye::estimateNewCameraMatrixForUndistortRectifyR=np.eye(3) (单位矩阵)和balance=1来获取所有像素:

代码语言:javascript
运行
复制
new_K = cv2.fisheye.estimateNewCameraMatrixForUndistortRectify(K, D, dim, np.eye(3), balance=balance)
map1, map2 = cv2.fisheye.initUndistortRectifyMap(scaled_K, D, np.eye(3), new_K, dim, cv2.CV_32FC1)
# and then remap:
undistorted_img = cv2.remap(img, map1, map2, interpolation=cv2.INTER_LINEAR, borderMode=cv2.BORDER_CONSTANT)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34316306

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档