You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In YOLOv8, to resize images while maintaining the aspect ratio, and then create a new 640x640 image where the resized image is centered, with the remaining area filled with a specified color (such as black), is there a simplified method?
demo:
fn resize_and_pad(
image: &DynamicImage,
target_width: u32,
target_height: u32,
padding_color: Rgb<u8>,
) -> DynamicImage {
// 计算新的尺寸,保持宽高比
let (original_width, original_height) = image.dimensions();
let ratio = if original_width > original_height {
target_width as f32 / original_width as f32
} else {
target_height as f32 / original_height as f32
};
let new_width = (original_width as f32 * ratio).round() as u32;
let new_height = (original_height as f32 * ratio).round() as u32;
// 调整大小并转换为 Rgb8 格式
let resized_image = image
.resize(new_width, new_height, FilterType::Nearest)
.to_rgb8();
// 创建一个目标尺寸的新图像并用指定颜色填充
let mut padded_image = ImageBuffer::from_pixel(target_width, target_height, padding_color);
// 将调整大小后的图像居中放置到新图像上
let x_offset = ((target_width as i64 - new_width as i64) / 2).max(0) as i64;
let y_offset = ((target_height as i64 - new_height as i64) / 2).max(0) as i64;
image::imageops::overlay(&mut padded_image, &resized_image, x_offset, y_offset);
DynamicImage::ImageRgb8(padded_image)
}
This discussion was converted from issue #2393 on December 16, 2024 13:14.
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
In YOLOv8, to resize images while maintaining the aspect ratio, and then create a new 640x640 image where the resized image is centered, with the remaining area filled with a specified color (such as black), is there a simplified method?
demo:
Beta Was this translation helpful? Give feedback.
All reactions