From 4faace17b6aeda5469eba6d6199f3f5b831b0542 Mon Sep 17 00:00:00 2001 From: Gines Hidalgo Date: Sat, 13 Jun 2020 21:40:17 -0400 Subject: [PATCH] 90 and 270 rotations working again #1586 --- doc/release_notes.md | 1 + include/openpose/core/matrix.hpp | 5 +++++ include/openpose/utilities/openCv.hpp | 4 +++- src/openpose/core/matrix.cpp | 13 +++++++++++++ src/openpose/utilities/openCv.cpp | 22 +++++++++++++++++----- 5 files changed, 39 insertions(+), 6 deletions(-) diff --git a/doc/release_notes.md b/doc/release_notes.md index fcac086a..e3fc9127 100644 --- a/doc/release_notes.md +++ b/doc/release_notes.md @@ -418,6 +418,7 @@ OpenPose Library - Release Notes 1. Main improvements: 2. Functions or parameters renamed: 3. Main bugs fixed: + 1. 90 and 270-degree rotations working again. 4. Changes/additions that affect the compatibility with the OpenPose Unity Plugin: diff --git a/include/openpose/core/matrix.hpp b/include/openpose/core/matrix.hpp index 8b470c78..bce5be90 100644 --- a/include/openpose/core/matrix.hpp +++ b/include/openpose/core/matrix.hpp @@ -112,6 +112,11 @@ namespace op */ explicit Matrix(const void* cvMatPtr); + /** + * Analog to cv::Mat(int rows, int cols, int type, void *data, size_t step=AUTO_STEP) + */ + explicit Matrix(const int rows, const int cols, const int type); + /** * Analog to cv::Mat(int rows, int cols, int type, void *data, size_t step=AUTO_STEP) * Very important: This Matrix will only "borrow" this pointer, so the caller must make sure to maintain the diff --git a/include/openpose/utilities/openCv.hpp b/include/openpose/utilities/openCv.hpp index 9c70364e..fd8ae9cb 100644 --- a/include/openpose/utilities/openCv.hpp +++ b/include/openpose/utilities/openCv.hpp @@ -13,13 +13,15 @@ namespace op OP_API void keepRoiInside(Rectangle& roi, const int imageWidth, const int imageHeight); + OP_API void transpose(Matrix& matrix); + /** * It performs rotation and flipping over the desired Mat. * @param cvMat Mat with the frame matrix to be rotated and/or flipped. * @param rotationAngle How much the cvMat element should be rotated. 0 would mean no rotation. * @param flipFrame Whether to flip the cvMat element. Set to false to disable it. */ - OP_API void rotateAndFlipFrame(Matrix& cvMat, const double rotationAngle, const bool flipFrame = false); + OP_API void rotateAndFlipFrame(Matrix& frame, const double rotationAngle, const bool flipFrame = false); /** * Wrapper of CV_CAP_PROP_FRAME_COUNT to avoid leaving OpenCV dependencies on headers. diff --git a/src/openpose/core/matrix.cpp b/src/openpose/core/matrix.cpp index 10e3dfcc..e7bf67a1 100644 --- a/src/openpose/core/matrix.cpp +++ b/src/openpose/core/matrix.cpp @@ -54,6 +54,19 @@ namespace op } } + Matrix::Matrix(const int rows, const int cols, const int type) : + spImpl{std::make_shared()} + { + try + { + spImpl->mCvMat = cv::Mat(rows, cols, type); + } + catch (const std::exception& e) + { + error(e.what(), __LINE__, __FUNCTION__, __FILE__); + } + } + Matrix::Matrix(const int rows, const int cols, const int type, void* cvMatPtr) : spImpl{std::make_shared()} { diff --git a/src/openpose/utilities/openCv.cpp b/src/openpose/utilities/openCv.cpp index bd1141e7..c66a78d1 100644 --- a/src/openpose/utilities/openCv.cpp +++ b/src/openpose/utilities/openCv.cpp @@ -224,14 +224,28 @@ namespace op } } + void transpose(Matrix& matrix) + { + cv::Mat cvMatrix = OP_OP2CVMAT(matrix); + Matrix matrixFinal(matrix.cols(), matrix.rows(), matrix.type()); + cv::Mat cvMatrixFinal = OP_OP2CVMAT(matrixFinal); + cv::transpose(cvMatrix, cvMatrixFinal); + std::swap(matrix, matrixFinal); + } + void rotateAndFlipFrame(Matrix& frame, const double rotationAngle, const bool flipFrame) { try { - cv::Mat cvMatFrame = OP_OP2CVMAT(frame); - if (!cvMatFrame.empty()) + // cv::flip() does not moidify the memory location of the cv::Mat, but cv::transpose does + if (!frame.empty()) { const auto rotationAngleInt = (int)std::round(rotationAngle) % 360; + // Transposing + if (rotationAngleInt == 90 || rotationAngleInt == 270 || rotationAngleInt == -90 || rotationAngleInt == -270) + transpose(frame); + // Mirroring (flipping) + cv::Mat cvMatFrame = OP_OP2CVMAT(frame); if (rotationAngleInt == 0 || rotationAngleInt == 360) { if (flipFrame) @@ -239,7 +253,6 @@ namespace op } else if (rotationAngleInt == 90 || rotationAngleInt == -270) { - cv::transpose(cvMatFrame, cvMatFrame); if (!flipFrame) cv::flip(cvMatFrame, cvMatFrame, 0); } @@ -252,7 +265,6 @@ namespace op } else if (rotationAngleInt == 270 || rotationAngleInt == -90) { - cv::transpose(cvMatFrame, cvMatFrame); if (flipFrame) cv::flip(cvMatFrame, cvMatFrame, -1); else @@ -260,7 +272,7 @@ namespace op } else error("Rotation angle = " + std::to_string(rotationAngleInt) - + " != {0, 90, 180, 270} degrees.", __LINE__, __FUNCTION__, __FILE__); + + " != {0, 90, 180, 270} degrees.", __LINE__, __FUNCTION__, __FILE__); } } catch (const std::exception& e) -- GitLab