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 3 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
List of users who liked
22