#find #position #processing #match #image-processing #graphics #subimage

find-subimage

一个Rust包,用于在较大图像中查找子图像

13个版本

0.1.12 2021年12月30日
0.1.11 2021年12月30日

#446图像

Download history 6/week @ 2024-03-10 72/week @ 2024-03-31

每月52次下载

MIT OR Apache-2…

33KB
514 代码行

查找子图像

github crates.io docs.rs build status

此crate通过计算图像距离提供在较大图像中查找子图像的可能位置的基本功能。它在Rust中有一个原始的标量实现,以及一个使用simdeez crate(基于CPU特征在运行时选择最佳实现的)的simd实现。它还提供了一个使用opencv-rust crate(通过可选的默认关闭特性)通过OpenCV的matchTemplate函数实现的C++库。它还可以选择性地在应用算法之前将图像转换为灰度。

以下是一个简单的示例,说明如何使用API

use find_subimage::{Image, SubImageFinderState};
// Make a dummy 128x128 black image with a red dot at (50, 0)
let (w, h) = (128, 128);
let mut rgb_image = vec![0u8; w * h * 3];
rgb_image[50 * 3] = 250;
// Make a dummy 32x32 black image
// with a red dot at (0, 0)
let (sub_w, sub_h) = (32, 32);
let mut rgb_subimage = vec![0u8; sub_w * sub_h * 3];
rgb_subimage[0] = 250;

let mut finder = SubImageFinderState::new();
// These are (x, y, distance) where x and y are the position within the larger image
// and distance is the distance value, where a smaller distance means a more precise match
let positions: &[(usize, usize, f32)] =
    finder.find_subimage_positions((&rgb_image, w, h), (&rgb_subimage, sub_w, sub_h), 3);
let max: Option<&(usize, usize, f32)> = positions
    .iter()
    .min_by(|(_, _, dist), (_, _, dist2)| dist.partial_cmp(dist2).unwrap());
println!("The subimage was found at position {:?}", &max);
assert_eq!(Some((50, 0)), max.map(|max| (max.0, max.1)));
// find_subimage_positions actually returns the results sorted by distance already,
// so we can skip finding the minimum
assert_eq!(Some((50, 0)), positions.get(0).map(|max| (max.0, max.1)));

更多文档可以在生成的rustdoc文档中找到。

以下是一个使用此crate可以完成的示例

我们的搜索图像

Ferris

我们的子图像

A crop of ferris' eyes

最佳匹配

Ferris again but with a red rectangle over their eyes

许可协议

此仓库中的代码可在以下任一许可协议下使用,由您选择:MIT OR Apache-2.0 OR BSL-1.0 OR MPL-2.0 OR Zlib OR Unlicense

此crate可选地依赖于opencv。您可以在此处找到其许可协议(取决于版本,3-clause BSD或Apache 2)。Rust绑定以MIT许可。

它还可选地依赖于simdeez,该协议为MIT。

运行测试

我建议以发布模式运行测试。

依赖项

~0.5–1MB
~16K SLoC