Skip to content

Commit

Permalink
Add common image rotation as special cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jlblancoc committed Oct 11, 2024
1 parent ad80afe commit 28857d5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/source/doxygen-docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

# Version 2.14.3: UNRELEASED
- Changes in libraries:
- \ref mrpt_img_grp:
- mrpt::img::CImage::rotateImage(): Special angles 90,-90, 180 are handled as expected with a quick image transformation and rotation.
- \ref mrpt_nav_grp:
- mrpt::nav::CWaypointsNavigator: New parameter "minimum_target_approach_per_step" and feature to keep approaching waypoints until no significant improvement is done.
- \ref mrpt_ros1bridge_grp and mrpt_ros2bridge_grp:
Expand Down
4 changes: 3 additions & 1 deletion libs/img/include/mrpt/img/CImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,10 @@ class CImage : public mrpt::serialization::CSerializable, public CCanvas
unsigned int height,
TInterpolationMethod interp = IMG_INTERP_CUBIC) const;

/** Rotates the image by the given angle around the given center point, with
/** Rotates the image by the given angle (in radians) around the given center point, with
* an optional scale factor.
* The output image will have the same size as the input, except if angle is exactly ±90 degrees,
* in which case a quick image rotation (switching height and widht) will be performed instead.
* \sa resize, scaleImage
*/
void rotateImage(
Expand Down
15 changes: 15 additions & 0 deletions libs/img/src/CImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,21 @@ void CImage::rotateImage(
// Detect in-place operation and make a deep copy if needed:
if (out_img.m_impl->img.data == srcImg.data) srcImg = srcImg.clone();

// quick rotation?
if (std::abs(M_PI * 0.5 - std::abs(ang)) < 1e-3 || std::abs(M_PI - std::abs(ang)) < 1e-3)
{
int rotCode = 0;
if (std::abs(M_PI * 0.5 - ang) < 1e-3)
rotCode = cv::ROTATE_90_COUNTERCLOCKWISE;
else if (std::abs(-M_PI * 0.5 - ang) < 1e-3)
rotCode = cv::ROTATE_90_CLOCKWISE;
else if (std::abs(M_PI - ang) < 1e-3)
rotCode = cv::ROTATE_180;

cv::rotate(srcImg, out_img.m_impl->img, rotCode);
}
// else: general rotation:

out_img.resize(getWidth(), getHeight(), getChannelCount());

// Based on the blog entry:
Expand Down

0 comments on commit 28857d5

Please sign in to comment.