rust - ArrayFire match_template 之后如何获得像素坐标 X、Y?

标签 rust template-matching arrayfire

我正在尝试使用 ArrayFire 库中的 matching_template 函数,但我不知道如何找到最佳匹配值的 X 和 Y 坐标。 我正在使用 imageproc 库来执行此功能,它具有 find_extremes 函数,可将坐标返回给我。你会如何使用 ArrayFire 库来做同样的事情?

我使用 imageproc 的示例

    let template = image::open("connect.png").unwrap().to_luma8();
    let screenshot = image::open("screenshot.png").unwrap().to_luma8();

   let matching_probability= imageproc::template_matching::match_template(&screenshot, &template, MatchTemplateMethod::CrossCorrelationNormalized);
   let positions = find_extremes(&matching_probability);

 println!("{:?}", positions);

Extremes { max_value: 0.9998113, min_value: 0.42247093, max_value_location: (843, 696), min_value_location: (657, 832) }

我使用 ArrayFire 的示例

   let template: Array<u8> = arrayfire::load_image(String::from("connect.png"), true);
   let screenshot: Array<u8> = arrayfire::load_image(String::from("screenshot.png"), true);

   let template_gray = rgb2gray(&template, 0.2126, 0.7152, 0.0722);
   let screen_gray =  rgb2gray(&screenshot, 0.2126, 0.7152, 0.0722);
   let matching_probability = arrayfire::match_template(&screen_gray, &template_gray, arrayfire::MatchType::LSAD);

af_print!("{:?}", matching_probability);

139569.0469 140099.2500 139869.8594 140015.7969 140680.9844 141952.5781 142602.7344 142870.7188...

从这里我不知道如何获得最匹配的像素坐标。

最佳答案

Arrayfire 不提供“极值”函数,而是将最小和最大函数族分开。

提供索引信息的前缀是i

imin_allimax_all 分别返回包含在元组中的最小值和最大值索引。

知道 arrayfire 是主要列,您可以从值索引和数组维度中得出像素位置。

let template: Array<u8> = arrayfire::load_image(String::from("connect.png"), true);
let screenshot: Array<u8> = arrayfire::load_image(String::from("screenshot.png"), true);

let template_gray = rgb2gray(&template, 0.2126, 0.7152, 0.0722);
let screen_gray = rgb2gray(&screenshot, 0.2126, 0.7152, 0.0722);
let matching_probability = arrayfire::match_template(&screen_gray, &template_gray, arrayfire::MatchType::LSAD);

let (min, _, min_idx) = imin_all(&matching_probability);
let (max, _, max_idx) = imax_all(&matching_probability);

let dims = matching_probability.dims();
let [_, height, _, _] = dims.get();

let px_x_min = min_idx as u64 / height;
let px_y_min = min_idx as u64 % height;

let px_x_max = max_idx as u64 / height;
let px_y_max = max_idx as u64 % height;

af_print!("{:?}", matching_probability);
println!("Minimum value: {} is at pixel ({},{}).",min, px_x_min, px_y_min);
println!("Maximum value: {} is at pixel ({},{}).", max, px_x_max, px_y_max);

关于rust - ArrayFire match_template 之后如何获得像素坐标 X、Y?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70006862/

相关文章:

rust - 如何在每个文件的基础上执行 `cargo bench`?

rust - 在 for 循环中使用正则表达式

rust - 插入任意嵌套的HashMap

python - 使用 Python/OpenCV 从图像中提取固定数量的正方形

julia - 在 Julia 中通过 ArrayFire 在 GPU 上编写代码的最佳方式

complex-numbers - 在 ArrayFire 中创建虚数单位

c++ - 是否可以在 GPU 中运行一段纯 C++ 代码

windows - 在临时目录中创建文件时的“The process cannot access the file because it is being used by another process”

java - 如何检测图片中图片位置可能发生变化的子集图像

python - 在 OpenCV Python 中与多个对象进行模板匹配