Windows11 + WSL2 + Rust + OpenCV

Uncategorized
690 words

Rust で OpenCV のテンプレートマッチングをする方法

環境

  • Windows 11 Home 22H2

  • WSL2

  • Ubuntu

  • Visual Studio Code

事前に WSL2 を有効にして、Ubuntu をインストールしておきます。

インストール

最初に Ubuntu のパッケージを最新化しておきます。

1
2
sudo apt update
sudo apt upgrade

Ubuntu に Rust をインストール

1
2
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
sudo apt install rust-all

Ubuntu に OpenCV をインストール

1
sudo apt install libopencv-dev clang libclang-dev

Visual Studio Code に WSL用の拡張機能をインストール

WSL

VSCode から WSL へ接続します。

Rust

プロジェクトの作成

次のコマンドで Rustプロジェクトを作成します。

1
cargo new rust-opencv-1

依存関係

OpenCV を依存関係に追加します。

1
2
[dependencies]
opencv = { version = "0.78.0" }

コード

次のコードをコピペします。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
use opencv::{imgproc, imgcodecs, core};

fn main() {
// テンプレートマッチングを実行するための画像とテンプレート画像を読み込みます。
let image = imgcodecs::imread("./image.png", imgcodecs::IMREAD_COLOR).unwrap();
let template = imgcodecs::imread("./template.png", imgcodecs::IMREAD_COLOR).unwrap();

// 画像のグレースケール化を行います。
let mut gray_image = core::Mat::default();
imgproc::cvt_color(&image, &mut gray_image, imgproc::COLOR_BGR2GRAY, 0).unwrap();
let mut gray_template = core::Mat::default();
imgproc::cvt_color(&template, &mut gray_template, imgproc::COLOR_BGR2GRAY, 0).unwrap();

// テンプレートマッチングを実行します。
let mut result = core::Mat::default();
imgproc::match_template(&gray_image, &gray_template, &mut result, imgproc::TM_CCOEFF_NORMED, &core::no_array()).unwrap();

// テンプレートマッチングの結果を取得します。
let mut max_val = 0.0;
let mut max_loc = core::Point::new(0, 0);
core::min_max_loc(&result, None, Some(&mut max_val), None, Some(&mut max_loc), &core::no_array()).unwrap();
println!("Max Location: {} {:?}", max_val, max_loc);
}

実行

1
cargo run

テンプレートマッチングした座標が出力されれば成功です。

エラー1

このエラーは、Rustプロジェクトの実行権限か所有権がないことを示しています。

1
error: Permission denied (os error 13) at path "/home/you/rust-opencv-1/********"

次のコマンドを実行して、ディレクトリの所有者を自分のユーザー名に変更する。

1
sudo chown -R [ユーザー名] /home/you/rust-opencv-1

エラー2

このエラーは、OpenCVをビルドするための pkg-config がインストールされていないことを示しています。

1
2
3
4
5
6
=== Detected probe priority based on environment vars: pkg_config: false, cmake: false, vcpkg: false
=== Probing the OpenCV library in the following order: environment, pkg_config, cmake, vcpkg_cmake, vcpkg
=== Can't probe using: environment, continuing with other methods because: Some environment variables are missing
=== Probing OpenCV library using pkg_config
=== Can't probe using: pkg_config, continuing with other methods because: Could not run `"pkg-config" "--libs" "--cflags" "opencv4"`
The pkg-config command could not be found.

次のコマンドを実行して、pkg-config をインストールする。

1
sudo apt-get install pkg-config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
you@DESKTOP-INFS1IG:~/rust-opencv-1$ sudo apt-get install pkg-config
[sudo] password for you:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
pkg-config
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 48.2 kB of archives.
After this operation, 134 kB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 pkg-config amd64 0.29.2-1ubuntu3 [48.2 kB]
Fetched 48.2 kB in 1s (40.3 kB/s)
Selecting previously unselected package pkg-config.
(Reading database ... 36286 files and directories currently installed.)
Preparing to unpack .../pkg-config_0.29.2-1ubuntu3_amd64.deb ...
Unpacking pkg-config (0.29.2-1ubuntu3) ...
Setting up pkg-config (0.29.2-1ubuntu3) ...
Processing triggers for man-db (2.10.2-1) ...

おわりに

今回のプロジェクトを GitHub に上げました。

https://github.com/noitaro/rust-opencv-1