46 #include <opencv2/calib3d/calib3d_c.h>
47 #include <opencv2/core/core_c.h>
68 #pragma GCC diagnostic push
69 #pragma GCC diagnostic ignored "-Wclass-memaccess"
71 #pragma GCC diagnostic pop
100 CvPoint2D32f* _points)
125 if (!params || cvRound(params[0]) != params[0] || params[0] < 3 ||
126 cvRound(params[1]) != params[1] || params[1] < 3 || params[2] <= 0)
132 pointCount = cvRound((params[0] - 1) * (params[1] - 1));
138 if (!_points || pointCount < 4)
172 int etalonWidth = cvRound(params[0]) - 1;
173 int etalonHeight = cvRound(params[1]) - 1;
180 for (y = 0; y < etalonHeight; y++)
181 for (x = 0; x < etalonWidth; x++)
184 cvPoint2D32f((etalonWidth - 1 - x) * params[2], y * params[2]);
210 const double** params,
212 const CvPoint2D32f** _points)
const
275 cvCalibrateCamera(
int image_count,
278 cv::Point2f* _image_points,
279 cv::Point2f* _object_points,
280 float* _distortion_coeffs,
281 float* _camera_matrix,
282 float* _translation_vectors,
283 float* _rotation_matrices,
287 cv::Mat point_counts = cv::Mat(image_count, 1, CV_32SC1, _point_counts);
288 std::vector<std::vector<cv::Point2f>> image_points, object_points;
289 cv::Mat dist_coeffs = cv::Mat(4, 1, CV_32FC1, _distortion_coeffs);
290 cv::Mat camera_matrix = cv::Mat(3, 3, CV_32FC1, _camera_matrix);
291 std::vector<cv::Mat> rotation_matrices;
292 std::vector<cv::Mat> translation_vectors;
294 for (i = 0; i < image_count; i++)
296 total += _point_counts[i];
297 for (
int j = 0; j < _point_counts[i]; j++)
299 std::vector<cv::Point2f> img, obj;
300 img.insert(img.begin(), _image_points, _image_points + _point_counts[i]);
301 obj.insert(obj.begin(), _object_points, _object_points + _point_counts[i]);
302 image_points.push_back(img);
303 object_points.push_back(obj);
304 _image_points += _point_counts[i];
305 _object_points += _point_counts[i];
308 rotation_matrices.emplace_back(1, 9, CV_32FC1, _rotation_matrices);
309 translation_vectors.emplace_back(1, 3, CV_32FC1, _translation_vectors);
310 _rotation_matrices += 9;
311 _translation_vectors += 3;
315 cv::calibrateCamera(object_points,
317 std::move(image_size),
325 #define icvCheckVector_32f(ptr, len)
326 #define icvCheckVector_64f(ptr, len)
329 icvMulMatrix_32f(
const float* src1,
int w1,
int h1,
const float* src2,
int w2,
int h2,
float* dst)
339 for (i = 0; i < h1; i++, src1 += w1, dst += w2)
340 for (j = 0; j < w2; j++)
343 for (k = 0; k < w1; k++)
345 s += src1[k] * src2[j + k * w2];
357 for (i = 0; i < len; i++)
359 dst[i] = src1[i] - src2[i];
382 for (i = 0; i < h1; i++, src1 += w1, dst += w2)
383 for (j = 0; j < w2; j++)
386 for (k = 0; k < w1; k++)
388 s += src1[k] * src2[j + k * w2];
399 CvMat Am = cvMat(n, n, CV_64F,
A);
400 CvMat invAm = cvMat(n, n, CV_64F, invA);
402 cvInvert(&Am, &invAm, CV_SVD);
409 for (i = 0; i < len; i++)
411 dst[i] = src1[i] + src2[i];
422 for (i = 0; i < w; i++)
423 for (j = 0; j < h; j++)
425 *dst++ = src[j * w + i];
434 return cvInvert(src, dst, CV_SVD);
537 #define cvmMul(src1, src2, dst) cvMatMulAdd(src1, src2, 0, dst)
589 pointSym2->x = pointCorner.x + alpha * (point1.x - pointCorner.x);
590 pointSym2->y = pointCorner.y + alpha * (point1.y - pointCorner.y);
591 pointSym2->z = pointCorner.z + alpha * (point1.z - pointCorner.z);
608 double alphabetta = alpha * betta;
610 partAll = alpha - betta;
611 if (fabs(partAll) > 0.00001)
614 partX = coeffs->Xcoef + coeffs->XcoefA * alpha + coeffs->XcoefB * betta +
615 coeffs->XcoefAB * alphabetta;
617 partY = coeffs->Ycoef + coeffs->YcoefA * alpha + coeffs->YcoefB * betta +
618 coeffs->YcoefAB * alphabetta;
620 partZ = coeffs->Zcoef + coeffs->ZcoefA * alpha + coeffs->ZcoefB * betta +
621 coeffs->ZcoefAB * alphabetta;
623 invPartAll = 1.0 / partAll;
625 point->x = partX * invPartAll;
626 point->y = partY * invPartAll;
627 point->z = partZ * invPartAll;
647 double invRotMatr2[9];
691 CvStereoLineCoeff* coeffs,
692 int* needSwapCamera);
695 icvComputeCoeffForStereoV3(
double quad1[4][2],
704 CvStereoLineCoeff* startCoeffs,
717 for (currLine = 0; currLine < numScanlines; currLine++)
720 double alpha = ((double)currLine) / ((double)(numScanlines));
722 point1.x = (1.0 - alpha) * quad1[0][0] + alpha * quad1[3][0];
723 point1.y = (1.0 - alpha) * quad1[0][1] + alpha * quad1[3][1];
725 point2.x = (1.0 - alpha) * quad1[1][0] + alpha * quad1[2][0];
726 point2.y = (1.0 - alpha) * quad1[1][1] + alpha * quad1[2][1];
728 point3.x = (1.0 - alpha) * quad2[0][0] + alpha * quad2[3][0];
729 point3.y = (1.0 - alpha) * quad2[0][1] + alpha * quad2[3][1];
731 point4.x = (1.0 - alpha) * quad2[1][0] + alpha * quad2[2][0];
732 point4.y = (1.0 - alpha) * quad2[1][1] + alpha * quad2[2][1];
745 &startCoeffs[currLine],
753 static int icvCvt_32f_64d(
float* src,
double* dst,
int size);
756 icvComputeCoeffForStereoNew(
double quad1[4][2],
763 CvStereoLineCoeff* startCoeffs,
768 double camMatr1_64d[9];
769 double camMatr2_64d[9];
771 double rotMatr1_64d[9];
772 double transVect1_64d[3];
774 double rotMatr2_64d[9];
775 double transVect2_64d[3];
777 icvCvt_32f_64d(camMatr1, camMatr1_64d, 9);
778 icvCvt_32f_64d(camMatr2, camMatr2_64d, 9);
780 icvCvt_32f_64d(rotMatr1, rotMatr1_64d, 9);
781 icvCvt_32f_64d(transVect1, transVect1_64d, 3);
793 transVect2_64d[0] = 0;
794 transVect2_64d[1] = 0;
795 transVect2_64d[2] = 0;
797 int status = icvComputeCoeffForStereoV3(quad1,
821 for (i = 0; i < 4; i++)
823 quad1[i][0] = stereoCamera->quad[0][i].x;
824 quad1[i][1] = stereoCamera->quad[0][i].y;
826 quad2[i][0] = stereoCamera->quad[1][i].x;
827 quad2[i][1] = stereoCamera->quad[1][i].y;
830 icvComputeCoeffForStereoNew(quad1,
832 stereoCamera->warpSize.height,
833 stereoCamera->camera[0]->matrix,
834 stereoCamera->rotMatrix,
835 stereoCamera->transVector,
836 stereoCamera->camera[1]->matrix,
837 stereoCamera->lineCoeffs,
838 &(stereoCamera->needSwapCameras));
846 CvStereoLineCoeff* coeffs);
868 CvStereoLineCoeff* coeffs,
874 CvPoint3D64f direct1;
875 CvPoint3D64f direct2;
876 CvPoint3D64f camPoint1;
878 CvPoint3D64f directS3;
879 CvPoint3D64f directS4;
880 CvPoint3D64f direct3;
881 CvPoint3D64f direct4;
882 CvPoint3D64f camPoint2;
896 double convRotMatr[9];
897 double convTransVect[3];
900 rotMatr1, transVect1, rotMatr2, transVect2, convRotMatr, convTransVect);
902 CvPoint3D64f zeroVect;
903 zeroVect.x = zeroVect.y = zeroVect.z = 0.0;
904 camPoint1.x = camPoint1.y = camPoint1.z = 0.0;
927 pointNewA.x = pointNewA.y = pointNewA.z = 0;
928 pointNewC.x = pointNewC.y = pointNewC.z = 0;
965 len1 =
sqrt((xA - xB) * (xA - xB) + (yA - yB) * (yA - yB) + (zA - zB) * (zA - zB));
966 len2 =
sqrt((xB - xC) * (xB - xC) + (yB - yC) * (yB - yC) + (zB - zC) * (zB - zC));
1032 double a11, a12, a21, a22;
1035 a11 = (xB - xA) * (xB - xA) + (yB - yA) * (yB - yA) + (zB - zA) * (zB - zA);
1036 a12 = -(xD - xC) * (xB - xA) - (yD - yC) * (yB - yA) - (zD - zC) * (zB - zA);
1037 a21 = (xB - xA) * (xD - xC) + (yB - yA) * (yD - yC) + (zB - zA) * (zD - zC);
1038 a22 = -(xD - xC) * (xD - xC) - (yD - yC) * (yD - yC) - (zD - zC) * (zD - zC);
1039 b1 = -((xA - xC) * (xB - xA) + (yA - yC) * (yB - yA) + (zA - zC) * (zB - zA));
1040 b2 = -((xA - xC) * (xD - xC) + (yA - yC) * (yD - yC) + (zA - zC) * (zD - zC));
1043 double deltaA, deltaB;
1044 double alpha, betta;
1046 delta = a11 * a22 - a12 * a21;
1048 if (fabs(delta) <
EPS64D)
1053 deltaA = b1 * a22 - b2 * a12;
1054 deltaB = a11 * b2 - b1 * a21;
1056 alpha = deltaA / delta;
1057 betta = deltaB / delta;
1059 xM = xA + alpha * (xB - xA);
1060 yM = yA + alpha * (yB - yA);
1061 zM = zA + alpha * (zB - zA);
1063 xN = xC + betta * (xD - xC);
1064 yN = yC + betta * (yD - yC);
1065 zN = zC + betta * (zD - zC);
1068 midPoint->x = (xM + xN) * 0.5;
1069 midPoint->y = (yM + yN) * 0.5;
1070 midPoint->z = (zM + zN) * 0.5;
1082 CvStereoLineCoeff* coeffs)
1103 coeffs->Xcoef = -x1 + xA;
1104 coeffs->XcoefA = xB + x1 - xA;
1105 coeffs->XcoefB = -xA - gamma * x1 + gamma * xA;
1106 coeffs->XcoefAB = -xB + xA + gamma * xB - gamma * xA;
1108 coeffs->Ycoef = -y1 + yA;
1109 coeffs->YcoefA = yB + y1 - yA;
1110 coeffs->YcoefB = -yA - gamma * y1 + gamma * yA;
1111 coeffs->YcoefAB = -yB + yA + gamma * yB - gamma * yA;
1113 coeffs->Zcoef = -z1 + zA;
1114 coeffs->ZcoefA = zB + z1 - zA;
1115 coeffs->ZcoefB = -zA - gamma * z1 + gamma * zA;
1116 coeffs->ZcoefAB = -zB + zA + gamma * zB - gamma * zA;
1121 coeffs->Xcoef = -(-x1 + xA);
1122 coeffs->XcoefB = -(xB + x1 - xA);
1123 coeffs->XcoefA = -(-xA - gamma * x1 + gamma * xA);
1124 coeffs->XcoefAB = -(-xB + xA + gamma * xB - gamma * xA);
1126 coeffs->Ycoef = -(-y1 + yA);
1127 coeffs->YcoefB = -(yB + y1 - yA);
1128 coeffs->YcoefA = -(-yA - gamma * y1 + gamma * yA);
1129 coeffs->YcoefAB = -(-yB + yA + gamma * yB - gamma * yA);
1131 coeffs->Zcoef = -(-z1 + zA);
1132 coeffs->ZcoefB = -(zB + z1 - zA);
1133 coeffs->ZcoefA = -(-zA - gamma * z1 + gamma * zA);
1134 coeffs->ZcoefAB = -(-zB + zA + gamma * zB - gamma * zA);
1162 pb.x = imageSize.width - 1;
1165 pd.x = imageSize.width - 1;
1166 pd.y = imageSize.height - 1;
1169 pc.y = imageSize.height - 1;
1174 if (startPoint.x < 0)
1177 if (startPoint.y < 0)
1183 else if (startPoint.y > imageSize.height - 1)
1196 else if (startPoint.x > imageSize.width - 1)
1199 if (startPoint.y < 0)
1205 else if (startPoint.y > imageSize.height - 1)
1221 if (startPoint.y < 0)
1224 if (startPoint.x < imageSize.width / 2)
1235 else if (startPoint.y > imageSize.height - 1)
1238 if (startPoint.x < imageSize.width / 2)
1269 double detA, detB, detC;
1271 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;
1278 detA = p_start.y - p_end.y;
1279 detB = p_end.x - p_start.x;
1280 detC = p_start.x * p_end.y - p_end.x * p_start.y;
1282 double invDet = 1.0 / det;
1293 #define icvTransformVector_64d(matr, src, dst, w, h) icvMulMatrix_64d(matr, w, h, src, 1, w, dst)
1305 icvGetCommonArea(CvSize imageSize,
1331 double transFundMatr[3 * 3];
1338 if (fabs(epipole1.z) < 1e-8)
1344 epipole1_2d.x = epipole1.x / epipole1.z;
1345 epipole1_2d.y = epipole1.y / epipole1.z;
1347 if (fabs(epipole2.z) < 1e-8)
1353 epipole2_2d.x = epipole2.x / epipole2.z;
1354 epipole2_2d.y = epipole2.y / epipole2.z;
1356 int stat =
icvGetAngleLine(epipole1_2d, imageSize, &point11, &point12);
1376 pointW11[0] = point11.x;
1377 pointW11[1] = point11.y;
1396 pointW21[0] = point21.x;
1397 pointW21[1] = point21.y;
1414 coeff11[0] = corr11[0];
1415 coeff11[1] = corr11[1];
1416 coeff11[2] = corr11[2];
1419 icvGetCoefForPiece(epipole2_2d, point21, &coeff21[0], &coeff21[1], &coeff21[2], &res);
1430 icvGetCoefForPiece(epipole1_2d, point11, &coeff11[0], &coeff11[1], &coeff11[2], &res);
1438 coeff21[0] = corr21[0];
1439 coeff21[1] = corr21[1];
1440 coeff21[2] = corr21[2];
1447 pointW12[0] = point12.x;
1448 pointW12[1] = point12.y;
1461 pointW22[0] = point22.x;
1462 pointW22[1] = point22.y;
1476 coeff12[0] = corr12[0];
1477 coeff12[1] = corr12[1];
1478 coeff12[2] = corr12[2];
1481 icvGetCoefForPiece(epipole2_2d, point22, &coeff22[0], &coeff22[1], &coeff22[2], &res);
1492 icvGetCoefForPiece(epipole1_2d, point12, &coeff12[0], &coeff12[1], &coeff12[2], &res);
1500 coeff22[0] = corr22[0];
1501 coeff22[1] = corr22[1];
1502 coeff22[2] = corr22[2];
1521 double det = direct1[0] * direct2[1] - direct2[0] * direct1[1];
1522 double detx = -direct1[2] * direct2[1] + direct1[1] * direct2[2];
1527 cross->x = detx / det;
1528 cross->y = (-direct1[0] * direct2[2] + direct2[0] * direct1[2]) / det;
1567 if ((
a * p_start.x + b * p_start.y +
c) * (
a * p_end.x + b * p_end.y +
c) <= 0)
1571 double detxc, detyc;
1573 det =
a * (p_end.x - p_start.x) + b * (p_end.y - p_start.y);
1578 if (fabs(
a * p_start.x + b * p_start.y +
c) <
EPS64D)
1591 detxc = b * (p_end.y * p_start.x - p_start.y * p_end.x) +
c * (p_start.x - p_end.x);
1592 detyc =
a * (p_end.x * p_start.y - p_start.x * p_end.y) +
c * (p_start.y - p_end.y);
1594 cross->x = detxc / det;
1595 cross->y = detyc / det;
1615 double ex1, ey1, ex2, ey2;
1616 double px1, py1, px2, py2;
1618 double delA, delB, delX, delY;
1619 double alpha, betta;
1631 del = (py1 - py2) * (ex1 - ex2) - (px1 - px2) * (ey1 - ey2);
1639 delA = (ey1 - ey2) * (ex1 - px1) + (ex1 - ex2) * (py1 - ey1);
1640 delB = (py1 - py2) * (ex1 - px1) + (px1 - px2) * (py1 - ey1);
1645 if (alpha < 0 || alpha > 1.0 || betta < 0 || betta > 1.0)
1651 delX = (px1 - px2) * (ey1 * (ex1 - ex2) - ex1 * (ey1 - ey2)) +
1652 (ex1 - ex2) * (px1 * (py1 - py2) - py1 * (px1 - px2));
1654 delY = (py1 - py2) * (ey1 * (ex1 - ex2) - ex1 * (ey1 - ey2)) +
1655 (ey1 - ey2) * (px1 * (py1 - py2) - py1 * (px1 - px2));
1657 cross->x = delX / del;
1658 cross->y = delY / del;
1669 double dx = point2.x - point1.x;
1670 double dy = point2.y - point1.y;
1671 *dist =
sqrt(dx * dx + dy * dy);
1680 double dx = point2.x - point1.x;
1681 double dy = point2.y - point1.y;
1682 double dz = point2.z - point1.z;
1683 *dist =
sqrt(dx * dx + dy * dy + dz * dz);
1712 frameEnd.x = imageSize.width;
1717 frameBeg.x = imageSize.width;
1719 frameEnd.x = imageSize.width;
1720 frameEnd.y = imageSize.height;
1723 frameBeg.x = imageSize.width;
1724 frameBeg.y = imageSize.height;
1726 frameEnd.y = imageSize.height;
1730 frameBeg.y = imageSize.height;
1737 int maxI = 0, maxJ = 0;
1746 for (i = 0; i < 3; i++)
1748 if (haveCross[i] == 1)
1750 for (j = i + 1; j < 4; j++)
1752 if (haveCross[j] == 1)
1770 *start =
cross[maxI];
1804 projPoint->x = tmpVect1[0] / tmpVect1[2];
1805 projPoint->y = tmpVect1[1] / tmpVect1[2];
1810 static int icvCvt_64d_32f(
double* src,
float* dst,
int size);
1825 CvPoint2D32f point1,
1826 CvPoint2D32f* point2);
1831 CvPoint2D32f* point1,
1832 CvPoint2D32f point2);
1857 double convRotMatr[9];
1858 double convTransVect[3];
1861 rotMatr1, transVect1, rotMatr2, transVect2, convRotMatr, convTransVect);
1862 float convRotMatr_32f[9];
1863 float convTransVect_32f[3];
1865 icvCvt_64d_32f(convRotMatr, convRotMatr_32f, 9);
1866 icvCvt_64d_32f(convTransVect, convTransVect_32f, 3);
1871 float fundMatr_32f[9];
1873 float camMatr1_32f[9];
1874 float camMatr2_32f[9];
1876 icvCvt_64d_32f(camMatr1, camMatr1_32f, 9);
1877 icvCvt_64d_32f(camMatr2, camMatr2_32f, 9);
1883 CvPoint3D32f epipole1_32f;
1884 CvPoint3D32f epipole2_32f;
1888 epipole1->x = epipole1_32f.x;
1889 epipole1->y = epipole1_32f.y;
1890 epipole1->z = epipole1_32f.z;
1892 epipole2->x = epipole2_32f.x;
1893 epipole2->y = epipole2_32f.y;
1894 epipole2->z = epipole2_32f.z;
1897 icvCvt_32f_64d(fundMatr_32f, fundMatr, 9);
1906 imageSize, *epipole1, *epipole2, fundMatr, coeff11, coeff12, coeff21, coeff22, &res);
1909 double width1, width2;
1910 double height1, height2;
1911 double tmpHeight1, tmpHeight2;
1917 if (fabs(epipole1->z) < 1e-8)
1921 epipole1_2d.x = epipole1->x / epipole1->z;
1922 epipole1_2d.y = epipole1->y / epipole1->z;
1925 coeff11, coeff12, epipole1_2d, imageSize, &point11, &point12, &point21, &point22, &res);
1931 height1 = MAX(tmpHeight1, tmpHeight2);
1933 quad1[0][0] = point11.x;
1934 quad1[0][1] = point11.y;
1936 quad1[1][0] = point21.x;
1937 quad1[1][1] = point21.y;
1939 quad1[2][0] = point22.x;
1940 quad1[2][1] = point22.y;
1942 quad1[3][0] = point12.x;
1943 quad1[3][1] = point12.y;
1946 if (fabs(epipole2->z) < 1e-8)
1950 epipole2_2d.x = epipole2->x / epipole2->z;
1951 epipole2_2d.y = epipole2->y / epipole2->z;
1954 coeff21, coeff22, epipole2_2d, imageSize, &point11, &point12, &point21, &point22, &res);
1960 height2 = MAX(tmpHeight1, tmpHeight2);
1962 quad2[0][0] = point11.x;
1963 quad2[0][1] = point11.y;
1965 quad2[1][0] = point21.x;
1966 quad2[1][1] = point21.y;
1968 quad2[2][0] = point22.x;
1969 quad2[2][1] = point22.y;
1971 quad2[3][0] = point12.x;
1972 quad2[3][1] = point12.y;
1979 double convRotMatr[9];
1980 double convTransVect[3];
1982 double newQuad1[4][2];
1983 double newQuad2[4][2];
1987 rotMatr1, transVect1, rotMatr2, transVect2, convRotMatr, convTransVect);
1990 CvPoint2D32f pointb1;
1991 CvPoint2D32f pointe1;
1993 CvPoint2D32f pointb2;
1994 CvPoint2D32f pointe2;
1996 pointb1.x = (
float)quad1[0][0];
1997 pointb1.y = (
float)quad1[0][1];
1999 pointe1.x = (
float)quad1[3][0];
2000 pointe1.y = (
float)quad1[3][1];
2009 double dxOld, dyOld;
2010 double dxNew, dyNew;
2011 double distOld, distNew;
2013 dxOld = quad2[1][0] - quad2[0][0];
2014 dyOld = quad2[1][1] - quad2[0][1];
2015 distOld = dxOld * dxOld + dyOld * dyOld;
2017 dxNew = quad2[1][0] - pointb2.x;
2018 dyNew = quad2[1][1] - pointb2.y;
2019 distNew = dxNew * dxNew + dyNew * dyNew;
2021 if (distNew > distOld)
2024 newQuad2[0][0] = pointb2.x;
2025 newQuad2[0][1] = pointb2.y;
2026 newQuad2[3][0] = pointe2.x;
2027 newQuad2[3][1] = pointe2.y;
2028 newQuad1[0][0] = quad1[0][0];
2029 newQuad1[0][1] = quad1[0][1];
2030 newQuad1[3][0] = quad1[3][0];
2031 newQuad1[3][1] = quad1[3][1];
2037 pointb2.x = (
float)quad2[0][0];
2038 pointb2.y = (
float)quad2[0][1];
2040 pointe2.x = (
float)quad2[3][0];
2041 pointe2.y = (
float)quad2[3][1];
2050 newQuad2[0][0] = quad2[0][0];
2051 newQuad2[0][1] = quad2[0][1];
2052 newQuad2[3][0] = quad2[3][0];
2053 newQuad2[3][1] = quad2[3][1];
2055 newQuad1[0][0] = pointb1.x;
2056 newQuad1[0][1] = pointb1.y;
2057 newQuad1[3][0] = pointe1.x;
2058 newQuad1[3][1] = pointe1.y;
2062 pointb1.x = (
float)quad1[1][0];
2063 pointb1.y = (
float)quad1[1][1];
2065 pointe1.x = (
float)quad1[2][0];
2066 pointe1.y = (
float)quad1[2][1];
2074 dxOld = quad2[0][0] - quad2[1][0];
2075 dyOld = quad2[0][1] - quad2[1][1];
2076 distOld = dxOld * dxOld + dyOld * dyOld;
2078 dxNew = quad2[0][0] - pointb2.x;
2079 dyNew = quad2[0][1] - pointb2.y;
2080 distNew = dxNew * dxNew + dyNew * dyNew;
2082 if (distNew > distOld)
2085 newQuad2[1][0] = pointb2.x;
2086 newQuad2[1][1] = pointb2.y;
2087 newQuad2[2][0] = pointe2.x;
2088 newQuad2[2][1] = pointe2.y;
2089 newQuad1[1][0] = quad1[1][0];
2090 newQuad1[1][1] = quad1[1][1];
2091 newQuad1[2][0] = quad1[2][0];
2092 newQuad1[2][1] = quad1[2][1];
2098 pointb2.x = (
float)quad2[1][0];
2099 pointb2.y = (
float)quad2[1][1];
2101 pointe2.x = (
float)quad2[2][0];
2102 pointe2.y = (
float)quad2[2][1];
2108 newQuad2[1][0] = quad2[1][0];
2109 newQuad2[1][1] = quad2[1][1];
2110 newQuad2[2][0] = quad2[2][0];
2111 newQuad2[2][1] = quad2[2][1];
2113 newQuad1[1][0] = pointb1.x;
2114 newQuad1[1][1] = pointb1.y;
2115 newQuad1[2][0] = pointe1.x;
2116 newQuad1[2][1] = pointe1.y;
2124 for (i = 0; i < 4; i++)
2127 quad1[i][0] = newQuad1[i][0];
2128 quad1[i][1] = newQuad1[i][1];
2129 quad2[i][0] = newQuad2[i][0];
2130 quad2[i][1] = newQuad2[i][1];
2136 double warpWidth, warpHeight;
2138 warpWidth = MAX(width1, width2);
2139 warpHeight = MAX(height1, height2);
2141 warpSize->width = (int)warpWidth;
2142 warpSize->height = (int)warpHeight;
2144 warpSize->width = cvRound(warpWidth - 1);
2145 warpSize->height = cvRound(warpHeight - 1);
2148 warpSize->width = imageSize.width;
2149 warpSize->height = imageSize.height;
2157 icvGetQuadsTransformNew(CvSize imageSize,
2166 CvPoint3D32f* epipole1,
2167 CvPoint3D32f* epipole2)
2171 double camMatr1_64d[9];
2172 double camMatr2_64d[9];
2173 double rotMatr1_64d[9];
2174 double transVect1_64d[3];
2175 double rotMatr2_64d[9];
2176 double transVect2_64d[3];
2177 double fundMatr_64d[9];
2181 icvCvt_32f_64d(camMatr1, camMatr1_64d, 9);
2182 icvCvt_32f_64d(camMatr2, camMatr2_64d, 9);
2183 icvCvt_32f_64d(rotMatr1, rotMatr1_64d, 9);
2184 icvCvt_32f_64d(transVect1, transVect1_64d, 3);
2188 rotMatr2_64d[0] = 1;
2189 rotMatr2_64d[1] = 0;
2190 rotMatr2_64d[2] = 0;
2191 rotMatr2_64d[3] = 0;
2192 rotMatr2_64d[4] = 1;
2193 rotMatr2_64d[5] = 0;
2194 rotMatr2_64d[6] = 0;
2195 rotMatr2_64d[7] = 0;
2196 rotMatr2_64d[8] = 1;
2198 transVect2_64d[0] = 0;
2199 transVect2_64d[1] = 0;
2200 transVect2_64d[2] = 0;
2217 epipole1->x = (
float)(epipole1_64d.x);
2218 epipole1->y = (
float)(epipole1_64d.y);
2219 epipole1->z = (
float)(epipole1_64d.z);
2221 epipole2->x = (
float)(epipole2_64d.x);
2222 epipole2->y = (
float)(epipole2_64d.y);
2223 epipole2->z = (
float)(epipole2_64d.z);
2226 icvCvt_64d_32f(fundMatr_64d, fundMatr, 9);
2241 icvGetQuadsTransformNew(cvSize(cvRound(stereoCamera->camera[0]->imgSize[0]),
2242 cvRound(stereoCamera->camera[0]->imgSize[1])),
2243 stereoCamera->camera[0]->matrix,
2244 stereoCamera->camera[1]->matrix,
2245 stereoCamera->rotMatrix,
2246 stereoCamera->transVector,
2247 &(stereoCamera->warpSize),
2250 stereoCamera->fundMatr,
2251 &(stereoCamera->epipole[0]),
2252 &(stereoCamera->epipole[1]));
2255 for (i = 0; i < 4; i++)
2257 stereoCamera->quad[0][i] = cvPoint2D32f(quad1[i][0], quad1[i][1]);
2258 stereoCamera->quad[1][i] = cvPoint2D32f(quad2[i][0], quad2[i][1]);
2279 double transVect1[3];
2280 double transVect2[3];
2282 double convRotMatr[9];
2283 double convTransVect[3];
2286 icvCvt_32f_64d(stereoCamera->camera[0]->rotMatr, rotMatr1, 9);
2287 icvCvt_32f_64d(stereoCamera->camera[1]->rotMatr, rotMatr2, 9);
2289 icvCvt_32f_64d(stereoCamera->camera[0]->transVect, transVect1, 3);
2290 icvCvt_32f_64d(stereoCamera->camera[1]->transVect, transVect2, 3);
2293 rotMatr1, transVect1, rotMatr2, transVect2, convRotMatr, convTransVect);
2296 icvCvt_64d_32f(convRotMatr, stereoCamera->rotMatrix, 9);
2297 icvCvt_64d_32f(convTransVect, stereoCamera->transVector, 3);
2315 normDirect[0] = direct[1];
2316 normDirect[1] = -direct[0];
2317 normDirect[2] = -(normDirect[0] * point.x + normDirect[1] * point.y);
2349 double midLine[3] = {0, 0, 0};
2354 pointOnLine1.x = pointOnLine1.y = 0;
2356 pointOnLine2.x = pointOnLine2.y = 0;
2361 imageSize, areaLineCoef1[0], areaLineCoef1[1], areaLineCoef1[2], &start1, &end1, &res);
2364 pointOnLine1 = start1;
2368 imageSize, areaLineCoef2[0], areaLineCoef2[1], areaLineCoef2[2], &start1, &end1, &res);
2371 pointOnLine2 = start1;
2384 icvTestPoint(cornerPoint, areaLineCoef1, areaLineCoef2, epipole, &res);
2388 candPoints[numPoints] = cornerPoint;
2392 cornerPoint.x = imageSize.width;
2394 icvTestPoint(cornerPoint, areaLineCoef1, areaLineCoef2, epipole, &res);
2398 candPoints[numPoints] = cornerPoint;
2402 cornerPoint.x = imageSize.width;
2403 cornerPoint.y = imageSize.height;
2404 icvTestPoint(cornerPoint, areaLineCoef1, areaLineCoef2, epipole, &res);
2408 candPoints[numPoints] = cornerPoint;
2413 cornerPoint.y = imageSize.height;
2414 icvTestPoint(cornerPoint, areaLineCoef1, areaLineCoef2, epipole, &res);
2418 candPoints[numPoints] = cornerPoint;
2430 for (i = 0; i < res; i++)
2432 candPoints[numPoints++] = tmpPoints[i];
2444 for (i = 0; i < res; i++)
2446 candPoints[numPoints++] = tmpPoints[i];
2458 minPoint.x = minPoint.y = FLT_MAX;
2460 maxPoint.x = maxPoint.y = -FLT_MAX;
2465 double minDist = 10000000;
2468 for (i = 0; i < numPoints; i++)
2475 minPoint = projPoint;
2481 maxPoint = projPoint;
2500 if (epipole.x > imageSize.width * 0.5)
2503 tmpPoint2 = *point11;
2504 *point11 = *point21;
2505 *point21 = tmpPoint2;
2507 tmpPoint2 = *point12;
2508 *point12 = *point22;
2509 *point22 = tmpPoint2;
2531 double alpha = dist2 / dist1;
2533 pointNew1.x = basePoint.x + (1.0 / alpha) * (point2.x - basePoint.x);
2534 pointNew1.y = basePoint.y + (1.0 / alpha) * (point2.y - basePoint.y);
2536 pointNew2.x = basePoint.x + alpha * (point1.x - basePoint.x);
2537 pointNew2.y = basePoint.y + alpha * (point1.y - basePoint.y);
2552 return (point1.x - basePoint.x) * (point2.y - basePoint.y) -
2553 (point2.x - basePoint.x) * (point1.y - basePoint.y);
2572 double sign1 =
icvGetVect(basePoint, point1, point2);
2573 double sign2 =
icvGetVect(basePoint, point1, testPoint);
2574 if (sign1 * sign2 > 0)
2578 sign2 =
icvGetVect(basePoint, point2, testPoint);
2579 if (sign1 * sign2 > 0)
2602 double a = lineCoeff[0];
2603 double b = lineCoeff[1];
2605 double det = 1.0 / (
a *
a + b * b);
2606 double delta =
a * point.y - b * point.x;
2608 projectPoint->x = (-
a * lineCoeff[2] - b * delta) * det;
2609 projectPoint->y = (-b * lineCoeff[2] +
a * delta) * det;
2621 double dx = point.x - tmpPoint.x;
2622 double dy = point.y - tmpPoint.y;
2623 *dist =
sqrt(dx * dx + dy * dy);
2633 src_size.width = src->width;
2634 src_size.height = src->height;
2636 CvSize dst_size = src_size;
2640 dst_size.width = dst->width;
2641 dst_size.height = dst->height;
2644 if (!dst || dst->depth != desired_depth || dst->nChannels != desired_num_channels ||
2645 dst_size.width != src_size.width || dst_size.height != src_size.height)
2647 cvReleaseImage(&dst);
2648 dst = cvCreateImage(src_size, desired_depth, desired_num_channels);
2649 CvRect rect = cvRect(0, 0, src_size.width, src_size.height);
2650 cvSetImageROI(dst, rect);
2657 icvCvt_32f_64d(
float* src,
double* dst,
int size)
2670 for (t = 0; t < size; t++)
2672 dst[t] = (double)(src[t]);
2681 icvCvt_64d_32f(
double* src,
float* dst,
int size)
2694 for (t = 0; t < size; t++)
2696 dst[t] = (
float)(src[t]);
2706 size.width = mat->cols;
2707 size.height = mat->rows;
2713 #define __BEGIN__ __CV_BEGIN__
2714 #define __END__ __CV_END__
2721 CV_FUNCNAME(
"cvComputePerspectiveMap");
2726 CvMat stubx, *mapx = (CvMat*)rectMapX;
2727 CvMat stuby, *mapy = (CvMat*)rectMapY;
2730 CV_CALL(mapx = cvGetMat(mapx, &stubx));
2731 CV_CALL(mapy = cvGetMat(mapy, &stuby));
2733 if (CV_MAT_TYPE(mapx->type) != CV_32FC1 || CV_MAT_TYPE(mapy->type) != CV_32FC1)
2735 CV_ERROR(CV_StsUnsupportedFormat,
"");
2739 assert(fabs(
c[2][2] - 1.) < FLT_EPSILON);
2741 for (i = 0; i < size.height; i++)
2743 float* mx = (
float*)(mapx->data.ptr + mapx->step * i);
2744 float* my = (
float*)(mapy->data.ptr + mapy->step * i);
2746 for (j = 0; j < size.width; j++)
2748 double w = 1. / (
c[2][0] * j +
c[2][1] * i + 1.);
2749 double x = (
c[0][0] * j +
c[0][1] * i +
c[0][2]) * w;
2750 double y = (
c[1][0] * j +
c[1][1] * i +
c[1][2]) * w;
2764 const CvPoint2D32f quad[4],
2765 double matrix[3][3],
2770 CV_FUNCNAME(
"cvInitPerspectiveTransform");
2778 CvMat mapstub, *map = (CvMat*)rectMap;
2783 CV_CALL(map = cvGetMat(map, &mapstub));
2785 if (CV_MAT_TYPE(map->type) != CV_32FC2)
2787 CV_ERROR(CV_StsUnsupportedFormat,
"");
2790 if (map->width != size.width || map->height != size.height)
2792 CV_ERROR(CV_StsUnmatchedSizes,
"");
2796 pt[0] = cvPoint2D32f(0, 0);
2797 pt[1] = cvPoint2D32f(size.width, 0);
2798 pt[2] = cvPoint2D32f(size.width, size.height);
2799 pt[3] = cvPoint2D32f(0, size.height);
2801 for (i = 0; i < 4; i++)
2804 double x = quad[i].x;
2805 double y = quad[i].y;
2811 double X = quad[i].x;
2812 double Y = quad[i].y;
2814 double*
a =
A + i * 16;
2842 CvMat matA = cvMat(8, 8, CV_64F,
A);
2843 CvMat matInvA = cvMat(8, 8, CV_64F, invA);
2844 CvMat matB = cvMat(8, 1, CV_64F, b);
2845 CvMat matX = cvMat(8, 1, CV_64F,
c);
2848 CV_CALL(cvMatMulAdd(&matInvA, &matB, 0, &matX));
2851 matrix[0][0] =
c[0];
2852 matrix[0][1] =
c[1];
2853 matrix[0][2] =
c[2];
2854 matrix[1][0] =
c[3];
2855 matrix[1][1] =
c[4];
2856 matrix[1][2] =
c[5];
2857 matrix[2][0] =
c[6];
2858 matrix[2][1] =
c[7];
2863 for (i = 0; i < size.height; i++)
2865 CvPoint2D32f* maprow = (CvPoint2D32f*)(map->data.ptr + map->step * i);
2866 for (j = 0; j < size.width; j++)
2868 double w = 1. / (
c[6] * j +
c[7] * i + 1.);
2869 double x = (
c[0] * j +
c[1] * i +
c[2]) * w;
2870 double y = (
c[3] * j +
c[4] * i +
c[5]) * w;
2872 maprow[j].x = (
float)x;
2873 maprow[j].y = (
float)y;
2889 CvPoint2D32f point1,
2890 CvPoint2D32f* point2)
2896 p1[0] = (double)(point1.x);
2897 p1[1] = (double)(point1.y);
2914 point2->x = (
float)(projP[0] / projP[2]);
2915 point2->y = (
float)(projP[1] / projP[2]);
2926 CvPoint2D32f* point1,
2927 CvPoint2D32f point2)
2933 p2[0] = (double)(point2.x);
2934 p2[1] = (double)(point2.y);
2949 point1->x = (
float)(projP[0] / projP[2]);
2950 point1->y = (
float)(projP[1] / projP[2]);
2958 CvPoint3D64f* _object_points,
2959 double* _rotation_matrix,
2960 double* _translation_vector,
2961 double* _camera_matrix,
2962 double* _distortion,
2963 CvPoint2D64f* _image_points)
2965 CvMat object_points = cvMat(point_count, 1, CV_64FC3, _object_points);
2966 CvMat image_points = cvMat(point_count, 1, CV_64FC2, _image_points);
2967 CvMat rotation_matrix = cvMat(3, 3, CV_64FC1, _rotation_matrix);
2968 CvMat translation_vector = cvMat(3, 1, CV_64FC1, _translation_vector);
2969 CvMat camera_matrix = cvMat(3, 3, CV_64FC1, _camera_matrix);
2970 CvMat dist_coeffs = cvMat(4, 1, CV_64FC1, _distortion);
2972 cvProjectPoints2(&object_points,
2974 &translation_vector,
2989 icvSelectBestRt(
int numImages,
2991 CvPoint2D32f* imagePoints1,
2992 CvPoint2D32f* imagePoints2,
2993 CvPoint3D32f* objectPoints,
3012 double* rotMatrs1_64d;
3013 double* rotMatrs2_64d;
3015 double* transVects1_64d;
3016 double* transVects2_64d;
3018 double cameraMatrix1_64d[9];
3019 double cameraMatrix2_64d[9];
3021 double distortion1_64d[4];
3022 double distortion2_64d[4];
3027 for (
int i = 0; i < numImages; i++)
3029 totalNum += numPoints[i];
3034 rotMatrs1_64d = (
double*)calloc(numImages,
sizeof(
double) * 9);
3035 rotMatrs2_64d = (
double*)calloc(numImages,
sizeof(
double) * 9);
3037 transVects1_64d = (
double*)calloc(numImages,
sizeof(
double) * 3);
3038 transVects2_64d = (
double*)calloc(numImages,
sizeof(
double) * 3);
3042 icvCvt_32f_64d((
float*)objectPoints, (
double*)objectPoints_64d, totalNum * 3);
3044 icvCvt_32f_64d(rotMatrs1, rotMatrs1_64d, numImages * 9);
3045 icvCvt_32f_64d(rotMatrs2, rotMatrs2_64d, numImages * 9);
3047 icvCvt_32f_64d(transVects1, transVects1_64d, numImages * 3);
3048 icvCvt_32f_64d(transVects2, transVects2_64d, numImages * 3);
3051 icvCvt_32f_64d(cameraMatrix1, cameraMatrix1_64d, 9);
3052 icvCvt_32f_64d(cameraMatrix2, cameraMatrix2_64d, 9);
3054 icvCvt_32f_64d(distortion1, distortion1_64d, 4);
3055 icvCvt_32f_64d(distortion2, distortion2_64d, 4);
3061 errors = (
float*)calloc(numImages * numImages,
sizeof(
float));
3069 for (currRt = 0; currRt < numImages; currRt++)
3072 for (currImagePair = 0; currImagePair < numImages; currImagePair++)
3076 double convRotMatr[9];
3077 double convTransVect[3];
3080 transVects1_64d + currRt * 3,
3081 rotMatrs2_64d + currRt * 9,
3082 transVects2_64d + currRt * 3,
3088 double convRotMatr2[9];
3089 double convTransVect2[3];
3091 convRotMatr2[0] = 1;
3092 convRotMatr2[1] = 0;
3093 convRotMatr2[2] = 0;
3095 convRotMatr2[3] = 0;
3096 convRotMatr2[4] = 1;
3097 convRotMatr2[5] = 0;
3099 convRotMatr2[6] = 0;
3100 convRotMatr2[7] = 0;
3101 convRotMatr2[8] = 1;
3103 convTransVect2[0] = 0;
3104 convTransVect2[1] = 0;
3105 convTransVect2[2] = 0;
3117 numberPnt = numPoints[currImagePair];
3125 for (
int i = 0; i < numberPnt; i++)
3129 tmpPoint.x = (double)(objectPoints[i].x);
3130 tmpPoint.y = (double)(objectPoints[i].y);
3131 tmpPoint.z = (double)(objectPoints[i].z);
3135 rotMatrs2_64d + currImagePair * 9,
3136 transVects2_64d + currImagePair * 3);
3144 rotMatrs1_64d + currImagePair * 9,
3145 transVects1_64d + currImagePair * 3);
3172 for (currPoint = 0; currPoint < numberPnt; currPoint++)
3176 dx1 = imagePoints1[begPoint + currPoint].x - projImagePoints1[currPoint].x;
3177 dy1 = imagePoints1[begPoint + currPoint].y - projImagePoints1[currPoint].y;
3178 len1 =
sqrt(dx1 * dx1 + dy1 * dy1);
3182 dx2 = imagePoints2[begPoint + currPoint].x - projImagePoints2[currPoint].x;
3183 dy2 = imagePoints2[begPoint + currPoint].y - projImagePoints2[currPoint].y;
3184 len2 =
sqrt(dx2 * dx2 + dy2 * dy2);
3188 err1 /= (
float)(numberPnt);
3189 err2 /= (
float)(numberPnt);
3191 err = (err1 + err2) * 0.5;
3192 begPoint += numberPnt;
3195 errors[numImages * currImagePair + currRt] = (
float)err;
3199 free(projImagePoints1);
3200 free(projImagePoints2);
3209 for (currRt = 0; currRt < numImages; currRt++)
3212 for (currImagePair = 0; currImagePair < numImages; currImagePair++)
3214 avErr += errors[numImages * currImagePair + currRt];
3216 avErr /= (
float)(numImages);
3226 if (avErr < minError)
3234 double bestRotMatr_64d[9];
3235 double bestTransVect_64d[3];
3238 transVects1_64d + bestnumRt * 3,
3239 rotMatrs2_64d + bestnumRt * 9,
3240 transVects2_64d + bestnumRt * 3,
3244 icvCvt_64d_32f(bestRotMatr_64d, bestRotMatr, 9);
3245 icvCvt_64d_32f(bestTransVect_64d, bestTransVect, 3);
3258 float ax = point2.x - point1.x;
3259 float ay = point2.y - point1.y;
3261 float bx = point.x - point1.x;
3262 float by = point.y - point1.y;
3264 return (ax * by - ay * bx);
3270 CvPoint2D32f* cameraPoint,
3271 CvPoint2D32f* warpPoint,
3282 det = (coeffs[2][0] * x + coeffs[2][1] * y + coeffs[2][2]);
3283 if (fabs(det) > 1e-8)
3285 cameraPoint->x = (
float)((coeffs[0][0] * x + coeffs[0][1] * y + coeffs[0][2]) / det);
3286 cameraPoint->y = (
float)((coeffs[1][0] * x + coeffs[1][1] * y + coeffs[1][2]) / det);
3296 det = (coeffs[2][0] * x - coeffs[0][0]) * (coeffs[2][1] * y - coeffs[1][1]) -
3297 (coeffs[2][1] * x - coeffs[0][1]) * (coeffs[2][0] * y - coeffs[1][0]);
3299 if (fabs(det) > 1e-8)
3302 (
float)(((coeffs[0][2] - coeffs[2][2] * x) * (coeffs[2][1] * y - coeffs[1][1]) -
3303 (coeffs[2][1] * x - coeffs[0][1]) * (coeffs[1][2] - coeffs[2][2] * y)) /
3306 (
float)(((coeffs[2][0] * x - coeffs[0][0]) * (coeffs[1][2] - coeffs[2][2] * y) -
3307 (coeffs[0][2] - coeffs[2][2] * x) * (coeffs[2][0] * y - coeffs[1][0])) /
3326 stereoparams->warpSize, stereoparams->quad[0], stereoparams->coeffs[0], 0);
3329 stereoparams->warpSize, stereoparams->quad[1], stereoparams->coeffs[1], 0);
3332 CvPoint2D32f corns[4];
3336 corns[1].x = (
float)(stereoparams->camera[0]->imgSize[0] - 1);
3339 corns[2].x = (
float)(stereoparams->camera[0]->imgSize[0] - 1);
3340 corns[2].y = (
float)(stereoparams->camera[0]->imgSize[1] - 1);
3343 corns[3].y = (
float)(stereoparams->camera[0]->imgSize[1] - 1);
3345 for (
int i = 0; i < 4; i++)
3349 stereoparams->coeffs[0], corns + i, stereoparams->border[0] + i,
CV_CAMERA_TO_WARP);
3353 stereoparams->coeffs[1], corns + i, stereoparams->border[1] + i,
CV_CAMERA_TO_WARP);
3358 CvPoint2D32f warpPoints[4];
3359 warpPoints[0] = cvPoint2D32f(0, 0);
3360 warpPoints[1] = cvPoint2D32f(stereoparams->warpSize.width - 1, 0);
3362 cvPoint2D32f(stereoparams->warpSize.width - 1, stereoparams->warpSize.height - 1);
3363 warpPoints[3] = cvPoint2D32f(0, stereoparams->warpSize.height - 1);
3365 CvPoint2D32f camPoints1[4];
3366 CvPoint2D32f camPoints2[4];
3368 for (
int i = 0; i < 4; i++)
3381 stereoparams->lineCoeffs =
3382 (CvStereoLineCoeff*)calloc(stereoparams->warpSize.height,
sizeof(CvStereoLineCoeff));
3398 CvPoint2D32f* imagePoints1,
3399 CvPoint2D32f* imagePoints2,
3400 CvPoint3D32f* objectPoints,
3401 CvStereoCamera* stereoparams)
3411 transVects1 = (
float*)calloc(numImages,
sizeof(
float) * 3);
3412 transVects2 = (
float*)calloc(numImages,
sizeof(
float) * 3);
3414 rotMatrs1 = (
float*)calloc(numImages,
sizeof(
float) * 9);
3415 rotMatrs2 = (
float*)calloc(numImages,
sizeof(
float) * 9);
3418 cvCalibrateCamera(numImages,
3423 stereoparams->camera[0]->distortion,
3424 stereoparams->camera[0]->matrix,
3430 cvCalibrateCamera(numImages,
3435 stereoparams->camera[1]->distortion,
3436 stereoparams->camera[1]->matrix,
3443 stereoparams->camera[0]->imgSize[0] = (
float)imageSize.width;
3444 stereoparams->camera[0]->imgSize[1] = (
float)imageSize.height;
3446 stereoparams->camera[1]->imgSize[0] = (
float)imageSize.width;
3447 stereoparams->camera[1]->imgSize[1] = (
float)imageSize.height;
3449 icvSelectBestRt(numImages,
3454 stereoparams->camera[0]->matrix,
3455 stereoparams->camera[0]->distortion,
3458 stereoparams->camera[1]->matrix,
3459 stereoparams->camera[1]->distortion,
3462 stereoparams->rotMatrix,
3463 stereoparams->transVector);
3478 CvPoint2D32f p1_end,
3479 CvPoint2D32f v2_start,
3480 CvPoint2D32f v2_end,
3481 CvPoint2D32f*
cross)
3483 double ex1 = p1_start.x;
3484 double ey1 = p1_start.y;
3485 double ex2 = p1_end.x;
3486 double ey2 = p1_end.y;
3488 double px1 = v2_start.x;
3489 double py1 = v2_start.y;
3490 double px2 = v2_end.x;
3491 double py2 = v2_end.y;
3493 double del = (ex1 - ex2) * (py2 - py1) + (ey2 - ey1) * (px2 - px1);
3499 double delA = (px1 - ex1) * (py1 - py2) + (ey1 - py1) * (px1 - px2);
3502 double alpha = delA / del;
3505 if (alpha < 0 || alpha > 1.0)
3510 double delX = (ex1 - ex2) * (py1 * (px1 - px2) - px1 * (py1 - py2)) +
3511 (px1 - px2) * (ex1 * (ey1 - ey2) - ey1 * (ex1 - ex2));
3513 double delY = (ey1 - ey2) * (px1 * (py1 - py2) - py1 * (px1 - px2)) +
3514 (py1 - py2) * (ey1 * (ex1 - ex2) - ex1 * (ey1 - ey2));
3527 CvPoint2D32f*
cross)
3530 double delX, delY, delA;
3532 double px1, px2, py1, py2;
3541 del =
a * (px2 - px1) + b * (py2 - py1);
3547 delA = -
c -
a * px1 - b * py1;
3550 if (alpha < 0 || alpha > 1.0)
3555 delX = b * (py1 * (px1 - px2) - px1 * (py1 - py2)) +
c * (px1 - px2);
3556 delY =
a * (px1 * (py1 - py2) - py1 * (px1 - px2)) +
c * (py1 - py2);
3572 CvMat fundMatrC = cvMat(3, 3,
CV_MAT32F, fundMatr);
3574 CvMat* matrW = cvCreateMat(3, 3,
CV_MAT32F);
3575 CvMat* matrU = cvCreateMat(3, 3,
CV_MAT32F);
3576 CvMat* matrV = cvCreateMat(3, 3,
CV_MAT32F);
3580 cvSVD(&fundMatrC, matrW, matrU, matrV, CV_SVD_V_T | CV_SVD_U_T);
3583 epipole1->x = matrU->data.fl[6];
3584 epipole1->y = matrU->data.fl[7];
3585 epipole1->z = matrU->data.fl[8];
3588 epipole2->x = matrV->data.fl[6];
3589 epipole2->y = matrV->data.fl[7];
3590 epipole2->z = matrV->data.fl[8];
3592 cvReleaseMat(&matrW);
3593 cvReleaseMat(&matrU);
3594 cvReleaseMat(&matrV);
3606 CvMat essMatrC = cvMat(3, 3,
CV_MAT32F, essMatr);
3607 CvMat fundMatrC = cvMat(3, 3,
CV_MAT32F, fundMatr);
3608 CvMat cameraMatr1C = cvMat(3, 3,
CV_MAT32F, cameraMatr1);
3609 CvMat cameraMatr2C = cvMat(3, 3,
CV_MAT32F, cameraMatr2);
3611 CvMat* invCM2 = cvCreateMat(3, 3,
CV_MAT32F);
3612 CvMat* tmpMatr = cvCreateMat(3, 3,
CV_MAT32F);
3613 CvMat* invCM1T = cvCreateMat(3, 3,
CV_MAT32F);
3615 cvTranspose(&cameraMatr1C, tmpMatr);
3616 cvInvert(tmpMatr, invCM1T);
3617 cvmMul(invCM1T, &essMatrC, tmpMatr);
3618 cvInvert(&cameraMatr2C, invCM2);
3619 cvmMul(tmpMatr, invCM2, &fundMatrC);
3623 scale = 1.0 / fundMatrC.data.fl[8];
3624 cvConvertScale(&fundMatrC, &fundMatrC, scale);
3626 cvReleaseMat(&invCM2);
3627 cvReleaseMat(&tmpMatr);
3628 cvReleaseMat(&invCM1T);
3642 transMatr[1] = -transVect[2];
3643 transMatr[2] = transVect[1];
3645 transMatr[3] = transVect[2];
3647 transMatr[5] = -transVect[0];
3649 transMatr[6] = -transVect[1];
3650 transMatr[7] = transVect[0];
3678 CvPoint3D32f* buffer = (CvPoint3D32f*)cvAlloc(n *
etalonPointCount *
sizeof(buffer[0]));
3680 float* rotMatr = (
float*)cvAlloc(n * 9 *
sizeof(rotMatr[0]));
3681 float* transVect = (
float*)cvAlloc(n * 3 *
sizeof(transVect[0]));
3682 int* counts = (
int*)cvAlloc(n *
sizeof(counts[0]));
3684 cvInitMatHeader(&mat, 1,
sizeof(CvCamera) /
sizeof(
float), CV_32FC1, 0);
3717 memcpy(
cameraParams[i].rotMatr, rotMatr, 9 *
sizeof(rotMatr[0]));
3718 memcpy(
cameraParams[i].transVect, transVect, 3 *
sizeof(transVect[0]));
3724 if (!cvCheckArr(&mat, CV_CHECK_RANGE | CV_CHECK_QUIET, -10000, 10000))
3764 CvSize pattern_size,
3765 CvPoint2D32f* corners,
3768 return cvFindChessboardCorners(
3769 arr, pattern_size, corners, corner_count, CV_CALIB_CB_ADAPTIVE_THRESH);
3791 (CvPoint2D32f*)cvAlloc(tempPointCount0 * 2 *
sizeof(
latestPoints[0]));
3797 int tempPointCount = tempPointCount0;
3800 if (!CV_IS_MAT(mats[i]) && !CV_IS_IMAGE(mats[i]))
3806 size = cvGetSize(mats[i]);
3808 if (size.width !=
imgSize.width || size.height !=
imgSize.height)
3823 storage = cvCreateMemStorage();
3829 if (CV_MAT_CN(cvGetElemType(mats[i])) == 1)
3835 cvCvtColor(mats[i],
grayImg, CV_BGR2GRAY);
3843 &tempPointCount) != 0;
3851 cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 10, 0.1));
3859 latestCounts[i] = found ? tempPointCount : -tempPointCount;
3860 result = result && found;
3900 CvPoint2D32f* prev =
points[i];
3902 points[i] = (CvPoint2D32f*)cvAlloc(newMaxPoints *
sizeof(prev[0]));
3930 if ((
unsigned)idx >= (
unsigned)
cameraCount || !pts || !count || !found)
3966 CvMat dst_stub, *dst;
3969 CvPoint2D32f* pts = 0;
3973 dst = cvGetMat(dstarr[i], &dst_stub);
3975 static const CvScalar line_colors[] = {CvScalar(0, 0, 255),
3976 CvScalar(0, 128, 255),
3977 CvScalar(0, 200, 200),
3978 CvScalar(0, 255, 0),
3979 CvScalar(200, 200, 0),
3980 CvScalar(255, 0, 0),
3981 CvScalar(255, 0, 255)};
3983 const int colorCount =
sizeof(line_colors) /
sizeof(line_colors[0]);
3985 CvScalar color = line_colors[0];
3986 CvPoint prev_pt = {0, 0};
3988 for (j = 0; j < count; j++)
3991 pt.x = cvRound(pts[j].x);
3992 pt.y = cvRound(pts[j].y);
3998 color = line_colors[(j / cvRound(
etalonParams[0])) % colorCount];
4002 color = CV_RGB(0, 255, 0);
4007 cvLine(dst, prev_pt, pt, color, 1, CV_AA);
4012 dst, cvPoint(pt.x - r, pt.y - r), cvPoint(pt.x + r, pt.y + r), color, 1, CV_AA);
4015 dst, cvPoint(pt.x - r, pt.y + r), cvPoint(pt.x + r, pt.y - r), color, 1, CV_AA);
4017 cvCircle(dst, pt, r + 1, color, 1, CV_AA);
4053 const CvStereoCamera*
4082 cvInitMatHeader(&mat, 1,
cameraCount * (arrSize /
sizeof(
float)), CV_32FC1, params);
4083 cvCheckArr(&mat, CV_CHECK_RANGE, -10000, 10000);
4109 for (j = 0; j < (int)(
sizeof(
cameraParams[i]) /
sizeof(
float)); j++)
4111 fprintf(f,
"%15.10f ", ((
float*)(
cameraParams + i))[j]);
4119 for (i = 0; i < 2; i++)
4121 for (j = 0; j < 4; j++)
4123 fprintf(f,
"%15.10f ",
stereo.quad[i][j].x);
4124 fprintf(f,
"%15.10f ",
stereo.quad[i][j].y);
4130 for (i = 0; i < 2; i++)
4132 for (j = 0; j < 9; j++)
4134 fprintf(f,
"%15.10lf ",
stereo.coeffs[i][j / 3][j % 3]);
4161 if (fscanf(f,
"%d", &d) != 1 || d <= 0 || d > 10)
4170 for (j = 0; j < (int)(
sizeof(
cameraParams[i]) /
sizeof(
float)); j++)
4172 int values_read = fscanf(f,
"%f", &((
float*)(
cameraParams + i))[j]);
4173 CV_Assert(values_read == 1);
4181 for (i = 0; i < 2; i++)
4183 for (j = 0; j < 4; j++)
4185 int values_read = fscanf(f,
"%f ", &(
stereo.quad[i][j].x));
4186 CV_Assert(values_read == 1);
4187 values_read = fscanf(f,
"%f ", &(
stereo.quad[i][j].y));
4188 CV_Assert(values_read == 1);
4193 for (i = 0; i < 2; i++)
4195 for (j = 0; j < 9; j++)
4197 int values_read = fscanf(f,
"%lf ", &(
stereo.coeffs[i][j / 3][j % 3]));
4198 CV_Assert(values_read == 1);
4216 return Rectify((CvMat**)srcarr, (CvMat**)dstarr);
4224 if (!srcarr || !dstarr)
4234 if (srcarr[i] && dstarr[i])
4236 IplImage src_stub, *src;
4237 IplImage dst_stub, *dst;
4239 src = cvGetImage(srcarr[i], &src_stub);
4240 dst = cvGetImage(dstarr[i], &dst_stub);
4242 if (src->imageData == dst->imageData)
4246 CV_MAT_CN(
undistImg->type) != src->nChannels)
4250 cvCreateMat(src->height, src->width, CV_8U + (src->nChannels - 1) * 8);
4259 rectMap[i][0]->height != src->height)
4264 cvCreateMat(
stereo.warpSize.height,
stereo.warpSize.width, CV_32FC1);
4266 cvCreateMat(
stereo.warpSize.height,
stereo.warpSize.width, CV_32FC1);
4277 if (srcarr[i] != dstarr[i])
4279 cvCopy(srcarr[i], dstarr[i]);
4290 return Undistort((CvMat**)srcarr, (CvMat**)dstarr);
4298 if (!srcarr || !dstarr)
4308 if (srcarr[i] && dstarr[i])
4310 CvMat src_stub, *src;
4311 CvMat dst_stub, *dst;
4313 src = cvGetMat(srcarr[i], &src_stub);
4314 dst = cvGetMat(dstarr[i], &dst_stub);
4316 if (src->data.ptr == dst->data.ptr)
4322 undistImg = cvCreateMat(src->height, src->width, src->type);
4332 CvMat k = cvMat(1, 4, CV_32FC1,
cameraParams[i].distortion);
4339 undistMap[i][0] = cvCreateMat(src->height, src->width, CV_32FC1);
4340 undistMap[i][1] = cvCreateMat(src->height, src->width, CV_32FC1);
4346 cvUndistort2(src, dst, &
A, &k);
4357 if (srcarr[i] != dstarr[i])
4359 cvCopy(srcarr[i], dstarr[i]);