Skip to content

Commit

Permalink
Use armadillo for easier conversion to OpenCV
Browse files Browse the repository at this point in the history
  • Loading branch information
sjmgarnier committed Nov 1, 2023
1 parent 7c45e59 commit 85b1716
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 50 deletions.
8 changes: 5 additions & 3 deletions R/calib3d.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ findChessboardCorners <- function(image, pprow, ppcol, adaptive_threshold = TRUE

flags <- 1 * adaptive_threshold + 2 * normalize + 4 * filter_quads + 8 * fast_check

`_findChessboardCorners`(image, pprow, ppcol, flags)
`_findChessboardCorners`(image, pprow, ppcol, flags)[,,]
}


Expand Down Expand Up @@ -115,7 +115,8 @@ cornerSubPix <- function(image, corners, win_size = c(11, 11), zero_zone = c(-1,
if (nrow(corners) < 1)
stop("'corners' does not contain any value.")

`_cornerSubPix`(image, corners, win_size, zero_zone, maxit, eps)
dim(corners) <- c(nrow(corners), 1, 2)
`_cornerSubPix`(image, corners, win_size, zero_zone, maxit, eps)[,,]
}


Expand Down Expand Up @@ -388,5 +389,6 @@ undistortPoints <- function(points, camera_matrix, dist_coefs, new_camera_matrix
if (ncol(points) != 2)
stop("'points' should have exactly 2 columns.")

`_undistortPoints`(points, camera_matrix, dist_coefs, new_camera_matrix)
dim(points) <- c(nrow(points), 1, 2)
`_undistortPoints`(points, camera_matrix, dist_coefs, new_camera_matrix)[,,]
}
71 changes: 27 additions & 44 deletions src/calib3d.h
Original file line number Diff line number Diff line change
@@ -1,51 +1,42 @@
Rcpp::NumericMatrix _findChessboardCorners(Image& image, int pprow, int ppcol, int flags) {
std::vector<cv::Point2f> corners;
arma::Cube<float> _findChessboardCorners(Image& image, int pprow, int ppcol, int flags) {
cv::Mat_<cv::Vec2f> cornersCV;

if (image.GPU) {
cv::findChessboardCorners(image.uimage, cv::Size(pprow, ppcol), corners, flags);
cv::findChessboardCorners(image.uimage, cv::Size(pprow, ppcol), cornersCV, flags);
} else {
cv::findChessboardCorners(image.image, cv::Size(pprow, ppcol), corners, flags);
cv::findChessboardCorners(image.image, cv::Size(pprow, ppcol), cornersCV, flags);
}

Rcpp::NumericMatrix out = Rcpp::NumericMatrix(corners.size(), 2);
colnames(out) = Rcpp::CharacterVector::create("x", "y");
arma::Cube<float> corners;

for (uint i = 0; i < corners.size(); i++) {
out(i, 0) = corners[i].x + 1;
out(i, 1) = -corners[i].y + image.nrow();
if (!cornersCV.empty()) {
cv2arma(cornersCV, corners);
corners.slice(0) += 1;
corners.slice(1) = -corners.slice(1) + image.nrow();
}

return out;
return corners;
}


Rcpp::NumericMatrix _cornerSubPix(Image& image, Rcpp::NumericMatrix corners,
Rcpp::NumericVector winSize, Rcpp::NumericVector zeroZone,
int maxit, double eps) {
arma::Cube<float> _cornerSubPix(Image& image, arma::Cube<float> corners,
Rcpp::NumericVector winSize, Rcpp::NumericVector zeroZone,
int maxit, double eps) {
cv::TermCriteria termcrit(cv::TermCriteria::COUNT+cv::TermCriteria::EPS, maxit, eps);
std::vector<cv::Point2f> corners_vec;

for (uint i = 0; i < corners.nrow(); i++) {
corners_vec.push_back(cv::Point2f(corners(i,0) - 1, -corners(i, 1) + image.nrow()));
}
cv::Mat_<cv::Vec2f> cornersCV;
arma2cv(corners, cornersCV);

if (image.GPU) {
cv::cornerSubPix(image.uimage, corners_vec, cv::Size(winSize(0), winSize(1)),
cv::cornerSubPix(image.uimage, cornersCV, cv::Size(winSize(0), winSize(1)),
cv::Size(zeroZone(0), zeroZone(1)), termcrit);
} else {
cv::cornerSubPix(image.image, corners_vec, cv::Size(winSize(0), winSize(1)),
cv::cornerSubPix(image.image, cornersCV, cv::Size(winSize(0), winSize(1)),
cv::Size(zeroZone(0), zeroZone(1)), termcrit);
}

for (uint i = 0; i < corners_vec.size(); i++) {
corners(i, 0) = corners_vec[i].x + 1;
corners(i, 1) = -corners_vec[i].y + image.nrow();
}

cv2arma(cornersCV, corners);
return corners;
}


Rcpp::List _calibrateCameraRO(Rcpp::List refPoints, Rcpp::List imgPoints,
Rcpp::NumericVector imgSize, int iFixedPoint,
int flags, int maxit, double eps) {
Expand Down Expand Up @@ -146,28 +137,20 @@ void _undistort(Image& image, arma::Mat<double> cameraMatrix, arma::Mat<double>
cv::undistort(image.image, target.image, cameraMatrixCV, distCoeffsCV, newCameraMatrixCV);
}

Rcpp::NumericMatrix _undistortPoints(Rcpp::NumericMatrix points,
arma::Mat<double> cameraMatrix,
arma::Mat<double> distCoeffs,
arma::Mat<double> newCameraMatrix) {
std::vector<cv::Point2f> pointsCV, outCV;
for (uint i = 0; i < points.nrow(); i++) {
pointsCV.push_back(cv::Point2f(points(i,0), points(i, 1)));
}
arma::Cube<float> _undistortPoints(arma::Cube<float> points,
arma::Mat<double> cameraMatrix,
arma::Mat<double> distCoeffs,
arma::Mat<double> newCameraMatrix) {
cv::Mat_<cv::Vec2f> pointsCV;
arma2cv(points, pointsCV);

cv::Mat_<double> cameraMatrixCV, distCoeffsCV, newCameraMatrixCV;
arma2cv(cameraMatrix, cameraMatrixCV);
arma2cv(distCoeffs, distCoeffsCV);
arma2cv(newCameraMatrix, newCameraMatrixCV);

cv::undistortPoints(pointsCV, outCV, cameraMatrixCV, distCoeffsCV, cv::noArray(), newCameraMatrixCV);
cv::undistortPoints(pointsCV, pointsCV, cameraMatrixCV, distCoeffsCV, cv::noArray(), newCameraMatrixCV);
cv2arma(pointsCV, points);

Rcpp::NumericMatrix out = Rcpp::NumericMatrix(outCV.size(), 2);
for (uint i = 0; i < outCV.size(); i++) {
out(i, 0) = outCV[i].x;
out(i, 1) = outCV[i].y;
}

return out;
return points;
}

4 changes: 2 additions & 2 deletions src/filters.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ void _pyrDown(Image& image, Image& target) {
if (target.GPU)
cv::pyrDown(image.image, target.uimage, cv::Size(target.uimage.cols, target.uimage.rows));

return cv::pyrDown(image.image, target.image, cv::Size(target.image.cols, target.image.rows));
return cv::pyrDown(image.image, target.image, cv::Size(target.image.cols, target.image.rows));
}

void _pyrUp(Image& image, Image& target) {
Expand All @@ -283,5 +283,5 @@ void _pyrUp(Image& image, Image& target) {
if (target.GPU)
cv::pyrUp(image.image, target.uimage, cv::Size(target.uimage.cols, target.uimage.rows));

return cv::pyrUp(image.image, target.image, cv::Size(target.image.cols, target.image.rows));
return cv::pyrUp(image.image, target.image, cv::Size(target.image.cols, target.image.rows));
}
2 changes: 1 addition & 1 deletion src/shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ Rcpp::NumericMatrix _approxPolyDP(Rcpp::NumericMatrix curve, double epsilon, boo
Rcpp::NumericMatrix out(approxpoints.size(), 2);
colnames(out) = Rcpp::CharacterVector::create("x", "y");

for (int i = 0; i < approxpoints.size(); i++) {
for (unsigned int i = 0; i < approxpoints.size(); i++) {
out(i, 0) = approxpoints[i].x;
out(i, 1) = approxpoints[i].y;
}
Expand Down

0 comments on commit 85b1716

Please sign in to comment.