35 void ProjPoints::Reset() {
36 object_points.clear();
43 if (image->width == 0)
return false;
44 IplImage *
gray = cvCreateImage(cvSize(image->width, image->height), IPL_DEPTH_8U, 1);
45 CvPoint2D32f *corners =
new CvPoint2D32f[etalon_rows*
etalon_columns];
46 if (image->nChannels == 1)
49 cvCvtColor(image, gray, CV_RGB2GRAY);
54 int pattern_was_found = cvFindChessboardCorners(gray, cvSize(etalon_rows, etalon_columns), corners, &point_count);
55 if (!pattern_was_found) point_count=0;
56 if (point_count > 0) {
57 cvFindCornerSubPix(gray, corners, point_count, cvSize(5,5), cvSize(-1,-1),
58 cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, 0.01
f));
59 for (
int i=0; i<point_count; i++) {
67 object_points.push_back(po);
68 image_points.push_back(pi);
70 point_counts.push_back(point_count);
73 cvDrawChessboardCorners(image, cvSize(etalon_rows, etalon_columns),
74 corners, point_count,
false );
77 cvReleaseImage(&gray);
78 if (point_count > 0)
return true;
82 bool ProjPoints::AddPointsUsingMarkers(vector<PointDouble> &marker_corners, vector<PointDouble> &marker_corners_img, IplImage*
image)
86 if ((marker_corners.size() == marker_corners_img.size()) &&
87 (marker_corners.size() == 4))
89 for (
size_t p=0; p<marker_corners.size(); p++) {
92 po.x = marker_corners[p].x;
93 po.y = marker_corners[p].y;
95 pi.x = marker_corners_img[p].x;
96 pi.y = marker_corners_img[p].y;
97 object_points.push_back(po);
98 image_points.push_back(pi);
100 point_counts.push_back(marker_corners.size());
107 calib_K = cvMat(3, 3, CV_64F, calib_K_data);
108 calib_D = cvMat(4, 1, CV_64F, calib_D_data);
109 memset(calib_K_data,0,
sizeof(
double)*3*3);
110 memset(calib_D_data,0,
sizeof(
double)*4);
111 calib_K_data[0][0] = 550;
112 calib_K_data[1][1] = 550;
113 calib_K_data[0][2] = 320;
114 calib_K_data[1][2] = 240;
115 calib_K_data[2][2] = 1;
139 ROS_INFO (
"Subscribing to info topic");
177 TiXmlDocument document;
178 if (!document.LoadFile(calibfile))
return false;
179 TiXmlElement *xml_root = document.RootElement();
182 xml_root->QueryIntAttribute(
"width", &
calib_x_res) == TIXML_SUCCESS &&
183 xml_root->QueryIntAttribute(
"height", &
calib_y_res) == TIXML_SUCCESS &&
189 cvSetErrMode(CV_ErrModeSilent);
190 CvFileStorage* fs = cvOpenFileStorage(calibfile, 0, CV_STORAGE_READ);
191 cvSetErrMode(CV_ErrModeLeaf);
193 CvFileNode* root_node = cvGetRootFileNode(fs);
195 CvFileNode* intrinsic_mat_node = cvGetFileNodeByName(fs, root_node,
"intrinsic_matrix");
196 CvMat* intrinsic_mat =
reinterpret_cast<CvMat*
>(cvRead(fs, intrinsic_mat_node));
197 cvmSet(&
calib_K, 0, 0, cvmGet(intrinsic_mat, 0, 0));
198 cvmSet(&
calib_K, 0, 1, cvmGet(intrinsic_mat, 0, 1));
199 cvmSet(&
calib_K, 0, 2, cvmGet(intrinsic_mat, 0, 2));
200 cvmSet(&
calib_K, 1, 0, cvmGet(intrinsic_mat, 1, 0));
201 cvmSet(&
calib_K, 1, 1, cvmGet(intrinsic_mat, 1, 1));
202 cvmSet(&
calib_K, 1, 2, cvmGet(intrinsic_mat, 1, 2));
203 cvmSet(&
calib_K, 2, 0, cvmGet(intrinsic_mat, 2, 0));
204 cvmSet(&
calib_K, 2, 1, cvmGet(intrinsic_mat, 2, 1));
205 cvmSet(&
calib_K, 2, 2, cvmGet(intrinsic_mat, 2, 2));
208 CvFileNode* dist_mat_node = cvGetFileNodeByName(fs, root_node,
"distortion");
209 CvMat* dist_mat =
reinterpret_cast<CvMat*
>(cvRead(fs, dist_mat_node));
210 cvmSet(&
calib_D, 0, 0, cvmGet(dist_mat, 0, 0));
211 cvmSet(&
calib_D, 1, 0, cvmGet(dist_mat, 1, 0));
212 cvmSet(&
calib_D, 2, 0, cvmGet(dist_mat, 2, 0));
213 cvmSet(&
calib_D, 3, 0, cvmGet(dist_mat, 3, 0));
216 CvFileNode* width_node = cvGetFileNodeByName(fs, root_node,
"width");
217 CvFileNode* height_node = cvGetFileNodeByName(fs, root_node,
"height");
220 cvReleaseFileStorage(&fs);
224 cvSetErrStatus(CV_StsOk);
273 if(!calibfile)
return false;
275 bool success =
false;
306 TiXmlDocument document;
307 document.LinkEndChild(
new TiXmlDeclaration(
"1.0",
"UTF-8",
"no"));
308 document.LinkEndChild(
new TiXmlElement(
"camera"));
309 TiXmlElement *xml_root = document.RootElement();
314 return document.SaveFile(calibfile);
318 cvSetErrMode(CV_ErrModeSilent);
319 CvFileStorage* fs = cvOpenFileStorage(calibfile, 0, CV_STORAGE_WRITE);
320 cvSetErrMode(CV_ErrModeLeaf);
322 cvWrite(fs,
"intrinsic_matrix", &
calib_K, cvAttrList(0,0));
323 cvWrite(fs,
"distortion", &
calib_D, cvAttrList(0,0));
328 cvReleaseFileStorage(&fs);
332 cvSetErrStatus(CV_StsOk);
353 CvMat *object_points = cvCreateMat((
int)pp.
object_points.size(), 1, CV_32FC3);
354 CvMat *image_points = cvCreateMat((
int)pp.
image_points.size(), 1, CV_32FC2);
357 object_points->data.fl[i*3+0] = (float)pp.
object_points[i].x;
358 object_points->data.fl[i*3+1] = (
float)pp.
object_points[i].y;
359 object_points->data.fl[i*3+2] = (float)pp.
object_points[i].z;
360 image_points->data.fl[i*2+0] = (
float)pp.
image_points[i].x;
361 image_points->data.fl[i*2+1] = (float)pp.
image_points[i].y;
363 cvCalibrateCamera2(object_points, image_points, &point_counts,
370 cvReleaseMat(&object_points);
371 cvReleaseMat(&image_points);
395 proj_matrix[0] = 2.0f *
calib_K_data[0][0] / float(width);
399 proj_matrix[4] = 2.0f *
calib_K_data[0][1] / float(width);
400 proj_matrix[5] = 2.0f *
calib_K_data[1][1] / float(height);
404 proj_matrix[8] = -(2.0f *
calib_K_data[0][2] / float(width)) + 1.0
f;
405 proj_matrix[9] = (2.0f *
calib_K_data[1][2] / float(height)) - 1.0
f;
406 proj_matrix[10] = -(far_clip + near_clip)/(far_clip - near_clip);
407 proj_matrix[11] = -1.0f;
410 proj_matrix[14] = -2.0f * far_clip * near_clip / (far_clip - near_clip);
419 calib_K_data[0][0] = proj_matrix[0] * float(width) / 2.0f;
420 calib_K_data[0][1] = proj_matrix[4] * float(width) / 2.0f;
421 calib_K_data[1][1] = proj_matrix[5] * float(height) / 2.0f;
423 calib_K_data[0][2] = (-proj_matrix[8] + 1.0f) *
float(width) / 2.0f;
424 calib_K_data[1][2] = (proj_matrix[9] + 1.0f) *
float(height) / 2.0f;
642 double ext_rodriques[3];
643 double ext_translate[3];
644 CvMat ext_rodriques_mat = cvMat(3, 1, CV_64F, ext_rodriques);
645 CvMat ext_translate_mat = cvMat(3, 1, CV_64F, ext_translate);
646 CvMat *object_points = cvCreateMat((
int)pw.size(), 1, CV_32FC3);
647 CvMat *image_points = cvCreateMat((
int)pi.size(), 1, CV_32FC2);
648 for (
size_t i=0; i<pw.size(); i++) {
649 object_points->data.fl[i*3+0] = (float)pw[i].
x;
650 object_points->data.fl[i*3+1] = (float)pw[i].
y;
651 object_points->data.fl[i*3+2] = (float)pw[i].
z;
652 image_points->data.fl[i*2+0] = (float)pi[i].
x;
653 image_points->data.fl[i*2+1] = (float)pi[i].
y;
656 cvFindExtrinsicCameraParams2(object_points, image_points, &
calib_K, NULL, &ext_rodriques_mat, &ext_translate_mat);
659 cvReleaseMat(&object_points);
660 cvReleaseMat(&image_points);
664 CvMat *rodriques, CvMat *tra)
668 int size = (int)pi.size();
670 CvPoint3D64f *world_pts =
new CvPoint3D64f[size];
671 CvPoint2D64f *image_pts =
new CvPoint2D64f[size];
673 for(
int i = 0; i < size; i++){
674 world_pts[i].x = pw[i].x;
675 world_pts[i].y = pw[i].y;
676 world_pts[i].z = pw[i].z;
680 image_pts[i].x = pi[i].x;
681 image_pts[i].y = pi[i].y;
685 CvMat world_mat, image_mat, rot_vec;
686 cvInitMatHeader(&world_mat, size, 1, CV_64FC3, world_pts);
687 cvInitMatHeader(&image_mat, size, 1, CV_64FC2, image_pts);
688 cvInitMatHeader(&rot_vec, 3, 1, CV_64FC1, rot);
692 cvFindExtrinsicCameraParams2(&world_mat, &image_mat, &
calib_K, &
calib_D, rodriques, tra);
699 CvMat *rodriques, CvMat *tra)
702 int size = (int)pi.size();
704 vector<CvPoint3D64f> pw3;
706 for (
int i=0; i<size; i++) {
717 double ext_rodriques[3];
718 double ext_translate[3];
719 CvMat ext_rodriques_mat = cvMat(3, 1, CV_64F, ext_rodriques);
720 CvMat ext_translate_mat = cvMat(3, 1, CV_64F, ext_translate);
727 cvFindExtrinsicCameraParams2(object_points, image_points, &
calib_K, &
calib_D, rodriques, tra);
732 double ext_rodriques[3];
733 double ext_translate[3];
734 CvMat ext_rodriques_mat = cvMat(3, 1, CV_64F, ext_rodriques);
735 CvMat ext_translate_mat = cvMat(3, 1, CV_64F, ext_translate);
743 double ext_rodriques[3];
744 double ext_translate[3];
745 CvMat ext_rodriques_mat = cvMat(3, 1, CV_64F, ext_rodriques);
746 CvMat ext_translate_mat = cvMat(3, 1, CV_64F, ext_translate);
749 CvMat *object_points = cvCreateMat((
int)pw.size(), 1, CV_32FC3);
750 CvMat *image_points = cvCreateMat((
int)pi.size(), 1, CV_32FC2);
751 for (
size_t i=0; i<pw.size(); i++) {
752 object_points->data.fl[i*3+0] = (float)pw[i].
x;
753 object_points->data.fl[i*3+1] = (float)pw[i].
y;
754 object_points->data.fl[i*3+2] = (float)pw[i].
z;
756 cvProjectPoints2(object_points, &ext_rodriques_mat, &ext_translate_mat, &
calib_K, &
calib_D, image_points);
757 for (
size_t i=0; i<pw.size(); i++) {
758 pi[i].x = image_points->data.fl[i*2+0];
759 pi[i].y = image_points->data.fl[i*2+1];
761 cvReleaseMat(&object_points);
762 cvReleaseMat(&image_points);
766 const CvMat* translation_vector, CvMat* image_points)
const 769 cvProjectPoints2(object_points, rotation_vector, translation_vector, &
calib_K, &
calib_D, image_points);
774 double ext_rodriques[3];
775 double ext_translate[3];
776 CvMat ext_rodriques_mat = cvMat(3, 1, CV_64F, ext_rodriques);
777 CvMat ext_translate_mat = cvMat(3, 1, CV_64F, ext_translate);
780 cvProjectPoints2(object_points, &ext_rodriques_mat, &ext_translate_mat, &
calib_K, &
calib_D, image_points);
786 gl[0], gl[4], gl[8], gl[12],
787 gl[1], gl[5], gl[9], gl[13],
788 gl[2], gl[6], gl[10], gl[14],
789 gl[3], gl[7], gl[11], gl[15],
791 CvMat glm_mat = cvMat(4, 4, CV_64F, glm);
794 double cv_mul_data[4][4];
795 CvMat cv_mul = cvMat(4, 4, CV_64F, cv_mul_data);
796 cvSetIdentity(&cv_mul);
797 cvmSet(&cv_mul, 1, 1, -1);
798 cvmSet(&cv_mul, 2, 2, -1);
799 cvMatMul(&cv_mul, &glm_mat, &glm_mat);
805 CvMat rod_mat=cvMat(3, 1, CV_64F, rod);
808 double tra[3] = { glm[0][3], glm[1][3], glm[2][3] };
809 CvMat tra_mat = cvMat(3, 1, CV_64F, tra);
811 ProjectPoints(object_points, &rod_mat, &tra_mat, image_points);
815 float object_points_data[3] = {(float)pw.x, (
float)pw.y, (float)pw.z};
816 float image_points_data[2] = {0};
817 CvMat object_points = cvMat(1, 1, CV_32FC3, object_points_data);
818 CvMat image_points = cvMat(1, 1, CV_32FC2, image_points_data);
820 pi.x = image_points.data.fl[0];
821 pi.y = image_points.data.fl[1];
825 float object_points_data[3] = {(float)pw.x, (
float)pw.y, (float)pw.z};
826 float image_points_data[2] = {0};
827 CvMat object_points = cvMat(1, 1, CV_32FC3, object_points_data);
828 CvMat image_points = cvMat(1, 1, CV_32FC2, image_points_data);
830 pi.x = image_points.data.fl[0];
831 pi.y = image_points.data.fl[1];
835 cvInitMatHeader(&H, 3, 3, CV_64F, H_data);
840 assert(pw.size() == pi.size());
841 int size = (int)pi.size();
843 CvPoint2D64f *srcp =
new CvPoint2D64f[size];
844 CvPoint2D64f *dstp =
new CvPoint2D64f[size];
846 for(
int i = 0; i < size; ++i){
854 CvMat src_pts, dst_pts;
855 cvInitMatHeader(&dst_pts, 1, size, CV_64FC2, dstp);
856 cvInitMatHeader(&src_pts, 1, size, CV_64FC2, srcp);
859 cvFindHomography(&src_pts, &dst_pts, &H, 0, 0, 0);
861 cvFindHomography(&src_pts, &dst_pts, &H);
870 int size = (int)from.size();
872 CvPoint3D64f *srcp =
new CvPoint3D64f[size];
874 for(
int i = 0; i < size; ++i){
875 srcp[i].x = from[i].x;
876 srcp[i].y = from[i].y;
880 CvPoint3D64f *dstp =
new CvPoint3D64f[size];
882 CvMat src_pts, dst_pts;
883 cvInitMatHeader(&src_pts, 1, size, CV_64FC3, srcp);
884 cvInitMatHeader(&dst_pts, 1, size, CV_64FC3, dstp);
886 cvTransform(&src_pts, &dst_pts, &H);
889 for(
int i = 0; i < size; ++i)
892 pt.x = dstp[i].x / dstp[i].z;
893 pt.y = dstp[i].y / dstp[i].z;
void GetOpenglProjectionMatrix(double proj_matrix[16], const int width, const int height, const float far_clip=1000.0f, const float near_clip=0.1f)
Get OpenGL matrix Generates the OpenGL projection matrix based on OpenCV intrinsic camera matrix K...
void GetRodriques(CvMat *mat) const
Returns the rotation in rotation vector form.
void camInfoCallback(const sensor_msgs::CameraInfoConstPtr &)
bool SaveCalib(const char *calibfile, FILE_FORMAT format=FILE_FORMAT_DEFAULT)
Save the current calibration information to a file.
This file implements a camera used for projecting points and computing homographies.
std::string cam_info_topic
Subscriber subscribe(const std::string &topic, uint32_t queue_size, void(T::*fp)(M), T *obj, const TransportHints &transport_hints=TransportHints())
void GetTranslation(CvMat *tra) const
void SetCameraInfo(const sensor_msgs::CameraInfo &camInfo)
std::vector< int > point_counts
Vector indicating how many points are detected for each frame.
void SetRes(int _x_res, int _y_res)
If we have no calibration file we can still adjust the default calibration to current resolution...
void SetMatrix(const CvMat *mat)
Sets the rotation from given rotation matrix. 3x3 and 4x4 matrices are allowed.
TFSIMD_FORCE_INLINE const tfScalar & y() const
void SetSimpleCalib(int _x_res, int _y_res, double f_fac=1.)
bool SetCalib(const char *calibfile, int _x_res, int _y_res, FILE_FORMAT format=FILE_FORMAT_DEFAULT)
Set the calibration file and the current resolution for which the calibration is adjusted to...
std::vector< CvPoint3D64f > object_points
3D object points corresponding with the detected 2D image points.
sensor_msgs::CameraInfo cam_info_
void ProjectPoints(std::vector< CvPoint3D64f > &pw, Pose *pose, std::vector< CvPoint2D64f > &pi) const
Project points.
void Find(const std::vector< PointDouble > &pw, const std::vector< PointDouble > &pi)
Find Homography for two point-sets.
void ProjectPoint(const CvPoint3D64f pw, const Pose *pose, CvPoint2D64f &pi) const
Project one point.
Simple structure for collecting 2D and 3D points e.g. for camera calibration.
Pose representation derived from the Rotation class
bool SaveCalibOpenCV(const char *calibfile)
TFSIMD_FORCE_INLINE const tfScalar & x() const
void Calibrate(ProjPoints &pp)
Calibrate using the collected ProjPoints.
File format written with cvWrite.
void SetRodriques(const CvMat *mat)
Sets the rotation from given rotation vector.
std::vector< CvPoint2D64f > image_points
Detected 2D object points If point_counts[0] == 10, then the first 10 points are detected in the firs...
TFSIMD_FORCE_INLINE const tfScalar & z() const
double calib_K_data[3][3]
void Distort(CvPoint2D32f &point)
Applys the lens distortion for one point on an image plane.
bool SaveCalibXML(const char *calibfile)
ALVAR_EXPORT Point< CvPoint2D64f > PointDouble
The default double point type.
void ProjectPoints(const std::vector< PointDouble > &from, std::vector< PointDouble > &to)
Project points using the Homography.
void CalcExteriorOrientation(std::vector< CvPoint3D64f > &pw, std::vector< CvPoint2D64f > &pi, Pose *pose)
Calculate exterior orientation.
bool LoadCalibOpenCV(const char *calibfile)
void SetTranslation(const CvMat *tra)
bool LoadCalibXML(const char *calibfile)
Rotation structure and transformations between different parameterizations.
This file defines library export definitions, version numbers and build information.
void Undistort(std::vector< PointDouble > &points)
Unapplys the lens distortion for points on image plane.
std::string cameraInfoTopic_
void SetOpenglProjectionMatrix(double proj_matrix[16], const int width, const int height)
Invert operation for GetOpenglProjectionMatrix.