ぱたへね

はてなダイアリーはrustの色分けができないのでこっちに来た

Rustでthe trait bound `ImageBuffer<Rgb<u8>, Vec<u8>>: image::image::GenericImage` is not satisfied

Rustでimageprocを使って画像処理に挑戦したときにはまったのでメモ。

ソースはこんな感じで、ファイルを開いてちょっと画像処理に挑戦してみました。

   let mut img_work = image::open(input_image_path).unwrap().to_rgb8();
   draw_filled_circle_mut(&mut img_work, (100, 100), 5, Rgb([255,0,0]));

buildしようとするとこのようなエラーがでて進みません。 検索しても良く分からない状態でした。

error[E0277]: the trait bound `ImageBuffer<Rgb<u8>, Vec<u8>>: image::image::GenericImage` is not satisfied
  --> src/chap2/lsm.rs:41:5
   |
41 |     draw_filled_circle_mut(&mut img_work, (100, 100), 5, Rgb([255,0,0]));
   |     ^^^^^^^^^^^^^^^^^^^^^^ the trait `image::image::GenericImage` is not implemented for `ImageBuffer<Rgb<u8>, Vec<u8>>`
   |
   = note: required because of the requirements on the impl of `Canvas` for `ImageBuffer<Rgb<u8>, Vec<u8>>`

Cargo.tomlはimageとimageprocの最新を指定しています。

[dependencies]
image = "0.24.1"
imageproc = "0.22.0"

どうもこの2つのバージョンが合っていないようですが、どう合わして良いのかわからず手こずりました。

まずは、Cargo.tomlからimageを消しimageprocだけでbuildしてみました。

[dependencies]
imageproc = "0.22.0"

実行結果

natu@Honoka:~/q/myproj/d3cv_handbook$ cargo clean
natu@Honoka:~/q/myproj/d3cv_handbook$ cargo build
   Compiling autocfg v1.1.0
   --- 省略 ----
  Compiling num v0.3.1
   Compiling image v0.23.14
   Compiling imageproc v0.22.0

この時にインストールされたバージョンのimageをCargo.tomlに指定すれば、上のソースが上手く動きました。

[dependencies]
#image = "0.24.1"
image = "0.23.14"
imageproc = "0.22.0"