var src = new Mat(@".\dot_Bobbin_img.png", ImreadModes.Grayscale);
var patternSize = new Size(18, 13);
var centers = new Mat();
var found = Cv2.FindCirclesGrid(src, patternSize, centers, FindCirclesGridFlags.SymmetricGrid);
var points_img = new Mat();
Cv2.CvtColor(src.Clone(), points_img, ColorConversionCodes.GRAY2BGR);
Cv2.DrawChessboardCorners(points_img, patternSize, centers, true);
Cv2.ImShow("points", points_img);
Cv2.WaitKey();
int left_margin = 26;
int top_margin = 18;
int interval = 44;
var object_points = new Mat<Point3f>();
for (int j = 0; j < patternSize.Height; j++)
{
for (int i = 0; i < patternSize.Width; i++)
{
object_points.Add(new Point3f(left_margin + i * interval, top_margin + j * interval, 0));
}
}
var imageSize = src.Size();
var cameraMatrix = new Mat<double>(3, 3);
var distCoeffs = new Mat<double>(5, 1);
Cv2.CalibrateCamera(new[] { object_points }, new[] { centers }, imageSize, cameraMatrix, distCoeffs, out Mat[] rvecs, out Mat[] tvecs);
var newImageSize = new Size();
var newCameraMatrix = Cv2.GetOptimalNewCameraMatrix(cameraMatrix, distCoeffs, imageSize, 1, newImageSize, out Rect validPixROI);
var data = new Mat(@".\dot_Bobbin_img.png", ImreadModes.Grayscale);
var temp_img = new Mat();
Cv2.Undistort(data, temp_img, cameraMatrix, distCoeffs, newCameraMatrix);
var rotation = new Mat();
Cv2.Rodrigues(rvecs[0], rotation);
var transRot = new Mat<double>(3, 3);
rotation.Col(0).CopyTo(transRot.Col(0));
rotation.Col(1).CopyTo(transRot.Col(1));
var transData = new double[3,3] { { 0, 0, tvecs[0].At<double>(0) },{ 0, 0, tvecs[0].At<double>(1) }, { 0, 0, tvecs[0].At<double>(2) } };
var translate = InputArray.Create(transData).GetMat();
translate.Col(2).CopyTo(transRot.Col(2));
var dst_img = new Mat();
var m = newCameraMatrix * transRot;
Cv2.WarpPerspective(temp_img, dst_img, m, newImageSize, InterpolationFlags.WarpInverseMap);
Cv2.ImShow("temp", temp_img);
Cv2.WaitKey();
Cv2.ImShow("dst", dst_img);
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme
![33661a48b48dca01f98a042375aba368[1].png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F662099%2F4a6a39da-9c8f-ef9d-37d9-2d7be5a5d169.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=bacdb713701a9011ca2cd84631fa3e15)
![c83a5792b630f2b553904eb8b601e50a[1].png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F662099%2F7065801a-c74c-ad87-3227-1fd59402e4a6.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=c49cba621b823caf96c6aeed8f2002e0)
![fb3273acdc75324b4c4ede4e3b11bef8[1].png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F662099%2F27ff1b2b-07b9-9ffd-9ac0-89f11f959b59.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=dbc2a7997226db739e07bab8d95e249f)
![41534924ae29ab8249ea3a1a3bf25e62[1].png](https://qiita-user-contents.imgix.net/https%3A%2F%2Fqiita-image-store.s3.ap-northeast-1.amazonaws.com%2F0%2F662099%2F84a5702b-8c50-6d53-05d0-6179896a97c4.png?ixlib=rb-4.0.0&auto=format&gif-q=60&q=75&s=3fe04619993a48de8944f531181803c9)