diff --git a/doc/release_notes.md b/doc/release_notes.md index fcac086a2992b231fdf55fdcf597a8b6c79003c4..e3fc91270ab8681ee27a91831a42402f4dbb4103 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 8b470c78e3d487375d2008a96b29d12519f0a927..bce5be90dcf4db26630601f6a1a95d5861b69aa6 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 9c70364e823d4d7d3fc092546959d83263404232..fd8ae9cb02df8e1849be96d34b9861f7179cf8cb 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 10e3dfcc0fdd3c8efd574d4d6f340158b22be921..e7bf67a1352e33ce19d5d2d77d3c2b952fa3dfe5 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 bd1141e7f1c8d45d6eb9965e6efece76d5182938..c66a78d13144e13772503abe875436eaabb4b369 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)