CV_CALIB_CB_ADAPTIVE_THRESH | CV_CALIB_CB_FAST_CHECK | CV_CALIB_CB_NORMALIZE_IMAGE在opencv4.55中怎么改写
时间: 2023-08-30 18:11:03 浏览: 194
在 OpenCV 4.5.5 中,这些标志被重命名为:
- `cv::CALIB_CB_ADAPTIVE_THRESH`
- `cv::CALIB_CB_FAST_CHECK`
- `cv::CALIB_CB_NORMALIZE_IMAGE`
因此,您可以使用以下代码来设置这些标志:
```
cv::Mat image;
int board_width = 9;
int board_height = 6;
int flags = cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_FAST_CHECK | cv::CALIB_CB_NORMALIZE_IMAGE;
cv::Size board_size(board_width, board_height);
std::vector<cv::Point2f> corners;
bool found = cv::findChessboardCorners(image, board_size, corners, flags);
```
注意,这些标志的值没有发生变化,只是名称发生了变化。因此,您可以像以前一样使用它们来设置相应的标志。
相关问题
SOLVEPNP_PERSPECTIVE_FISHEYE 使用例程 C++
以下是使用SOLVEPNP_PERSPECTIVE_FISHEYE的简单例程。
```c++
#include <iostream>
#include <opencv2/opencv.hpp>
int main(int argc, char** argv)
{
// 读取图像
cv::Mat img = cv::imread("test.jpg");
if (img.empty()) {
std::cerr << "Failed to read image file." << std::endl;
return -1;
}
// 检测角点
cv::Size patternSize(8, 6);
std::vector<cv::Point2f> corners;
bool found = cv::findChessboardCorners(img, patternSize, corners, cv::CALIB_CB_ADAPTIVE_THRESH | cv::CALIB_CB_FAST_CHECK | cv::CALIB_CB_NORMALIZE_IMAGE);
if (!found) {
std::cerr << "Failed to find chessboard corners." << std::endl;
return -1;
}
// 亚像素级别精度优化
cv::Mat gray;
cv::cvtColor(img, gray, cv::COLOR_BGR2GRAY);
cv::cornerSubPix(gray, corners, cv::Size(11, 11), cv::Size(-1, -1), cv::TermCriteria(cv::TermCriteria::EPS + cv::TermCriteria::MAX_ITER, 30, 0.1));
// 相机内参矩阵
cv::Mat K = cv::Mat::eye(3, 3, CV_64F);
K.at<double>(0, 0) = 1000.0; // fx
K.at<double>(1, 1) = 1000.0; // fy
K.at<double>(0, 2) = img.cols / 2.0; // cx
K.at<double>(1, 2) = img.rows / 2.0; // cy
// 畸变参数
cv::Mat D = cv::Mat::zeros(4, 1, CV_64F);
// 估计相机位姿
cv::Mat rvec, tvec;
cv::solvePnP(corners, corners3D, K, D, rvec, tvec, false, cv::SOLVEPNP_PERSPECTIVE_FISHEYE);
// 输出结果
std::cout << "rvec: " << rvec << std::endl;
std::cout << "tvec: " << tvec << std::endl;
return 0;
}
```
说明:
1. 读取图像并检测棋盘格角点。
2. 对角点进行亚像素级别精度优化。
3. 定义相机内参矩阵和畸变参数,这里使用固定值。
4. 调用 `cv::solvePnP` 函数估计相机位姿。注意最后一个参数设置为 `SOLVEPNP_PERSPECTIVE_FISHEYE`。
5. 输出旋转向量和平移向量。
需要注意的是,在使用 `cv::solvePnP` 函数时,输入的角点是经过亚像素级别精度优化的,输出的旋转向量和平移向量是相机坐标系下的,需要经过变换才能得到世界坐标系下的坐标。
images= glob.glob('./image/*.jpg') # print(images) for fname in images: img = cv2.imread(fname) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Find the chess board corners # If desired number of corners are found in the image then ret = true ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH + cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE) """ If desired number of corner are detected, we refine the pixel coordinates and display them on the images of checker board """ if ret == True: objpoints.append(objp) # refining pixel coordinates for given 2d points. corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria) imgpoints.append(corners2) # Draw and display the corners img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2, ret) # cv2.imshow('img',img) # cv2.waitKey(0) cv2.destroyAllWindows() # h, w = np.array(img).shape[:2] h, w = img.shape[:2] """ Performing camera calibration by passing the value of known 3D points (objpoints) and corresponding pixel coordinates of the detected corners (imgpoints) """ ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None) print("Camera matrix : \n") print(mtx) print("dist : \n") print(dist) print("rvecs : \n") print(rvecs) print("tvecs : \n") print(tvecs)这段代码有错误,请帮我找出来
这段代码存在一些错误,可能会导致出现你之前提到的错误。我列出了一些可能的问题:
1. 代码中没有定义CHECKERBOARD变量,需要在代码中加入如下语句定义CHECKERBOARD:
CHECKERBOARD = (6, 9)
2. 代码中没有定义objp变量,需要在代码中加入如下语句定义objp:
objp = np.zeros((1, CHECKERBOARD[0] * CHECKERBOARD[1], 3), np.float32)
objp[0, :, :2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
3. 可能存在一些图像无法找到所有的角点,导致ret为False。可以在代码中加入如下语句,跳过这些无法找到所有角点的图像:
if ret == False:
continue
4. 在调用calibrateCamera函数时,输入的第三个参数应该是图像的大小,而不是灰度图像的大小。可以将代码中的gray.shape[::-1]修改为img.shape[:2]。
修改后的代码如下所示:
```python
import numpy as np
import cv2
import glob
CHECKERBOARD = (6, 9)
objp = np.zeros((1, CHECKERBOARD[0] * CHECKERBOARD[1], 3), np.float32)
objp[0, :, :2] = np.mgrid[0:CHECKERBOARD[0], 0:CHECKERBOARD[1]].T.reshape(-1, 2)
objpoints = []
imgpoints = []
images = glob.glob('./image/*.jpg')
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, CHECKERBOARD, cv2.CALIB_CB_ADAPTIVE_THRESH +
cv2.CALIB_CB_FAST_CHECK + cv2.CALIB_CB_NORMALIZE_IMAGE)
if ret == True:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
imgpoints.append(corners2)
img = cv2.drawChessboardCorners(img, CHECKERBOARD, corners2, ret)
h, w = img.shape[:2]
ret, mtx, dist, rvecs, tvecs = cv2.calibrateCamera(objpoints, imgpoints, (w, h), None, None)
print("Camera matrix : \n")
print(mtx)
print("dist : \n")
print(dist)
print("rvecs : \n")
print(rvecs)
print("tvecs : \n")
print(tvecs)
```
希望以上修改可以帮到你,如果还有其他问题,可以随时问我。
阅读全文