45 #include <opencv2/calib3d/calib3d_c.h>
46 #include <opencv2/core/core_c.h>
67 #pragma GCC diagnostic push
68 #pragma GCC diagnostic ignored "-Wclass-memaccess"
70 #pragma GCC diagnostic pop
98 int pointCount, CvPoint2D32f* _points)
123 if (!params || cvRound(params[0]) != params[0] || params[0] < 3 ||
124 cvRound(params[1]) != params[1] || params[1] < 3 || params[2] <= 0)
130 pointCount = cvRound((params[0] - 1) * (params[1] - 1));
136 if (!_points || pointCount < 4)
170 int etalonWidth = cvRound(params[0]) - 1;
171 int etalonHeight = cvRound(params[1]) - 1;
178 for (y = 0; y < etalonHeight; y++)
179 for (x = 0; x < etalonWidth; x++)
181 etalonPoints[k++] = cvPoint2D32f((etalonWidth - 1 - x) * params[2],
209 int* pointCount,
const CvPoint2D32f** _points)
const
271 static void cvCalibrateCamera(
int image_count,
int* _point_counts,
272 cv::Size image_size, cv::Point2f* _image_points, cv::Point2f* _object_points,
273 float* _distortion_coeffs,
float* _camera_matrix,
float* _translation_vectors,
274 float* _rotation_matrices,
int flags)
277 cv::Mat point_counts = cv::Mat(image_count, 1, CV_32SC1, _point_counts);
278 std::vector<std::vector<cv::Point2f> > image_points, object_points;
279 cv::Mat dist_coeffs = cv::Mat(4, 1, CV_32FC1, _distortion_coeffs);
280 cv::Mat camera_matrix = cv::Mat(3, 3, CV_32FC1, _camera_matrix);
281 std::vector<cv::Mat> rotation_matrices;
282 std::vector<cv::Mat> translation_vectors;
284 for (i = 0; i < image_count; i++)
286 total += _point_counts[i];
287 for (
int j = 0; j < _point_counts[i]; j++)
289 std::vector<cv::Point2f> img, obj;
290 img.insert(img.begin(), _image_points, _image_points + _point_counts[i]);
291 obj.insert(obj.begin(), _object_points, _object_points + _point_counts[i]);
292 image_points.push_back(img);
293 object_points.push_back(obj);
294 _image_points += _point_counts[i];
295 _object_points += _point_counts[i];
298 rotation_matrices.emplace_back(1, 9, CV_32FC1, _rotation_matrices);
299 translation_vectors.emplace_back(1, 3, CV_32FC1, _translation_vectors);
300 _rotation_matrices += 9;
301 _translation_vectors += 3;
305 cv::calibrateCamera(object_points, image_points, std::move(image_size),
306 camera_matrix, dist_coeffs, rotation_matrices, translation_vectors,
310 #define icvCheckVector_32f( ptr, len )
311 #define icvCheckVector_64f( ptr, len )
314 const float* src2,
int w2,
int h2,
325 for (i = 0; i < h1; i++, src1 += w1, dst += w2)
326 for (j = 0; j < w2; j++)
329 for (k = 0; k < w1; k++)
331 s += src1[k] * src2[j + k * w2];
340 double* dst,
int len)
343 for (i = 0; i < len; i++)
345 dst[i] = src1[i] - src2[i];
352 const double* src2,
int w2,
int h2,
363 for (i = 0; i < h1; i++, src1 += w1, dst += w2)
364 for (j = 0; j < w2; j++)
367 for (k = 0; k < w1; k++)
369 s += src1[k] * src2[j + k * w2];
379 CvMat Am = cvMat(n, n, CV_64F,
A);
380 CvMat invAm = cvMat(n, n, CV_64F, invA);
382 cvInvert(&Am, &invAm, CV_SVD);
386 double* dst,
int len)
389 for (i = 0; i < len; i++)
391 dst[i] = src1[i] + src2[i];
401 for (i = 0; i < w; i++)
402 for (j = 0; j < h; j++)
404 *dst++ = src[j * w + i];
412 return cvInvert(src, dst, CV_SVD);
517 #define cvmMul( src1, src2, dst ) cvMatMulAdd( src1, src2, 0, dst )
544 CvPoint3D32f* epipole1,
545 CvPoint3D32f* epipole2);
570 pointSym2->x = pointCorner.x + alpha * (point1.x - pointCorner.x);
571 pointSym2->y = pointCorner.y + alpha * (point1.y - pointCorner.y);
572 pointSym2->z = pointCorner.z + alpha * (point1.z - pointCorner.z);
580 CvStereoLineCoeff* coeffs,
590 double alphabetta = alpha * betta;
592 partAll = alpha - betta;
593 if (fabs(partAll) > 0.00001)
596 partX = coeffs->Xcoef + coeffs->XcoefA * alpha +
597 coeffs->XcoefB * betta + coeffs->XcoefAB * alphabetta;
599 partY = coeffs->Ycoef + coeffs->YcoefA * alpha +
600 coeffs->YcoefB * betta + coeffs->YcoefAB * alphabetta;
602 partZ = coeffs->Zcoef + coeffs->ZcoefA * alpha +
603 coeffs->ZcoefB * betta + coeffs->ZcoefAB * alphabetta;
605 invPartAll = 1.0 / partAll;
607 point->x = partX * invPartAll;
608 point->y = partY * invPartAll;
609 point->z = partZ * invPartAll;
628 double invRotMatr2[9];
686 CvStereoLineCoeff* coeffs,
687 int* needSwapCamera);
689 static int icvComputeCoeffForStereoV3(
double quad1[4][2],
698 CvStereoLineCoeff* startCoeffs,
711 for (currLine = 0; currLine < numScanlines; currLine++)
714 double alpha = ((double)currLine) / ((double)(numScanlines));
716 point1.x = (1.0 - alpha) * quad1[0][0] + alpha * quad1[3][0];
717 point1.y = (1.0 - alpha) * quad1[0][1] + alpha * quad1[3][1];
719 point2.x = (1.0 - alpha) * quad1[1][0] + alpha * quad1[2][0];
720 point2.y = (1.0 - alpha) * quad1[1][1] + alpha * quad1[2][1];
722 point3.x = (1.0 - alpha) * quad2[0][0] + alpha * quad2[3][0];
723 point3.y = (1.0 - alpha) * quad2[0][1] + alpha * quad2[3][1];
725 point4.x = (1.0 - alpha) * quad2[1][0] + alpha * quad2[2][0];
726 point4.y = (1.0 - alpha) * quad2[1][1] + alpha * quad2[2][1];
739 &startCoeffs[currLine],
746 static int icvCvt_32f_64d(
float* src,
double* dst,
int size);
748 static int icvComputeCoeffForStereoNew(
double quad1[4][2],
755 CvStereoLineCoeff* startCoeffs,
760 double camMatr1_64d[9];
761 double camMatr2_64d[9];
763 double rotMatr1_64d[9];
764 double transVect1_64d[3];
766 double rotMatr2_64d[9];
767 double transVect2_64d[3];
769 icvCvt_32f_64d(camMatr1, camMatr1_64d, 9);
770 icvCvt_32f_64d(camMatr2, camMatr2_64d, 9);
772 icvCvt_32f_64d(rotMatr1, rotMatr1_64d, 9);
773 icvCvt_32f_64d(transVect1, transVect1_64d, 3);
785 transVect2_64d[0] = 0;
786 transVect2_64d[1] = 0;
787 transVect2_64d[2] = 0;
789 int status = icvComputeCoeffForStereoV3(quad1,
812 for (i = 0; i < 4; i++)
814 quad1[i][0] = stereoCamera->quad[0][i].x;
815 quad1[i][1] = stereoCamera->quad[0][i].y;
817 quad2[i][0] = stereoCamera->quad[1][i].x;
818 quad2[i][1] = stereoCamera->quad[1][i].y;
821 icvComputeCoeffForStereoNew(quad1,
823 stereoCamera->warpSize.height,
824 stereoCamera->camera[0]->matrix,
825 stereoCamera->rotMatrix,
826 stereoCamera->transVector,
827 stereoCamera->camera[1]->matrix,
828 stereoCamera->lineCoeffs,
829 &(stereoCamera->needSwapCameras));
837 CvStereoLineCoeff* coeffs);
859 CvStereoLineCoeff* coeffs,
865 CvPoint3D64f direct1;
866 CvPoint3D64f direct2;
867 CvPoint3D64f camPoint1;
869 CvPoint3D64f directS3;
870 CvPoint3D64f directS4;
871 CvPoint3D64f direct3;
872 CvPoint3D64f direct4;
873 CvPoint3D64f camPoint2;
895 double convRotMatr[9];
896 double convTransVect[3];
905 CvPoint3D64f zeroVect;
906 zeroVect.x = zeroVect.y = zeroVect.z = 0.0;
907 camPoint1.x = camPoint1.y = camPoint1.z = 0.0;
934 pointNewA.x = pointNewA.y = pointNewA.z = 0;
935 pointNewC.x = pointNewC.y = pointNewC.z = 0;
984 len1 =
sqrt((xA - xB) * (xA - xB) + (yA - yB) * (yA - yB) + (zA - zB) * (zA - zB));
985 len2 =
sqrt((xB - xC) * (xB - xC) + (yB - yC) * (yB - yC) + (zB - zC) * (zB - zC));
1058 double a11, a12, a21, a22;
1061 a11 = (xB - xA) * (xB - xA) + (yB - yA) * (yB - yA) + (zB - zA) * (zB - zA);
1062 a12 = -(xD - xC) * (xB - xA) - (yD - yC) * (yB - yA) - (zD - zC) * (zB - zA);
1063 a21 = (xB - xA) * (xD - xC) + (yB - yA) * (yD - yC) + (zB - zA) * (zD - zC);
1064 a22 = -(xD - xC) * (xD - xC) - (yD - yC) * (yD - yC) - (zD - zC) * (zD - zC);
1065 b1 = -((xA - xC) * (xB - xA) + (yA - yC) * (yB - yA) + (zA - zC) * (zB - zA));
1066 b2 = -((xA - xC) * (xD - xC) + (yA - yC) * (yD - yC) + (zA - zC) * (zD - zC));
1069 double deltaA, deltaB;
1070 double alpha, betta;
1072 delta = a11 * a22 - a12 * a21;
1074 if (fabs(delta) <
EPS64D)
1079 deltaA = b1 * a22 - b2 * a12;
1080 deltaB = a11 * b2 - b1 * a21;
1082 alpha = deltaA / delta;
1083 betta = deltaB / delta;
1085 xM = xA + alpha * (xB - xA);
1086 yM = yA + alpha * (yB - yA);
1087 zM = zA + alpha * (zB - zA);
1089 xN = xC + betta * (xD - xC);
1090 yN = yC + betta * (yD - yC);
1091 zN = zC + betta * (zD - zC);
1094 midPoint->x = (xM + xN) * 0.5;
1095 midPoint->y = (yM + yN) * 0.5;
1096 midPoint->z = (zM + zN) * 0.5;
1107 CvStereoLineCoeff* coeffs)
1128 coeffs->Xcoef = -x1 + xA;
1129 coeffs->XcoefA = xB + x1 - xA;
1130 coeffs->XcoefB = -xA - gamma * x1 + gamma * xA;
1131 coeffs->XcoefAB = -xB + xA + gamma * xB - gamma * xA;
1133 coeffs->Ycoef = -y1 + yA;
1134 coeffs->YcoefA = yB + y1 - yA;
1135 coeffs->YcoefB = -yA - gamma * y1 + gamma * yA;
1136 coeffs->YcoefAB = -yB + yA + gamma * yB - gamma * yA;
1138 coeffs->Zcoef = -z1 + zA;
1139 coeffs->ZcoefA = zB + z1 - zA;
1140 coeffs->ZcoefB = -zA - gamma * z1 + gamma * zA;
1141 coeffs->ZcoefAB = -zB + zA + gamma * zB - gamma * zA;
1146 coeffs->Xcoef = -(-x1 + xA);
1147 coeffs->XcoefB = -(xB + x1 - xA);
1148 coeffs->XcoefA = -(-xA - gamma * x1 + gamma * xA);
1149 coeffs->XcoefAB = -(-xB + xA + gamma * xB - gamma * xA);
1151 coeffs->Ycoef = -(-y1 + yA);
1152 coeffs->YcoefB = -(yB + y1 - yA);
1153 coeffs->YcoefA = -(-yA - gamma * y1 + gamma * yA);
1154 coeffs->YcoefAB = -(-yB + yA + gamma * yB - gamma * yA);
1156 coeffs->Zcoef = -(-z1 + zA);
1157 coeffs->ZcoefB = -(zB + z1 - zA);
1158 coeffs->ZcoefA = -(-zA - gamma * z1 + gamma * zA);
1159 coeffs->ZcoefAB = -(-zB + zA + gamma * zB - gamma * zA);
1183 pb.x = imageSize.width - 1;
1186 pd.x = imageSize.width - 1;
1187 pd.y = imageSize.height - 1;
1190 pc.y = imageSize.height - 1;
1195 if (startPoint.x < 0)
1198 if (startPoint.y < 0)
1204 else if (startPoint.y > imageSize.height - 1)
1217 else if (startPoint.x > imageSize.width - 1)
1220 if (startPoint.y < 0)
1226 else if (startPoint.y > imageSize.height - 1)
1242 if (startPoint.y < 0)
1245 if (startPoint.x < imageSize.width / 2)
1256 else if (startPoint.y > imageSize.height - 1)
1259 if (startPoint.x < imageSize.width / 2)
1282 double*
a,
double* b,
double*
c,
1286 double detA, detB, detC;
1288 det = p_start.x * p_end.y + p_end.x + p_start.y - p_end.y - p_start.y * p_end.x - p_start.x;
1295 detA = p_start.y - p_end.y;
1296 detB = p_end.x - p_start.x;
1297 detC = p_start.x * p_end.y - p_end.x * p_start.y;
1299 double invDet = 1.0 / det;
1310 #define icvTransformVector_64d( matr, src, dst, w, h ) \
1311 icvMulMatrix_64d( matr, w, h, src, 1, w, dst )
1314 double a,
double b,
double c,
1319 static void icvGetCommonArea(CvSize imageSize,
1342 double transFundMatr[3 * 3];
1349 if (fabs(epipole1.z) < 1e-8)
1355 epipole1_2d.x = epipole1.x / epipole1.z;
1356 epipole1_2d.y = epipole1.y / epipole1.z;
1358 if (fabs(epipole2.z) < 1e-8)
1364 epipole2_2d.x = epipole2.x / epipole2.z;
1365 epipole2_2d.y = epipole2.y / epipole2.z;
1367 int stat =
icvGetAngleLine(epipole1_2d, imageSize, &point11, &point12);
1387 pointW11[0] = point11.x;
1388 pointW11[1] = point11.y;
1400 corr21[0], corr21[1], corr21[2],
1409 pointW21[0] = point21.x;
1410 pointW21[1] = point21.y;
1426 coeff11[0] = corr11[0];
1427 coeff11[1] = corr11[1];
1428 coeff11[2] = corr11[2];
1432 &coeff21[0], &coeff21[1], &coeff21[2],
1445 &coeff11[0], &coeff11[1], &coeff11[2],
1454 coeff21[0] = corr21[0];
1455 coeff21[1] = corr21[1];
1456 coeff21[2] = corr21[2];
1464 pointW12[0] = point12.x;
1465 pointW12[1] = point12.y;
1475 corr22[0], corr22[1], corr22[2],
1484 pointW22[0] = point22.x;
1485 pointW22[1] = point22.y;
1502 coeff12[0] = corr12[0];
1503 coeff12[1] = corr12[1];
1504 coeff12[2] = corr12[2];
1508 &coeff22[0], &coeff22[1], &coeff22[2],
1521 &coeff12[0], &coeff12[1], &coeff12[2],
1530 coeff22[0] = corr22[0];
1531 coeff22[1] = corr22[1];
1532 coeff22[2] = corr22[2];
1552 double det = direct1[0] * direct2[1] - direct2[0] * direct1[1];
1553 double detx = -direct1[2] * direct2[1] + direct1[1] * direct2[2];
1558 cross->x = detx / det;
1559 cross->y = (-direct1[0] * direct2[2] + direct2[0] * direct1[2]) / det;
1589 double a,
double b,
double c,
1593 if ((
a * p_start.x + b * p_start.y +
c) * (
a * p_end.x + b * p_end.y +
c) <= 0)
1597 double detxc, detyc;
1599 det =
a * (p_end.x - p_start.x) + b * (p_end.y - p_start.y);
1604 if (fabs(
a * p_start.x + b * p_start.y +
c) <
EPS64D)
1617 detxc = b * (p_end.y * p_start.x - p_start.y * p_end.x) +
c * (p_start.x - p_end.x);
1618 detyc =
a * (p_end.x * p_start.y - p_start.x * p_end.y) +
c * (p_start.y - p_end.y);
1620 cross->x = detxc / det;
1621 cross->y = detyc / det;
1638 double ex1, ey1, ex2, ey2;
1639 double px1, py1, px2, py2;
1641 double delA, delB, delX, delY;
1642 double alpha, betta;
1654 del = (py1 - py2) * (ex1 - ex2) - (px1 - px2) * (ey1 - ey2);
1662 delA = (ey1 - ey2) * (ex1 - px1) + (ex1 - ex2) * (py1 - ey1);
1663 delB = (py1 - py2) * (ex1 - px1) + (px1 - px2) * (py1 - ey1);
1668 if (alpha < 0 || alpha > 1.0 || betta < 0 || betta > 1.0)
1674 delX = (px1 - px2) * (ey1 * (ex1 - ex2) - ex1 * (ey1 - ey2)) +
1675 (ex1 - ex2) * (px1 * (py1 - py2) - py1 * (px1 - px2));
1677 delY = (py1 - py2) * (ey1 * (ex1 - ex2) - ex1 * (ey1 - ey2)) +
1678 (ey1 - ey2) * (px1 * (py1 - py2) - py1 * (px1 - px2));
1680 cross->x = delX / del;
1681 cross->y = delY / del;
1692 double dx = point2.x - point1.x;
1693 double dy = point2.y - point1.y;
1694 *dist =
sqrt(dx * dx + dy * dy);
1702 double dx = point2.x - point1.x;
1703 double dy = point2.y - point1.y;
1704 double dz = point2.z - point1.z;
1705 *dist =
sqrt(dx * dx + dy * dy + dz * dz);
1714 double a,
double b,
double c,
1730 frameEnd.x = imageSize.width;
1735 frameBeg.x = imageSize.width;
1737 frameEnd.x = imageSize.width;
1738 frameEnd.y = imageSize.height;
1741 frameBeg.x = imageSize.width;
1742 frameBeg.y = imageSize.height;
1744 frameEnd.y = imageSize.height;
1748 frameBeg.y = imageSize.height;
1755 int maxI = 0, maxJ = 0;
1764 for (i = 0; i < 3; i++)
1766 if (haveCross[i] == 1)
1768 for (j = i + 1; j < 4; j++)
1770 if (haveCross[j] == 1)
1788 *start =
cross[maxI];
1827 projPoint->x = tmpVect1[0] / tmpVect1[2];
1828 projPoint->y = tmpVect1[1] / tmpVect1[2];
1833 static int icvCvt_64d_32f(
double* src,
float* dst,
int size);
1845 CvPoint2D32f point1,
1846 CvPoint2D32f* point2);
1851 CvPoint2D32f* point1,
1852 CvPoint2D32f point2);
1878 double convRotMatr[9];
1879 double convTransVect[3];
1887 float convRotMatr_32f[9];
1888 float convTransVect_32f[3];
1890 icvCvt_64d_32f(convRotMatr, convRotMatr_32f, 9);
1891 icvCvt_64d_32f(convTransVect, convTransVect_32f, 3);
1896 float fundMatr_32f[9];
1898 float camMatr1_32f[9];
1899 float camMatr2_32f[9];
1901 icvCvt_64d_32f(camMatr1, camMatr1_32f, 9);
1902 icvCvt_64d_32f(camMatr2, camMatr2_32f, 9);
1913 CvPoint3D32f epipole1_32f;
1914 CvPoint3D32f epipole2_32f;
1920 epipole1->x = epipole1_32f.x;
1921 epipole1->y = epipole1_32f.y;
1922 epipole1->z = epipole1_32f.z;
1924 epipole2->x = epipole2_32f.x;
1925 epipole2->y = epipole2_32f.y;
1926 epipole2->z = epipole2_32f.z;
1929 icvCvt_32f_64d(fundMatr_32f, fundMatr, 9);
1937 icvGetCommonArea(imageSize,
1938 *epipole1, *epipole2,
1945 double width1, width2;
1946 double height1, height2;
1947 double tmpHeight1, tmpHeight2;
1953 if (fabs(epipole1->z) < 1e-8)
1957 epipole1_2d.x = epipole1->x / epipole1->z;
1958 epipole1_2d.y = epipole1->y / epipole1->z;
1971 height1 = MAX(tmpHeight1, tmpHeight2);
1973 quad1[0][0] = point11.x;
1974 quad1[0][1] = point11.y;
1976 quad1[1][0] = point21.x;
1977 quad1[1][1] = point21.y;
1979 quad1[2][0] = point22.x;
1980 quad1[2][1] = point22.y;
1982 quad1[3][0] = point12.x;
1983 quad1[3][1] = point12.y;
1986 if (fabs(epipole2->z) < 1e-8)
1990 epipole2_2d.x = epipole2->x / epipole2->z;
1991 epipole2_2d.y = epipole2->y / epipole2->z;
2004 height2 = MAX(tmpHeight1, tmpHeight2);
2006 quad2[0][0] = point11.x;
2007 quad2[0][1] = point11.y;
2009 quad2[1][0] = point21.x;
2010 quad2[1][1] = point21.y;
2012 quad2[2][0] = point22.x;
2013 quad2[2][1] = point22.y;
2015 quad2[3][0] = point12.x;
2016 quad2[3][1] = point12.y;
2023 double convRotMatr[9];
2024 double convTransVect[3];
2026 double newQuad1[4][2];
2027 double newQuad2[4][2];
2038 CvPoint2D32f pointb1;
2039 CvPoint2D32f pointe1;
2041 CvPoint2D32f pointb2;
2042 CvPoint2D32f pointe2;
2044 pointb1.x = (
float)quad1[0][0];
2045 pointb1.y = (
float)quad1[0][1];
2047 pointe1.x = (
float)quad1[3][0];
2048 pointe1.y = (
float)quad1[3][1];
2065 double dxOld, dyOld;
2066 double dxNew, dyNew;
2067 double distOld, distNew;
2069 dxOld = quad2[1][0] - quad2[0][0];
2070 dyOld = quad2[1][1] - quad2[0][1];
2071 distOld = dxOld * dxOld + dyOld * dyOld;
2073 dxNew = quad2[1][0] - pointb2.x;
2074 dyNew = quad2[1][1] - pointb2.y;
2075 distNew = dxNew * dxNew + dyNew * dyNew;
2077 if (distNew > distOld)
2080 newQuad2[0][0] = pointb2.x;
2081 newQuad2[0][1] = pointb2.y;
2082 newQuad2[3][0] = pointe2.x;
2083 newQuad2[3][1] = pointe2.y;
2084 newQuad1[0][0] = quad1[0][0];
2085 newQuad1[0][1] = quad1[0][1];
2086 newQuad1[3][0] = quad1[3][0];
2087 newQuad1[3][1] = quad1[3][1];
2093 pointb2.x = (
float)quad2[0][0];
2094 pointb2.y = (
float)quad2[0][1];
2096 pointe2.x = (
float)quad2[3][0];
2097 pointe2.y = (
float)quad2[3][1];
2114 newQuad2[0][0] = quad2[0][0];
2115 newQuad2[0][1] = quad2[0][1];
2116 newQuad2[3][0] = quad2[3][0];
2117 newQuad2[3][1] = quad2[3][1];
2119 newQuad1[0][0] = pointb1.x;
2120 newQuad1[0][1] = pointb1.y;
2121 newQuad1[3][0] = pointe1.x;
2122 newQuad1[3][1] = pointe1.y;
2126 pointb1.x = (
float)quad1[1][0];
2127 pointb1.y = (
float)quad1[1][1];
2129 pointe1.x = (
float)quad1[2][0];
2130 pointe1.y = (
float)quad1[2][1];
2146 dxOld = quad2[0][0] - quad2[1][0];
2147 dyOld = quad2[0][1] - quad2[1][1];
2148 distOld = dxOld * dxOld + dyOld * dyOld;
2150 dxNew = quad2[0][0] - pointb2.x;
2151 dyNew = quad2[0][1] - pointb2.y;
2152 distNew = dxNew * dxNew + dyNew * dyNew;
2154 if (distNew > distOld)
2157 newQuad2[1][0] = pointb2.x;
2158 newQuad2[1][1] = pointb2.y;
2159 newQuad2[2][0] = pointe2.x;
2160 newQuad2[2][1] = pointe2.y;
2161 newQuad1[1][0] = quad1[1][0];
2162 newQuad1[1][1] = quad1[1][1];
2163 newQuad1[2][0] = quad1[2][0];
2164 newQuad1[2][1] = quad1[2][1];
2170 pointb2.x = (
float)quad2[1][0];
2171 pointb2.y = (
float)quad2[1][1];
2173 pointe2.x = (
float)quad2[2][0];
2174 pointe2.y = (
float)quad2[2][1];
2188 newQuad2[1][0] = quad2[1][0];
2189 newQuad2[1][1] = quad2[1][1];
2190 newQuad2[2][0] = quad2[2][0];
2191 newQuad2[2][1] = quad2[2][1];
2193 newQuad1[1][0] = pointb1.x;
2194 newQuad1[1][1] = pointb1.y;
2195 newQuad1[2][0] = pointe1.x;
2196 newQuad1[2][1] = pointe1.y;
2205 for (i = 0; i < 4; i++)
2208 quad1[i][0] = newQuad1[i][0];
2209 quad1[i][1] = newQuad1[i][1];
2210 quad2[i][0] = newQuad2[i][0];
2211 quad2[i][1] = newQuad2[i][1];
2217 double warpWidth, warpHeight;
2219 warpWidth = MAX(width1, width2);
2220 warpHeight = MAX(height1, height2);
2222 warpSize->width = (int)warpWidth;
2223 warpSize->height = (int)warpHeight;
2225 warpSize->width = cvRound(warpWidth - 1);
2226 warpSize->height = cvRound(warpHeight - 1);
2229 warpSize->width = imageSize.width;
2230 warpSize->height = imageSize.height;
2238 static void icvGetQuadsTransformNew(CvSize imageSize,
2247 CvPoint3D32f* epipole1,
2248 CvPoint3D32f* epipole2
2253 double camMatr1_64d[9];
2254 double camMatr2_64d[9];
2255 double rotMatr1_64d[9];
2256 double transVect1_64d[3];
2257 double rotMatr2_64d[9];
2258 double transVect2_64d[3];
2259 double fundMatr_64d[9];
2263 icvCvt_32f_64d(camMatr1, camMatr1_64d, 9);
2264 icvCvt_32f_64d(camMatr2, camMatr2_64d, 9);
2265 icvCvt_32f_64d(rotMatr1, rotMatr1_64d, 9);
2266 icvCvt_32f_64d(transVect1, transVect1_64d, 3);
2270 rotMatr2_64d[0] = 1;
2271 rotMatr2_64d[1] = 0;
2272 rotMatr2_64d[2] = 0;
2273 rotMatr2_64d[3] = 0;
2274 rotMatr2_64d[4] = 1;
2275 rotMatr2_64d[5] = 0;
2276 rotMatr2_64d[6] = 0;
2277 rotMatr2_64d[7] = 0;
2278 rotMatr2_64d[8] = 1;
2280 transVect2_64d[0] = 0;
2281 transVect2_64d[1] = 0;
2282 transVect2_64d[2] = 0;
2300 epipole1->x = (
float)(epipole1_64d.x);
2301 epipole1->y = (
float)(epipole1_64d.y);
2302 epipole1->z = (
float)(epipole1_64d.z);
2304 epipole2->x = (
float)(epipole2_64d.x);
2305 epipole2->y = (
float)(epipole2_64d.y);
2306 epipole2->z = (
float)(epipole2_64d.z);
2309 icvCvt_64d_32f(fundMatr_64d, fundMatr, 9);
2323 icvGetQuadsTransformNew(cvSize(cvRound(stereoCamera->camera[0]->imgSize[0]), cvRound(stereoCamera->camera[0]->imgSize[1])),
2324 stereoCamera->camera[0]->matrix,
2325 stereoCamera->camera[1]->matrix,
2326 stereoCamera->rotMatrix,
2327 stereoCamera->transVector,
2328 &(stereoCamera->warpSize),
2331 stereoCamera->fundMatr,
2332 &(stereoCamera->epipole[0]),
2333 &(stereoCamera->epipole[1])
2337 for (i = 0; i < 4; i++)
2339 stereoCamera->quad[0][i] = cvPoint2D32f(quad1[i][0], quad1[i][1]);
2340 stereoCamera->quad[1][i] = cvPoint2D32f(quad2[i][0], quad2[i][1]);
2360 double transVect1[3];
2361 double transVect2[3];
2363 double convRotMatr[9];
2364 double convTransVect[3];
2367 icvCvt_32f_64d(stereoCamera->camera[0]->rotMatr, rotMatr1, 9);
2368 icvCvt_32f_64d(stereoCamera->camera[1]->rotMatr, rotMatr2, 9);
2370 icvCvt_32f_64d(stereoCamera->camera[0]->transVect, transVect1, 3);
2371 icvCvt_32f_64d(stereoCamera->camera[1]->transVect, transVect2, 3);
2381 icvCvt_64d_32f(convRotMatr, stereoCamera->rotMatrix, 9);
2382 icvCvt_64d_32f(convTransVect, stereoCamera->transVector, 3);
2399 normDirect[0] = direct[1];
2400 normDirect[1] = - direct[0];
2401 normDirect[2] = -(normDirect[0] * point.x + normDirect[1] * point.y);
2429 double midLine[3] = {0, 0, 0};
2434 pointOnLine1.x = pointOnLine1.y = 0;
2436 pointOnLine2.x = pointOnLine2.y = 0;
2441 areaLineCoef1[0], areaLineCoef1[1], areaLineCoef1[2],
2442 &start1, &end1, &res);
2445 pointOnLine1 = start1;
2449 areaLineCoef2[0], areaLineCoef2[1], areaLineCoef2[2],
2450 &start1, &end1, &res);
2453 pointOnLine2 = start1;
2466 icvTestPoint(cornerPoint, areaLineCoef1, areaLineCoef2, epipole, &res);
2470 candPoints[numPoints] = cornerPoint;
2474 cornerPoint.x = imageSize.width;
2476 icvTestPoint(cornerPoint, areaLineCoef1, areaLineCoef2, epipole, &res);
2480 candPoints[numPoints] = cornerPoint;
2484 cornerPoint.x = imageSize.width;
2485 cornerPoint.y = imageSize.height;
2486 icvTestPoint(cornerPoint, areaLineCoef1, areaLineCoef2, epipole, &res);
2490 candPoints[numPoints] = cornerPoint;
2495 cornerPoint.y = imageSize.height;
2496 icvTestPoint(cornerPoint, areaLineCoef1, areaLineCoef2, epipole, &res);
2500 candPoints[numPoints] = cornerPoint;
2506 areaLineCoef1[0], areaLineCoef1[1], areaLineCoef1[2],
2507 &tmpPoints[0], &tmpPoints[1],
2509 for (i = 0; i < res; i++)
2511 candPoints[numPoints++] = tmpPoints[i];
2516 areaLineCoef2[0], areaLineCoef2[1], areaLineCoef2[2],
2517 &tmpPoints[0], &tmpPoints[1],
2520 for (i = 0; i < res; i++)
2522 candPoints[numPoints++] = tmpPoints[i];
2534 minPoint.x = minPoint.y = FLT_MAX;
2536 maxPoint.x = maxPoint.y = -FLT_MAX;
2541 double minDist = 10000000;
2544 for (i = 0; i < numPoints; i++)
2551 minPoint = projPoint;
2557 maxPoint = projPoint;
2576 if (epipole.x > imageSize.width * 0.5)
2579 tmpPoint2 = *point11;
2580 *point11 = *point21;
2581 *point21 = tmpPoint2;
2583 tmpPoint2 = *point12;
2584 *point12 = *point22;
2585 *point22 = tmpPoint2;
2604 double alpha = dist2 / dist1;
2606 pointNew1.x = basePoint.x + (1.0 / alpha) * (point2.x - basePoint.x);
2607 pointNew1.y = basePoint.y + (1.0 / alpha) * (point2.y - basePoint.y);
2609 pointNew2.x = basePoint.x + alpha * (point1.x - basePoint.x);
2610 pointNew2.y = basePoint.y + alpha * (point1.y - basePoint.y);
2624 return (point1.x - basePoint.x) * (point2.y - basePoint.y) -
2625 (point2.x - basePoint.x) * (point1.y - basePoint.y);
2641 double sign1 =
icvGetVect(basePoint, point1, point2);
2642 double sign2 =
icvGetVect(basePoint, point1, testPoint);
2643 if (sign1 * sign2 > 0)
2647 sign2 =
icvGetVect(basePoint, point2, testPoint);
2648 if (sign1 * sign2 > 0)
2671 double a = lineCoeff[0];
2672 double b = lineCoeff[1];
2674 double det = 1.0 / (
a *
a + b * b);
2675 double delta =
a * point.y - b * point.x;
2677 projectPoint->x = (-
a * lineCoeff[2] - b * delta) * det;
2678 projectPoint->y = (-b * lineCoeff[2] +
a * delta) * det ;
2689 double dx = point.x - tmpPoint.x;
2690 double dy = point.y - tmpPoint.y;
2691 *dist =
sqrt(dx * dx + dy * dy);
2697 int desired_depth,
int desired_num_channels)
2700 src_size.width = src->width;
2701 src_size.height = src->height;
2703 CvSize dst_size = src_size;
2707 dst_size.width = dst->width;
2708 dst_size.height = dst->height;
2711 if (!dst || dst->depth != desired_depth ||
2712 dst->nChannels != desired_num_channels ||
2713 dst_size.width != src_size.width ||
2714 dst_size.height != src_size.height)
2716 cvReleaseImage(&dst);
2717 dst = cvCreateImage(src_size, desired_depth, desired_num_channels);
2718 CvRect rect = cvRect(0, 0, src_size.width, src_size.height);
2719 cvSetImageROI(dst, rect);
2727 icvCvt_32f_64d(
float* src,
double* dst,
int size)
2740 for (t = 0; t < size; t++)
2742 dst[t] = (double)(src[t]);
2751 icvCvt_64d_32f(
double* src,
float* dst,
int size)
2764 for (t = 0; t < size; t++)
2766 dst[t] = (
float)(src[t]);
2775 size.width = mat->cols;
2776 size.height = mat->rows;
2782 #define __BEGIN__ __CV_BEGIN__
2783 #define __END__ __CV_END__
2789 CV_FUNCNAME(
"cvComputePerspectiveMap");
2794 CvMat stubx, *mapx = (CvMat*)rectMapX;
2795 CvMat stuby, *mapy = (CvMat*)rectMapY;
2798 CV_CALL(mapx = cvGetMat(mapx, &stubx));
2799 CV_CALL(mapy = cvGetMat(mapy, &stuby));
2801 if (CV_MAT_TYPE(mapx->type) != CV_32FC1 || CV_MAT_TYPE(mapy->type) != CV_32FC1)
2803 CV_ERROR(CV_StsUnsupportedFormat,
"");
2807 assert(fabs(
c[2][2] - 1.) < FLT_EPSILON);
2809 for (i = 0; i < size.height; i++)
2811 float* mx = (
float*)(mapx->data.ptr + mapx->step * i);
2812 float* my = (
float*)(mapy->data.ptr + mapy->step * i);
2814 for (j = 0; j < size.width; j++)
2816 double w = 1. / (
c[2][0] * j +
c[2][1] * i + 1.);
2817 double x = (
c[0][0] * j +
c[0][1] * i +
c[0][2]) * w;
2818 double y = (
c[1][0] * j +
c[1][1] * i +
c[1][2]) * w;
2835 CV_FUNCNAME(
"cvInitPerspectiveTransform");
2843 CvMat mapstub, *map = (CvMat*)rectMap;
2848 CV_CALL(map = cvGetMat(map, &mapstub));
2850 if (CV_MAT_TYPE(map->type) != CV_32FC2)
2852 CV_ERROR(CV_StsUnsupportedFormat,
"");
2855 if (map->width != size.width || map->height != size.height)
2857 CV_ERROR(CV_StsUnmatchedSizes,
"");
2861 pt[0] = cvPoint2D32f(0, 0);
2862 pt[1] = cvPoint2D32f(size.width, 0);
2863 pt[2] = cvPoint2D32f(size.width, size.height);
2864 pt[3] = cvPoint2D32f(0, size.height);
2866 for (i = 0; i < 4; i++)
2869 double x = quad[i].x;
2870 double y = quad[i].y;
2876 double X = quad[i].x;
2877 double Y = quad[i].y;
2879 double*
a =
A + i * 16;
2907 CvMat matA = cvMat(8, 8, CV_64F,
A);
2908 CvMat matInvA = cvMat(8, 8, CV_64F, invA);
2909 CvMat matB = cvMat(8, 1, CV_64F, b);
2910 CvMat matX = cvMat(8, 1, CV_64F,
c);
2913 CV_CALL(cvMatMulAdd(&matInvA, &matB, 0, &matX));
2916 matrix[0][0] =
c[0];
2917 matrix[0][1] =
c[1];
2918 matrix[0][2] =
c[2];
2919 matrix[1][0] =
c[3];
2920 matrix[1][1] =
c[4];
2921 matrix[1][2] =
c[5];
2922 matrix[2][0] =
c[6];
2923 matrix[2][1] =
c[7];
2928 for (i = 0; i < size.height; i++)
2930 CvPoint2D32f* maprow = (CvPoint2D32f*)(map->data.ptr + map->step * i);
2931 for (j = 0; j < size.width; j++)
2933 double w = 1. / (
c[6] * j +
c[7] * i + 1.);
2934 double x = (
c[0] * j +
c[1] * i +
c[2]) * w;
2935 double y = (
c[3] * j +
c[4] * i +
c[5]) * w;
2937 maprow[j].x = (
float)x;
2938 maprow[j].y = (
float)y;
2954 CvPoint2D32f point1,
2955 CvPoint2D32f* point2)
2961 p1[0] = (double)(point1.x);
2962 p1[1] = (double)(point1.y);
2991 point2->x = (
float)(projP[0] / projP[2]);
2992 point2->y = (
float)(projP[1] / projP[2]);
3002 CvPoint2D32f* point1,
3003 CvPoint2D32f point2)
3009 p2[0] = (double)(point2.x);
3010 p2[1] = (double)(point2.y);
3037 point1->x = (
float)(projP[0] / projP[2]);
3038 point1->y = (
float)(projP[1] / projP[2]);
3046 double* _rotation_matrix,
double* _translation_vector,
3047 double* _camera_matrix,
double* _distortion, CvPoint2D64f* _image_points)
3049 CvMat object_points = cvMat(point_count, 1, CV_64FC3, _object_points);
3050 CvMat image_points = cvMat(point_count, 1, CV_64FC2, _image_points);
3051 CvMat rotation_matrix = cvMat(3, 3, CV_64FC1, _rotation_matrix);
3052 CvMat translation_vector = cvMat(3, 1, CV_64FC1, _translation_vector);
3053 CvMat camera_matrix = cvMat(3, 3, CV_64FC1, _camera_matrix);
3054 CvMat dist_coeffs = cvMat(4, 1, CV_64FC1, _distortion);
3056 cvProjectPoints2(&object_points, &rotation_matrix, &translation_vector,
3057 &camera_matrix, &dist_coeffs, &image_points,
3063 static int icvSelectBestRt(
int numImages,
3065 CvPoint2D32f* imagePoints1,
3066 CvPoint2D32f* imagePoints2,
3067 CvPoint3D32f* objectPoints,
3087 double* rotMatrs1_64d;
3088 double* rotMatrs2_64d;
3090 double* transVects1_64d;
3091 double* transVects2_64d;
3093 double cameraMatrix1_64d[9];
3094 double cameraMatrix2_64d[9];
3096 double distortion1_64d[4];
3097 double distortion2_64d[4];
3102 for (
int i = 0; i < numImages; i++)
3104 totalNum += numPoints[i];
3109 rotMatrs1_64d = (
double*)calloc(numImages,
sizeof(
double) * 9);
3110 rotMatrs2_64d = (
double*)calloc(numImages,
sizeof(
double) * 9);
3112 transVects1_64d = (
double*)calloc(numImages,
sizeof(
double) * 3);
3113 transVects2_64d = (
double*)calloc(numImages,
sizeof(
double) * 3);
3117 icvCvt_32f_64d((
float*)objectPoints, (
double*)objectPoints_64d, totalNum * 3);
3119 icvCvt_32f_64d(rotMatrs1, rotMatrs1_64d, numImages * 9);
3120 icvCvt_32f_64d(rotMatrs2, rotMatrs2_64d, numImages * 9);
3122 icvCvt_32f_64d(transVects1, transVects1_64d, numImages * 3);
3123 icvCvt_32f_64d(transVects2, transVects2_64d, numImages * 3);
3126 icvCvt_32f_64d(cameraMatrix1, cameraMatrix1_64d, 9);
3127 icvCvt_32f_64d(cameraMatrix2, cameraMatrix2_64d, 9);
3129 icvCvt_32f_64d(distortion1, distortion1_64d, 4);
3130 icvCvt_32f_64d(distortion2, distortion2_64d, 4);
3136 errors = (
float*)calloc(numImages * numImages,
sizeof(
float));
3144 for (currRt = 0; currRt < numImages; currRt++)
3147 for (currImagePair = 0; currImagePair < numImages; currImagePair++)
3151 double convRotMatr[9];
3152 double convTransVect[3];
3155 transVects1_64d + currRt * 3,
3156 rotMatrs2_64d + currRt * 9,
3157 transVects2_64d + currRt * 3,
3163 double convRotMatr2[9];
3164 double convTransVect2[3];
3166 convRotMatr2[0] = 1;
3167 convRotMatr2[1] = 0;
3168 convRotMatr2[2] = 0;
3170 convRotMatr2[3] = 0;
3171 convRotMatr2[4] = 1;
3172 convRotMatr2[5] = 0;
3174 convRotMatr2[6] = 0;
3175 convRotMatr2[7] = 0;
3176 convRotMatr2[8] = 1;
3178 convTransVect2[0] = 0;
3179 convTransVect2[1] = 0;
3180 convTransVect2[2] = 0;
3192 numberPnt = numPoints[currImagePair];
3200 for (
int i = 0; i < numberPnt; i++)
3204 tmpPoint.x = (double)(objectPoints[i].x);
3205 tmpPoint.y = (double)(objectPoints[i].y);
3206 tmpPoint.z = (double)(objectPoints[i].z);
3210 rotMatrs2_64d + currImagePair * 9,
3211 transVects2_64d + currImagePair * 3);
3222 rotMatrs1_64d + currImagePair * 9,
3223 transVects1_64d + currImagePair * 3);
3250 for (currPoint = 0; currPoint < numberPnt; currPoint++)
3254 dx1 = imagePoints1[begPoint + currPoint].x - projImagePoints1[currPoint].x;
3255 dy1 = imagePoints1[begPoint + currPoint].y - projImagePoints1[currPoint].y;
3256 len1 =
sqrt(dx1 * dx1 + dy1 * dy1);
3260 dx2 = imagePoints2[begPoint + currPoint].x - projImagePoints2[currPoint].x;
3261 dy2 = imagePoints2[begPoint + currPoint].y - projImagePoints2[currPoint].y;
3262 len2 =
sqrt(dx2 * dx2 + dy2 * dy2);
3266 err1 /= (
float)(numberPnt);
3267 err2 /= (
float)(numberPnt);
3269 err = (err1 + err2) * 0.5;
3270 begPoint += numberPnt;
3273 errors[numImages * currImagePair + currRt] = (
float)err;
3277 free(projImagePoints1);
3278 free(projImagePoints2);
3287 for (currRt = 0; currRt < numImages; currRt++)
3290 for (currImagePair = 0; currImagePair < numImages; currImagePair++)
3292 avErr += errors[numImages * currImagePair + currRt];
3294 avErr /= (
float)(numImages);
3304 if (avErr < minError)
3313 double bestRotMatr_64d[9];
3314 double bestTransVect_64d[3];
3317 transVects1_64d + bestnumRt * 3,
3318 rotMatrs2_64d + bestnumRt * 9,
3319 transVects2_64d + bestnumRt * 3,
3323 icvCvt_64d_32f(bestRotMatr_64d, bestRotMatr, 9);
3324 icvCvt_64d_32f(bestTransVect_64d, bestTransVect, 3);
3337 float ax = point2.x - point1.x;
3338 float ay = point2.y - point1.y;
3340 float bx = point.x - point1.x;
3341 float by = point.y - point1.y;
3343 return (ax * by - ay * bx);
3348 CvPoint2D32f* cameraPoint,
3349 CvPoint2D32f* warpPoint,
3360 det = (coeffs[2][0] * x + coeffs[2][1] * y + coeffs[2][2]);
3361 if (fabs(det) > 1e-8)
3363 cameraPoint->x = (
float)((coeffs[0][0] * x + coeffs[0][1] * y + coeffs[0][2]) / det);
3364 cameraPoint->y = (
float)((coeffs[1][0] * x + coeffs[1][1] * y + coeffs[1][2]) / det);
3374 det = (coeffs[2][0] * x - coeffs[0][0]) * (coeffs[2][1] * y - coeffs[1][1]) - (coeffs[2][1] * x - coeffs[0][1]) * (coeffs[2][0] * y - coeffs[1][0]);
3376 if (fabs(det) > 1e-8)
3378 warpPoint->x = (
float)(((coeffs[0][2] - coeffs[2][2] * x) * (coeffs[2][1] * y - coeffs[1][1]) - (coeffs[2][1] * x - coeffs[0][1]) * (coeffs[1][2] - coeffs[2][2] * y)) / det);
3379 warpPoint->y = (
float)(((coeffs[2][0] * x - coeffs[0][0]) * (coeffs[1][2] - coeffs[2][2] * y) - (coeffs[0][2] - coeffs[2][2] * x) * (coeffs[2][0] * y - coeffs[1][0])) / det);
3396 stereoparams->quad[0],
3397 stereoparams->coeffs[0],
3401 stereoparams->quad[1],
3402 stereoparams->coeffs[1],
3406 CvPoint2D32f corns[4];
3410 corns[1].x = (
float)(stereoparams->camera[0]->imgSize[0] - 1);
3413 corns[2].x = (
float)(stereoparams->camera[0]->imgSize[0] - 1);
3414 corns[2].y = (
float)(stereoparams->camera[0]->imgSize[1] - 1);
3417 corns[3].y = (
float)(stereoparams->camera[0]->imgSize[1] - 1);
3419 for (
int i = 0; i < 4; i++)
3424 stereoparams->border[0] + i,
3430 stereoparams->border[1] + i,
3436 CvPoint2D32f warpPoints[4];
3437 warpPoints[0] = cvPoint2D32f(0, 0);
3438 warpPoints[1] = cvPoint2D32f(stereoparams->warpSize.width - 1, 0);
3439 warpPoints[2] = cvPoint2D32f(stereoparams->warpSize.width - 1, stereoparams->warpSize.height - 1);
3440 warpPoints[3] = cvPoint2D32f(0, stereoparams->warpSize.height - 1);
3442 CvPoint2D32f camPoints1[4];
3443 CvPoint2D32f camPoints2[4];
3445 for (
int i = 0; i < 4; i++)
3462 stereoparams->lineCoeffs = (CvStereoLineCoeff*)calloc(stereoparams->warpSize.height,
sizeof(CvStereoLineCoeff));
3477 CvPoint2D32f* imagePoints1,
3478 CvPoint2D32f* imagePoints2,
3479 CvPoint3D32f* objectPoints,
3480 CvStereoCamera* stereoparams
3491 transVects1 = (
float*)calloc(numImages,
sizeof(
float) * 3);
3492 transVects2 = (
float*)calloc(numImages,
sizeof(
float) * 3);
3494 rotMatrs1 = (
float*)calloc(numImages,
sizeof(
float) * 9);
3495 rotMatrs2 = (
float*)calloc(numImages,
sizeof(
float) * 9);
3498 cvCalibrateCamera(numImages,
3503 stereoparams->camera[0]->distortion,
3504 stereoparams->camera[0]->matrix,
3510 cvCalibrateCamera(numImages,
3515 stereoparams->camera[1]->distortion,
3516 stereoparams->camera[1]->matrix,
3523 stereoparams->camera[0]->imgSize[0] = (
float)imageSize.width;
3524 stereoparams->camera[0]->imgSize[1] = (
float)imageSize.height;
3526 stereoparams->camera[1]->imgSize[0] = (
float)imageSize.width;
3527 stereoparams->camera[1]->imgSize[1] = (
float)imageSize.height;
3529 icvSelectBestRt(numImages,
3534 stereoparams->camera[0]->matrix,
3535 stereoparams->camera[0]->distortion,
3538 stereoparams->camera[1]->matrix,
3539 stereoparams->camera[1]->distortion,
3542 stereoparams->rotMatrix,
3543 stereoparams->transVector
3559 double ex1 = p1_start.x;
3560 double ey1 = p1_start.y;
3561 double ex2 = p1_end.x;
3562 double ey2 = p1_end.y;
3564 double px1 = v2_start.x;
3565 double py1 = v2_start.y;
3566 double px2 = v2_end.x;
3567 double py2 = v2_end.y;
3569 double del = (ex1 - ex2) * (py2 - py1) + (ey2 - ey1) * (px2 - px1);
3575 double delA = (px1 - ex1) * (py1 - py2) + (ey1 - py1) * (px1 - px2);
3578 double alpha = delA / del;
3581 if (alpha < 0 || alpha > 1.0)
3586 double delX = (ex1 - ex2) * (py1 * (px1 - px2) - px1 * (py1 - py2)) +
3587 (px1 - px2) * (ex1 * (ey1 - ey2) - ey1 * (ex1 - ex2));
3589 double delY = (ey1 - ey2) * (px1 * (py1 - py2) - py1 * (px1 - px2)) +
3590 (py1 - py2) * (ey1 * (ex1 - ex2) - ex1 * (ey1 - ey2));
3601 double delX, delY, delA;
3603 double px1, px2, py1, py2;
3612 del =
a * (px2 - px1) + b * (py2 - py1);
3618 delA = -
c -
a * px1 - b * py1;
3621 if (alpha < 0 || alpha > 1.0)
3626 delX = b * (py1 * (px1 - px2) - px1 * (py1 - py2)) +
c * (px1 - px2);
3627 delY =
a * (px1 * (py1 - py2) - py1 * (px1 - px2)) +
c * (py1 - py2);
3640 CvPoint3D32f* epipole1,
3641 CvPoint3D32f* epipole2)
3644 CvMat fundMatrC = cvMat(3, 3,
CV_MAT32F, fundMatr);
3646 CvMat* matrW = cvCreateMat(3, 3,
CV_MAT32F);
3647 CvMat* matrU = cvCreateMat(3, 3,
CV_MAT32F);
3648 CvMat* matrV = cvCreateMat(3, 3,
CV_MAT32F);
3652 cvSVD(&fundMatrC, matrW, matrU, matrV, CV_SVD_V_T | CV_SVD_U_T);
3655 epipole1->x = matrU->data.fl[6];
3656 epipole1->y = matrU->data.fl[7];
3657 epipole1->z = matrU->data.fl[8];
3660 epipole2->x = matrV->data.fl[6];
3661 epipole2->y = matrV->data.fl[7];
3662 epipole2->z = matrV->data.fl[8];
3664 cvReleaseMat(&matrW);
3665 cvReleaseMat(&matrU);
3666 cvReleaseMat(&matrV);
3677 CvMat essMatrC = cvMat(3, 3,
CV_MAT32F, essMatr);
3678 CvMat fundMatrC = cvMat(3, 3,
CV_MAT32F, fundMatr);
3679 CvMat cameraMatr1C = cvMat(3, 3,
CV_MAT32F, cameraMatr1);
3680 CvMat cameraMatr2C = cvMat(3, 3,
CV_MAT32F, cameraMatr2);
3682 CvMat* invCM2 = cvCreateMat(3, 3,
CV_MAT32F);
3683 CvMat* tmpMatr = cvCreateMat(3, 3,
CV_MAT32F);
3684 CvMat* invCM1T = cvCreateMat(3, 3,
CV_MAT32F);
3686 cvTranspose(&cameraMatr1C, tmpMatr);
3687 cvInvert(tmpMatr, invCM1T);
3688 cvmMul(invCM1T, &essMatrC, tmpMatr);
3689 cvInvert(&cameraMatr2C, invCM2);
3690 cvmMul(tmpMatr, invCM2, &fundMatrC);
3694 scale = 1.0 / fundMatrC.data.fl[8];
3695 cvConvertScale(&fundMatrC, &fundMatrC, scale);
3697 cvReleaseMat(&invCM2);
3698 cvReleaseMat(&tmpMatr);
3699 cvReleaseMat(&invCM1T);
3714 transMatr[1] = - transVect[2];
3715 transMatr[2] = transVect[1];
3717 transMatr[3] = transVect[2];
3719 transMatr[5] = - transVect[0];
3721 transMatr[6] = - transVect[1];
3722 transMatr[7] = transVect[0];
3748 CvPoint3D32f* buffer =
3751 float* rotMatr = (
float*)cvAlloc(n * 9 *
sizeof(rotMatr[0]));
3752 float* transVect = (
float*)cvAlloc(n * 3 *
sizeof(transVect[0]));
3753 int* counts = (
int*)cvAlloc(n *
sizeof(counts[0]));
3755 cvInitMatHeader(&mat, 1,
sizeof(CvCamera) /
sizeof(
float), CV_32FC1, 0);
3772 transVect, rotMatr, 0);
3783 memcpy(
cameraParams[i].rotMatr, rotMatr, 9 *
sizeof(rotMatr[0]));
3784 memcpy(
cameraParams[i].transVect, transVect, 3 *
sizeof(transVect[0]));
3790 if (!cvCheckArr(&mat, CV_CHECK_RANGE | CV_CHECK_QUIET, -10000, 10000))
3832 CvMemStorage*, CvSize pattern_size,
3833 CvPoint2D32f* corners,
int* corner_count)
3835 return cvFindChessboardCorners(arr, pattern_size, corners,
3836 corner_count, CV_CALIB_CB_ADAPTIVE_THRESH);
3857 cvAlloc(tempPointCount0 * 2 *
sizeof(
latestPoints[0]));
3863 int tempPointCount = tempPointCount0;
3866 if (!CV_IS_MAT(mats[i]) && !CV_IS_IMAGE(mats[i]))
3872 size = cvGetSize(mats[i]);
3874 if (size.width !=
imgSize.width || size.height !=
imgSize.height)
3890 storage = cvCreateMemStorage();
3896 if (CV_MAT_CN(cvGetElemType(mats[i])) == 1)
3902 cvCvtColor(mats[i],
grayImg, CV_BGR2GRAY);
3910 cvSize(5, 5), cvSize(-1, -1),
3911 cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, 0.1));
3919 latestCounts[i] = found ? tempPointCount : -tempPointCount;
3920 result = result && found;
3960 CvPoint2D32f* prev =
points[i];
3962 points[i] = (CvPoint2D32f*)cvAlloc(newMaxPoints *
sizeof(prev[0]));
3986 int* count,
bool* found)
3991 !pts || !count || !found)
4027 CvMat dst_stub, *dst;
4030 CvPoint2D32f* pts = 0;
4034 dst = cvGetMat(dstarr[i], &dst_stub);
4036 static const CvScalar line_colors[] =
4038 CvScalar(0, 0, 255),
4039 CvScalar(0, 128, 255),
4040 CvScalar(0, 200, 200),
4041 CvScalar(0, 255, 0),
4042 CvScalar(200, 200, 0),
4043 CvScalar(255, 0, 0),
4044 CvScalar(255, 0, 255)
4047 const int colorCount =
sizeof(line_colors) /
sizeof(line_colors[0]);
4049 CvScalar color = line_colors[0];
4050 CvPoint prev_pt = { 0, 0};
4052 for (j = 0; j < count; j++)
4055 pt.x = cvRound(pts[j].x);
4056 pt.y = cvRound(pts[j].y);
4062 color = line_colors[(j / cvRound(
etalonParams[0])) % colorCount];
4066 color = CV_RGB(0, 255, 0);
4071 cvLine(dst, prev_pt, pt, color, 1, CV_AA);
4075 cvLine(dst, cvPoint(pt.x - r, pt.y - r),
4076 cvPoint(pt.x + r, pt.y + r), color, 1, CV_AA);
4078 cvLine(dst, cvPoint(pt.x - r, pt.y + r),
4079 cvPoint(pt.x + r, pt.y - r), color, 1, CV_AA);
4081 cvCircle(dst, pt, r + 1, color, 1, CV_AA);
4146 cvInitMatHeader(&mat, 1,
cameraCount * (arrSize /
sizeof(
float)),
4148 cvCheckArr(&mat, CV_CHECK_RANGE, -10000, 10000);
4174 for (j = 0; j < (int)(
sizeof(
cameraParams[i]) /
sizeof(
float)); j++)
4176 fprintf(f,
"%15.10f ", ((
float*)(
cameraParams + i))[j]);
4184 for (i = 0; i < 2; i++)
4186 for (j = 0; j < 4; j++)
4188 fprintf(f,
"%15.10f ",
stereo.quad[i][j].x);
4189 fprintf(f,
"%15.10f ",
stereo.quad[i][j].y);
4195 for (i = 0; i < 2; i++)
4197 for (j = 0; j < 9; j++)
4199 fprintf(f,
"%15.10lf ",
stereo.coeffs[i][j / 3][j % 3]);
4226 if (fscanf(f,
"%d", &d) != 1 || d <= 0 || d > 10)
4235 for (j = 0; j < (int)(
sizeof(
cameraParams[i]) /
sizeof(
float)); j++)
4237 int values_read = fscanf(f,
"%f", &((
float*)(
cameraParams + i))[j]);
4238 CV_Assert(values_read == 1);
4246 for (i = 0; i < 2; i++)
4248 for (j = 0; j < 4; j++)
4250 int values_read = fscanf(f,
"%f ", &(
stereo.quad[i][j].x));
4251 CV_Assert(values_read == 1);
4252 values_read = fscanf(f,
"%f ", &(
stereo.quad[i][j].y));
4253 CV_Assert(values_read == 1);
4258 for (i = 0; i < 2; i++)
4260 for (j = 0; j < 9; j++)
4262 int values_read = fscanf(f,
"%lf ", &(
stereo.coeffs[i][j / 3][j % 3]));
4263 CV_Assert(values_read == 1);
4282 return Rectify((CvMat**)srcarr, (CvMat**)dstarr);
4289 if (!srcarr || !dstarr)
4299 if (srcarr[i] && dstarr[i])
4301 IplImage src_stub, *src;
4302 IplImage dst_stub, *dst;
4304 src = cvGetImage(srcarr[i], &src_stub);
4305 dst = cvGetImage(dstarr[i], &dst_stub);
4307 if (src->imageData == dst->imageData)
4312 CV_MAT_CN(
undistImg->type) != src->nChannels)
4315 undistImg = cvCreateMat(src->height, src->width,
4316 CV_8U + (src->nChannels - 1) * 8);
4325 rectMap[i][0]->height != src->height)
4341 if (srcarr[i] != dstarr[i])
4343 cvCopy(srcarr[i], dstarr[i]);
4353 return Undistort((CvMat**)srcarr, (CvMat**)dstarr);
4361 if (!srcarr || !dstarr)
4371 if (srcarr[i] && dstarr[i])
4373 CvMat src_stub, *src;
4374 CvMat dst_stub, *dst;
4376 src = cvGetMat(srcarr[i], &src_stub);
4377 dst = cvGetMat(dstarr[i], &dst_stub);
4379 if (src->data.ptr == dst->data.ptr)
4386 undistImg = cvCreateMat(src->height, src->width, src->type);
4396 CvMat k = cvMat(1, 4, CV_32FC1,
cameraParams[i].distortion);
4403 undistMap[i][0] = cvCreateMat(src->height, src->width, CV_32FC1);
4404 undistMap[i][1] = cvCreateMat(src->height, src->width, CV_32FC1);
4410 cvUndistort2(src, dst, &
A, &k);
4420 if (srcarr[i] != dstarr[i])
4422 cvCopy(srcarr[i], dstarr[i]);