Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rotate_about_center_no_crop to prevent pixel loss during image rotations #688

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 83 additions & 3 deletions src/geometric_transformations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,85 @@ where
warp(image, &projection, interpolation, default)
}

/// Rotates an image clockwise about its center by theta radians without cropping.
/// The output image has dimensions calculated to fit the entire rotated image.
/// Output pixels whose pre-image lies outside the input image are set to `default`.
pub fn rotate_about_center_no_crop<P>(
image: &Image<P>,
theta: f32,
Tikitikitikidesuka marked this conversation as resolved.
Show resolved Hide resolved
interpolation: Interpolation,
default: P,
) -> Image<P>
where
P: Pixel + Send + Sync,
<P as Pixel>::Subpixel: Send + Sync,
<P as Pixel>::Subpixel: Into<f32> + Clamp<f32>,
{
let (width, height) = image.dimensions();

let cos = theta.cos();
let sin = theta.sin();

let new_width = (height as f32 * sin.abs() + width as f32 * cos.abs()).ceil() as u32;
let new_height = (height as f32 * cos.abs() + width as f32 * sin.abs()).ceil() as u32;

let mut out_img = Image::new(new_width, new_height);

rotate_about_center_into(image, theta, interpolation, default, &mut out_img);

out_img
}

/// Rotates an image clockwise about its center by theta radians, writing to a provided output.
/// Output pixels whose pre-image lies outside the input image are set to `default`.
fn rotate_about_center_into<P>(
image: &Image<P>,
theta: f32,
interpolation: Interpolation,
default: P,
out: &mut Image<P>,
) where
P: Pixel + Send + Sync,
<P as Pixel>::Subpixel: Send + Sync,
<P as Pixel>::Subpixel: Into<f32> + Clamp<f32>,
{
let (w, h) = image.dimensions();
let (ow, oh) = out.dimensions();
rotate_into(
image,
(w as f32 / 2.0, h as f32 / 2.0),
(ow as f32 / 2.0, oh as f32 / 2.0),
theta,
interpolation,
default,
out,
)
}

/// Rotates an image clockwise about the provided center by theta radians, writing to a provided output.
/// Output pixels whose pre-image lies outside the input image are set to `default`.
fn rotate_into<P>(
image: &Image<P>,
center: (f32, f32),
out_center: (f32, f32),
theta: f32,
interpolation: Interpolation,
default: P,
out: &mut Image<P>,
) where
P: Pixel + Send + Sync,
<P as Pixel>::Subpixel: Send + Sync,
<P as Pixel>::Subpixel: Into<f32> + Clamp<f32>,
{
let (cx, cy) = center;
let (ocx, ocy) = out_center;
let projection = Projection::translate(ocx, ocy)
* Projection::rotate(theta)
* Projection::translate(-cx, -cy);

warp_into(image, &projection, interpolation, default, out);
}

/// Rotates an image 90 degrees clockwise.
///
/// # Examples
Expand Down Expand Up @@ -882,7 +961,8 @@ pub enum Interpolation {
#[cfg(test)]
mod tests {
use super::*;
use image::Luma;
use image::{GrayImage, Luma};
use std::f32::consts::PI;

#[test]
fn test_rotate_nearest_zero_radians() {
Expand All @@ -901,7 +981,7 @@ mod tests {
}

#[test]
fn text_rotate_nearest_quarter_turn_clockwise() {
fn test_rotate_nearest_quarter_turn_clockwise() {
let image = gray_image!(
00, 01, 02;
10, 11, 12);
Expand All @@ -917,7 +997,7 @@ mod tests {
}

#[test]
fn text_rotate_nearest_half_turn_anticlockwise() {
fn test_rotate_nearest_half_turn_anticlockwise() {
let image = gray_image!(
00, 01, 02;
10, 11, 12);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
125 changes: 125 additions & 0 deletions tests/regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use imageproc::contrast::ThresholdType;
use imageproc::definitions::Image;
use imageproc::filter::bilateral::GaussianEuclideanColorDistance;
use imageproc::filter::bilateral_filter;
use imageproc::geometric_transformations::rotate_about_center_no_crop;
use imageproc::kernel::{self};
use imageproc::{
definitions::{Clamp, HasBlack, HasWhite},
Expand Down Expand Up @@ -142,6 +143,23 @@ fn test_rotate_nearest_rgb() {
);
}

#[test]
fn test_rotate_no_crop_nearest_rgb() {
fn rotate_nearest_about_center_no_crop(image: &RgbImage) -> RgbImage {
rotate_about_center_no_crop(
image,
std::f32::consts::PI / 4f32,
Interpolation::Nearest,
Rgb::black(),
)
}
compare_to_truth(
"elephant.png",
"elephant_rotate_no_crop_nearest.png",
rotate_nearest_about_center_no_crop,
);
}

#[test]
fn test_rotate_nearest_rgba() {
fn rotate_nearest_about_center(image: &RgbaImage) -> RgbaImage {
Expand All @@ -159,6 +177,23 @@ fn test_rotate_nearest_rgba() {
);
}

#[test]
fn test_rotate_no_crop_nearest_rgba() {
fn rotate_nearest_about_center_no_crop(image: &RgbaImage) -> RgbaImage {
rotate_about_center_no_crop(
image,
std::f32::consts::PI / 4f32,
Interpolation::Nearest,
Rgba::black(),
)
}
compare_to_truth(
"elephant_rgba.png",
"elephant_rotate_no_crop_nearest_rgba.png",
rotate_nearest_about_center_no_crop,
);
}

#[test]
fn test_equalize_histogram_grayscale() {
use imageproc::contrast::equalize_histogram;
Expand All @@ -183,6 +218,24 @@ fn test_rotate_bilinear_rgb() {
);
}

#[test]
fn test_rotate_bilinear_no_crop_rgb() {
fn rotate_bilinear_about_center_no_crop(image: &RgbImage) -> RgbImage {
rotate_about_center_no_crop(
image,
std::f32::consts::PI / 4f32,
Interpolation::Bilinear,
Rgb::black(),
)
}
compare_to_truth_with_tolerance(
"elephant.png",
"elephant_rotate_no_crop_bilinear.png",
rotate_bilinear_about_center_no_crop,
2,
);
}

#[test]
fn test_rotate_bilinear_rgba() {
fn rotate_bilinear_about_center(image: &RgbaImage) -> RgbaImage {
Expand All @@ -201,6 +254,24 @@ fn test_rotate_bilinear_rgba() {
);
}

#[test]
fn test_rotate_no_crop_bilinear_rgba() {
fn rotate_bilinear_about_center_no_crop(image: &RgbaImage) -> RgbaImage {
rotate_about_center_no_crop(
image,
std::f32::consts::PI / 4f32,
Interpolation::Bilinear,
Rgba::black(),
)
}
compare_to_truth_with_tolerance(
"elephant_rgba.png",
"elephant_rotate_no_crop_bilinear_rgba.png",
rotate_bilinear_about_center_no_crop,
2,
);
}

#[test]
fn test_rotate_bicubic_rgb() {
fn rotate_bicubic_about_center(image: &RgbImage) -> RgbImage {
Expand All @@ -219,6 +290,24 @@ fn test_rotate_bicubic_rgb() {
);
}

#[test]
fn test_rotate_no_crop_bicubic_rgb() {
fn rotate_bicubic_about_center_no_crop(image: &RgbImage) -> RgbImage {
rotate_about_center_no_crop(
image,
std::f32::consts::PI / 4f32,
Interpolation::Bicubic,
Rgb::black(),
)
}
compare_to_truth_with_tolerance(
"elephant.png",
"elephant_rotate_no_crop_bicubic.png",
rotate_bicubic_about_center_no_crop,
2,
);
}

#[test]
fn test_rotate_bicubic_rgba() {
fn rotate_bicubic_about_center(image: &RgbaImage) -> RgbaImage {
Expand All @@ -237,6 +326,42 @@ fn test_rotate_bicubic_rgba() {
);
}

#[test]
fn test_rotate_no_crop_bicubic_rgba() {
fn rotate_bicubic_about_center_no_crop(image: &RgbaImage) -> RgbaImage {
rotate_about_center_no_crop(
image,
std::f32::consts::PI / 4f32,
Interpolation::Bicubic,
Rgba::black(),
Tikitikitikidesuka marked this conversation as resolved.
Show resolved Hide resolved
)
}
compare_to_truth_with_tolerance(
"elephant_rgba.png",
"elephant_rotate_no_crop_bicubic_rgba.png",
rotate_bicubic_about_center_no_crop,
2,
);
}

#[test]
fn test_rotate_no_crop_default_color() {
fn rotate_nearest_about_center_no_crop_default_red(image: &RgbaImage) -> RgbaImage {
rotate_about_center_no_crop(
image,
std::f32::consts::PI / 4f32,
Interpolation::Nearest,
Rgba([255, 0, 0, 255]),
)
}
compare_to_truth_with_tolerance(
"elephant_rgba.png",
"elephant_rotate_no_crop_nearest_default_red_rgba.png",
rotate_nearest_about_center_no_crop_default_red,
2,
)
}

#[test]
fn test_affine_nearest_rgb() {
fn affine_nearest(image: &RgbImage) -> RgbImage {
Expand Down