using OpenCvSharp;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace demo
{
internal class Program
{
static void Main(string[] args)
{
List<string> paths = new List<string>()
{
"../../../images/lefts/",
"../../../images/rights/"
};
List<string> imagesList = new List<string>();
foreach (string path in paths)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fis = di.GetFiles("*.jpg");
Mat srcImage = new Mat();
foreach (FileInfo fi in fis)
{
imagesList.Add(fi.FullName);
}
}
TermCriteria criteria = new TermCriteria(CriteriaTypes.Eps | CriteriaTypes.MaxIter, 30, 0.001);
// 棋盘格内角点数量(行数和列数)
Size BoardSize = new Size(9, 6);
// 棋盘格尺寸(单位:米)
float squareSize = 0.025f;
Mat img = new Mat();
Mat gray = new Mat();
List<List<Point3f>> objpoints = new List<List<Point3f>>();
List<List<Point2f>> imgpoints = new List<List<Point2f>>();
for (int i = 0; i < imagesList.Count; i++)
{
img = new Mat(imagesList[i]);
if (!img.Empty())
{
Cv2.CvtColor(img, gray, ColorConversionCodes.BGR2GRAY);
var corners = new Mat<Point2f>();
bool found = Cv2.FindChessboardCorners(gray, BoardSize, corners);
if (found == true)
{
var objectPointsArray = Create3DChessboardCorners(BoardSize, squareSize).ToArray();
var imagePointsArray = corners.ToArray();
//var objectPoints = Mat<Point3f>.FromArray(objectPointsArray);
//var imagePoints = Mat<Point2f>.FromArray(imagePointsArray);
objpoints.Add(objectPointsArray.ToList());
imgpoints.Add(imagePointsArray.ToList());
}
}
}
var mtx = new double[,] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
var dist = new double[5];
Cv2.CalibrateCamera(objpoints, imgpoints, gray.Size(), mtx,
dist, out var rvecs, out var tvecs, CalibrationFlags.None);
List<Point3f> objPts = new List<Point3f>();
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 9; j++)
{
objPts.Add(new Point3f(j, i, 0));
}
}
// X、Y、Z三轴方向,占3格
var axis = new[]
{
new Point3f(3,0,0),
new Point3f(0,3,0),
new Point3f(0,0,-3),
};
foreach (string fname in imagesList)
{
img = Cv2.ImRead(fname);
Cv2.CvtColor(img, gray, ColorConversionCodes.BGR2GRAY);
Point2f[] corners;
bool found = Cv2.FindChessboardCorners(img, BoardSize, out corners);
if (found == true)
{
var corners2 = Cv2.CornerSubPix(gray, corners, new Size(11, 11), new Size(-1, -1), criteria);
var rvec = new double[] { };
var tvec = new double[] { };
Cv2.SolvePnP(objPts, corners2, mtx, dist, ref rvec, ref tvec);
Cv2.ProjectPoints(axis, rvec, tvec, mtx, dist, out var imgPts, out var jacobian);
img = draw(img, corners2, imgPts);
Cv2.ImShow("img", img);
if (Cv2.WaitKey(0) == 27)
break;
}
}
}
private static IEnumerable<Point3f> Create3DChessboardCorners(Size boardSize, float squareSize)
{
for (int y = 0; y < boardSize.Height; y++)
{
for (int x = 0; x < boardSize.Width; x++)
{
yield return new Point3f(x * squareSize, y * squareSize, 0);
}
}
}
static Mat draw(Mat img, Point2f[] corners, Point2f[] imgpts)
{
Point l0 = new Point((int)corners[0].X, (int)corners[0].Y);
Point l1 = new Point((int)imgpts[0].X, (int)imgpts[0].Y);
Point l2 = new Point((int)imgpts[1].X, (int)imgpts[1].Y);
Point l3 = new Point((int)imgpts[2].X, (int)imgpts[2].Y);
Cv2.Line(img, l0, l1, new Scalar(255, 0, 0), 5);
Cv2.Line(img, l0, l2, new Scalar(0, 255, 0), 5);
Cv2.Line(img, l0, l3, new Scalar(0, 0, 255), 5);
return img;
}
}
}
C++版本代码如下:
#include <opencv2/opencv.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/cvconfig.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <io.h>
#include <string>
#include <iostream>
#include <ctime>
using namespace cv;
using namespace std;
vector<Point3f> Create3DChessboardCorners(Size boardSize, float squareSize)
{
std::vector<cv::Point3f> objectPoints;
for (int y = 0; y < boardSize.height; y++)
{
for (int x = 0; x < boardSize.width; x++)
{
objectPoints.push_back(Point3f(x * squareSize, y * squareSize, 0));
}
}
return objectPoints;
}
Mat draw(Mat img, vector<Point2f> corners, vector<Point2f> imgpts)
{
Point l0((int)corners[0].x, (int)corners[0].y);
Point l1((int)imgpts[0].x, (int)imgpts[0].y);
Point l2((int)imgpts[1].x, (int)imgpts[1].y);
Point l3((int)imgpts[2].x, (int)imgpts[2].y);
line(img, l0, l1, Scalar(255, 0, 0), 5);
line(img, l0, l2, Scalar(0, 255, 0), 5);
line(img, l0, l3, Scalar(0, 0, 255), 5);
return img;
}
int main()
{
// 获取图片目录
std::string inPath1 = "..\\images\\lefts\\left*.jpg";
std::string inPath2 = "..\\images\\rights\\right*.jpg";
intptr_t handle1, handle2;
struct _finddata_t fileinfo1, fileinfo2;
handle1 = _findfirst(inPath1.c_str(), &fileinfo1);
handle2 = _findfirst(inPath2.c_str(), &fileinfo2);
vector<string> images;
do {
string Name = fileinfo1.name;
images.push_back("..\\images\\lefts\\" + Name);
} while (!_findnext(handle1, &fileinfo1));
do {
string Name = fileinfo2.name;
images.push_back("..\\images\\lefts\\" + Name);
} while (!_findnext(handle2, &fileinfo2));
TermCriteria criteria = TermCriteria(TermCriteria::EPS | TermCriteria::MAX_ITER, 30, 0.001);
// 棋盘格内角点数量(行数和列数)
cv::Size BoardSize(9, 6);
// 棋盘格尺寸(单位:米)
float squareSize = 0.025f;
// 存储棋盘格角点坐标的容器
std::vector<std::vector<cv::Point3f>> objpoints;
// 存储检测到的棋盘格角点坐标的容器
std::vector<std::vector<cv::Point2f>> imgpoints;
Mat gray;
for (int i = 0; i < images.size(); i++)
{
Mat img = imread(images[i]);
if (!img.empty())
{
cvtColor(img, gray, COLOR_BGR2GRAY);
// Find the chess board corners
std::vector<cv::Point2f> corners;;
bool found = findChessboardCorners(gray, BoardSize, corners);
if (found == true)
{
vector<Point3f> objectPointsArray = Create3DChessboardCorners(BoardSize, squareSize);
objpoints.push_back(objectPointsArray);
imgpoints.push_back(corners);
}
}
}
cv::Mat mtx, dist;
std::vector<cv::Mat> rvecs, tvecs;
cv::calibrateCamera(objpoints, imgpoints, gray.size(), mtx, dist, rvecs, tvecs);
vector<Point3f> objPts;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 9; j++)
{
objPts.push_back(Point3f((float)j, (float)i, (float)0));
}
}
// X、Y、Z三轴方向,占3格
vector<Point3f> axis;
axis.push_back(Point3f(3, 0, 0));
axis.push_back(Point3f(0, 3, 0));
axis.push_back(Point3f(0, 0, -3));
for (int i = 0; i < images.size(); i++)
{
Mat img = imread(images[i]);
cvtColor(img, gray, COLOR_BGR2GRAY);
std::vector<cv::Point2f> corners;
bool found = findChessboardCorners(gray, BoardSize, corners);
if (found == true)
{
cornerSubPix(gray, corners, Size(11, 11), Size(-1, -1), criteria);
cv::Mat rvec; // 输出的旋转向量
cv::Mat tvec; // 输出的平移向量
solvePnP(objPts, corners, mtx, dist, rvec, tvec);
// project 3D points to image plane
cv::Mat imgpts;
cv::Mat jac;
projectPoints(axis, rvec, tvec, mtx, dist, imgpts, jac);
img = draw(img, corners, imgpts);
imshow("img", img);
if (waitKey(0) == 27)
break;
}
}
}
Python版本代码如下:
import cv2
import numpy as np
import glob
# 函数 draw 它的参数有棋盘上的角点
# 使用 cv2.findChessboardCorners() 得到
# 绘制的 3D 坐标轴上的点
def draw(img, corners, imgpts):
corner = tuple(corners[0].ravel())
l0 = (int(corner[0]), int(corner[1]))
l1 = (int(imgpts[0].ravel()[0]), int(imgpts[0].ravel()[1]))
l2 = (int(imgpts[1].ravel()[0]), int(imgpts[1].ravel()[1]))
l3 = (int(imgpts[2].ravel()[0]), int(imgpts[2].ravel()[1]))
print(l0, l1, l2, l3)
img = cv2.line(img, l0, l1, (255, 0, 0), 5)
img = cv2.line(img, l0, l2, (0, 255, 0), 5)
img = cv2.line(img, l0, l3, (0, 0, 255), 5)
return img
# 渲染一个立方体
def draw_cube(img, corners, imgpts):
imgpts = np.int32(imgpts).reshape(-1, 2)
# draw ground floor in green
img = cv2.drawContours(img, [imgpts[:4]], -1, (0, 255, 0), -3)
# draw pillars in blue color
for i, j in zip(range(4), range(4, 8)):
img = cv2.line(img, tuple(imgpts[i]), tuple(imgpts[j]), (255), 3)
# draw top layer in red color
img = cv2.drawContours(img, [imgpts[4:]], -1, (0, 0, 255), 3)
return img
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((7 * 6, 3), np.float32)
objp[:, :2] = np.mgrid[0:7, 0:6].T.reshape(-1, 2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
images = glob.glob('../images/lefts/left*.jpg')
images += glob.glob('../images/rights/right*.jpg')
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (7, 6), None)
# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
imgpoints.append(corners)
# Draw and display the corners
cv2.drawChessboardCorners(img, (7, 6), corners2, ret)
cv2.imshow('img', img)
cv2.waitKey(1)
cv2.destroyAllWindows()
# 标定
ret, mtx, dist, _, _ = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
# X、Y、Z三轴方向,占3格
axis = np.float32([[3, 0, 0], [0, 3, 0], [0, 0, -3]]).reshape(-1, 3)
# 渲染一个立方体
# axis = np.float32([[0, 0, 0], [0, 3, 0], [3, 3, 0], [3, 0, 0],
# [0, 0, -3], [0, 3, -3], [3, 3, -3], [3, 0, -3]])
for fname in glob.glob('../images/lefts/left*.jpg'):
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (7, 6), None)
if ret:
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
# Find the rotation and translation vectors.
ret, rvecs, tvecs = cv2.solvePnP(objp, corners2, mtx, dist)
# project 3D points to image plane
imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)
img = draw(img, corners2, imgpts)
cv2.imshow('img', img)
cv2.imwrite('img.jpg', img)
if cv2.waitKey(0) == 27:
break
cv2.destroyAllWindows()
using OpenCvSharp;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace demo
{
internal class Program
{
static void Main(string[] args)
{
List<string> paths = new List<string>()
{
"../../../images/lefts/",
"../../../images/rights/"
};
List<string> imagesList = new List<string>();
foreach (string path in paths)
{
DirectoryInfo di = new DirectoryInfo(path);
FileInfo[] fis = di.GetFiles("*.jpg");
Mat srcImage = new Mat();
foreach (FileInfo fi in fis)
{
imagesList.Add(fi.FullName);
}
}
TermCriteria criteria = new TermCriteria(CriteriaTypes.Eps | CriteriaTypes.MaxIter, 30, 0.001);
// 棋盘格内角点数量(行数和列数)
Size BoardSize = new Size(9, 6);
// 棋盘格尺寸(单位:米)
float squareSize = 0.025f;
Mat img = new Mat();
Mat gray = new Mat();
List<List<Point3f>> objpoints = new List<List<Point3f>>();
List<List<Point2f>> imgpoints = new List<List<Point2f>>();
for (int i = 0; i < imagesList.Count; i++)
{
img = new Mat(imagesList[i]);
if (!img.Empty())
{
Cv2.CvtColor(img, gray, ColorConversionCodes.BGR2GRAY);
var corners = new Mat<Point2f>();
bool found = Cv2.FindChessboardCorners(gray, BoardSize, corners);
if (found == true)
{
var objectPointsArray = Create3DChessboardCorners(BoardSize, squareSize).ToArray();
var imagePointsArray = corners.ToArray();
//var objectPoints = Mat<Point3f>.FromArray(objectPointsArray);
//var imagePoints = Mat<Point2f>.FromArray(imagePointsArray);
objpoints.Add(objectPointsArray.ToList());
imgpoints.Add(imagePointsArray.ToList());
}
}
}
var mtx = new double[,] { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
var dist = new double[5];
Cv2.CalibrateCamera(objpoints, imgpoints, gray.Size(), mtx,
dist, out var rvecs, out var tvecs, CalibrationFlags.None);
List<Point3f> objPts = new List<Point3f>();
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 9; j++)
{
objPts.Add(new Point3f(j, i, 0));
}
}
// X、Y、Z三轴方向,占3格
var axis = new[]
{
new Point3f(3,0,0),
new Point3f(0,3,0),
new Point3f(0,0,-3),
};
foreach (string fname in imagesList)
{
img = Cv2.ImRead(fname);
Cv2.CvtColor(img, gray, ColorConversionCodes.BGR2GRAY);
Point2f[] corners;
bool found = Cv2.FindChessboardCorners(img, BoardSize, out corners);
if (found == true)
{
var corners2 = Cv2.CornerSubPix(gray, corners, new Size(11, 11), new Size(-1, -1), criteria);
var rvec = new double[] { };
var tvec = new double[] { };
Cv2.SolvePnP(objPts, corners2, mtx, dist, ref rvec, ref tvec);
Cv2.ProjectPoints(axis, rvec, tvec, mtx, dist, out var imgPts, out var jacobian);
img = draw(img, corners2, imgPts);
Cv2.ImShow("img", img);
if (Cv2.WaitKey(0) == 27)
break;
}
}
}
private static IEnumerable<Point3f> Create3DChessboardCorners(Size boardSize, float squareSize)
{
for (int y = 0; y < boardSize.Height; y++)
{
for (int x = 0; x < boardSize.Width; x++)
{
yield return new Point3f(x * squareSize, y * squareSize, 0);
}
}
}
static Mat draw(Mat img, Point2f[] corners, Point2f[] imgpts)
{
Point l0 = new Point((int)corners[0].X, (int)corners[0].Y);
Point l1 = new Point((int)imgpts[0].X, (int)imgpts[0].Y);
Point l2 = new Point((int)imgpts[1].X, (int)imgpts[1].Y);
Point l3 = new Point((int)imgpts[2].X, (int)imgpts[2].Y);
Cv2.Line(img, l0, l1, new Scalar(255, 0, 0), 5);
Cv2.Line(img, l0, l2, new Scalar(0, 255, 0), 5);
Cv2.Line(img, l0, l3, new Scalar(0, 0, 255), 5);
return img;
}
}
}
C++版本代码如下:
#include <opencv2/opencv.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/cvconfig.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <io.h>
#include <string>
#include <iostream>
#include <ctime>
using namespace cv;
using namespace std;
vector<Point3f> Create3DChessboardCorners(Size boardSize, float squareSize)
{
std::vector<cv::Point3f> objectPoints;
for (int y = 0; y < boardSize.height; y++)
{
for (int x = 0; x < boardSize.width; x++)
{
objectPoints.push_back(Point3f(x * squareSize, y * squareSize, 0));
}
}
return objectPoints;
}
Mat draw(Mat img, vector<Point2f> corners, vector<Point2f> imgpts)
{
Point l0((int)corners[0].x, (int)corners[0].y);
Point l1((int)imgpts[0].x, (int)imgpts[0].y);
Point l2((int)imgpts[1].x, (int)imgpts[1].y);
Point l3((int)imgpts[2].x, (int)imgpts[2].y);
line(img, l0, l1, Scalar(255, 0, 0), 5);
line(img, l0, l2, Scalar(0, 255, 0), 5);
line(img, l0, l3, Scalar(0, 0, 255), 5);
return img;
}
int main()
{
// 获取图片目录
std::string inPath1 = "..\\images\\lefts\\left*.jpg";
std::string inPath2 = "..\\images\\rights\\right*.jpg";
intptr_t handle1, handle2;
struct _finddata_t fileinfo1, fileinfo2;
handle1 = _findfirst(inPath1.c_str(), &fileinfo1);
handle2 = _findfirst(inPath2.c_str(), &fileinfo2);
vector<string> images;
do {
string Name = fileinfo1.name;
images.push_back("..\\images\\lefts\\" + Name);
} while (!_findnext(handle1, &fileinfo1));
do {
string Name = fileinfo2.name;
images.push_back("..\\images\\lefts\\" + Name);
} while (!_findnext(handle2, &fileinfo2));
TermCriteria criteria = TermCriteria(TermCriteria::EPS | TermCriteria::MAX_ITER, 30, 0.001);
// 棋盘格内角点数量(行数和列数)
cv::Size BoardSize(9, 6);
// 棋盘格尺寸(单位:米)
float squareSize = 0.025f;
// 存储棋盘格角点坐标的容器
std::vector<std::vector<cv::Point3f>> objpoints;
// 存储检测到的棋盘格角点坐标的容器
std::vector<std::vector<cv::Point2f>> imgpoints;
Mat gray;
for (int i = 0; i < images.size(); i++)
{
Mat img = imread(images[i]);
if (!img.empty())
{
cvtColor(img, gray, COLOR_BGR2GRAY);
// Find the chess board corners
std::vector<cv::Point2f> corners;;
bool found = findChessboardCorners(gray, BoardSize, corners);
if (found == true)
{
vector<Point3f> objectPointsArray = Create3DChessboardCorners(BoardSize, squareSize);
objpoints.push_back(objectPointsArray);
imgpoints.push_back(corners);
}
}
}
cv::Mat mtx, dist;
std::vector<cv::Mat> rvecs, tvecs;
cv::calibrateCamera(objpoints, imgpoints, gray.size(), mtx, dist, rvecs, tvecs);
vector<Point3f> objPts;
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 9; j++)
{
objPts.push_back(Point3f((float)j, (float)i, (float)0));
}
}
// X、Y、Z三轴方向,占3格
vector<Point3f> axis;
axis.push_back(Point3f(3, 0, 0));
axis.push_back(Point3f(0, 3, 0));
axis.push_back(Point3f(0, 0, -3));
for (int i = 0; i < images.size(); i++)
{
Mat img = imread(images[i]);
cvtColor(img, gray, COLOR_BGR2GRAY);
std::vector<cv::Point2f> corners;
bool found = findChessboardCorners(gray, BoardSize, corners);
if (found == true)
{
cornerSubPix(gray, corners, Size(11, 11), Size(-1, -1), criteria);
cv::Mat rvec; // 输出的旋转向量
cv::Mat tvec; // 输出的平移向量
solvePnP(objPts, corners, mtx, dist, rvec, tvec);
// project 3D points to image plane
cv::Mat imgpts;
cv::Mat jac;
projectPoints(axis, rvec, tvec, mtx, dist, imgpts, jac);
img = draw(img, corners, imgpts);
imshow("img", img);
if (waitKey(0) == 27)
break;
}
}
}
Python版本代码如下:
import cv2
import numpy as np
import glob
# 函数 draw 它的参数有棋盘上的角点
# 使用 cv2.findChessboardCorners() 得到
# 绘制的 3D 坐标轴上的点
def draw(img, corners, imgpts):
corner = tuple(corners[0].ravel())
l0 = (int(corner[0]), int(corner[1]))
l1 = (int(imgpts[0].ravel()[0]), int(imgpts[0].ravel()[1]))
l2 = (int(imgpts[1].ravel()[0]), int(imgpts[1].ravel()[1]))
l3 = (int(imgpts[2].ravel()[0]), int(imgpts[2].ravel()[1]))
print(l0, l1, l2, l3)
img = cv2.line(img, l0, l1, (255, 0, 0), 5)
img = cv2.line(img, l0, l2, (0, 255, 0), 5)
img = cv2.line(img, l0, l3, (0, 0, 255), 5)
return img
# 渲染一个立方体
def draw_cube(img, corners, imgpts):
imgpts = np.int32(imgpts).reshape(-1, 2)
# draw ground floor in green
img = cv2.drawContours(img, [imgpts[:4]], -1, (0, 255, 0), -3)
# draw pillars in blue color
for i, j in zip(range(4), range(4, 8)):
img = cv2.line(img, tuple(imgpts[i]), tuple(imgpts[j]), (255), 3)
# draw top layer in red color
img = cv2.drawContours(img, [imgpts[4:]], -1, (0, 0, 255), 3)
return img
# termination criteria
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 30, 0.001)
# prepare object points, like (0,0,0), (1,0,0), (2,0,0) ....,(6,5,0)
objp = np.zeros((7 * 6, 3), np.float32)
objp[:, :2] = np.mgrid[0:7, 0:6].T.reshape(-1, 2)
# Arrays to store object points and image points from all the images.
objpoints = [] # 3d point in real world space
imgpoints = [] # 2d points in image plane.
images = glob.glob('../images/lefts/left*.jpg')
images += glob.glob('../images/rights/right*.jpg')
for fname in images:
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Find the chess board corners
ret, corners = cv2.findChessboardCorners(gray, (7, 6), None)
# If found, add object points, image points (after refining them)
if ret == True:
objpoints.append(objp)
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
imgpoints.append(corners)
# Draw and display the corners
cv2.drawChessboardCorners(img, (7, 6), corners2, ret)
cv2.imshow('img', img)
cv2.waitKey(1)
cv2.destroyAllWindows()
# 标定
ret, mtx, dist, _, _ = cv2.calibrateCamera(objpoints, imgpoints, gray.shape[::-1], None, None)
# X、Y、Z三轴方向,占3格
axis = np.float32([[3, 0, 0], [0, 3, 0], [0, 0, -3]]).reshape(-1, 3)
# 渲染一个立方体
# axis = np.float32([[0, 0, 0], [0, 3, 0], [3, 3, 0], [3, 0, 0],
# [0, 0, -3], [0, 3, -3], [3, 3, -3], [3, 0, -3]])
for fname in glob.glob('../images/lefts/left*.jpg'):
img = cv2.imread(fname)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, corners = cv2.findChessboardCorners(gray, (7, 6), None)
if ret:
corners2 = cv2.cornerSubPix(gray, corners, (11, 11), (-1, -1), criteria)
# Find the rotation and translation vectors.
ret, rvecs, tvecs = cv2.solvePnP(objp, corners2, mtx, dist)
# project 3D points to image plane
imgpts, jac = cv2.projectPoints(axis, rvecs, tvecs, mtx, dist)
img = draw(img, corners2, imgpts)
cv2.imshow('img', img)
cv2.imwrite('img.jpg', img)
if cv2.waitKey(0) == 27:
break
cv2.destroyAllWindows()