3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

パノラマ画像や手ブレ補正に使えます

IMG_1399.JPG  IMG_1400.JPG
名称未設定.png

手順

2枚目の画像で登録リクエストを作成します。

let request = VNTranslationalImageRegistrationRequest(targetedCIImage: image2, options: [:])

1枚目の画像で実行します。

let handler = VNImageRequestHandler(ciImage: image1, options: [:])
do {
    try handler.perform([request])
} catch let error {
    print(error)
}

結果は2枚目の画像を1枚目の画像に位置合わせするためのCGAffineTransformで返されます。

guard let observation = request.results?.first as? VNImageTranslationAlignmentObservation else { return }
let alignmentTransform = observation.alignmentTransform
print(alignmentTransform)
// CGAffineTransform(a: 1.0, b: 0.0, c: 0.0, d: 1.0, tx: -777.0, ty: 74.0)

結果を元に、2枚の画像を合成します。

image2 = image2.transformed(by: alignmentTransform)
let compositedImage = image1.applyingFilter("CIAdditionCompositing", parameters: ["inputBackgroundImage": image2])

注意点

・何回か試してみたところ、画像内の顕著なオブジェクトを基準に合わせる傾向がある気がします。
・カメラで撮った画像の向きに注意しましょう。90度回転していたりします。
・水平移動には強いですが、カメラが回転すると、結果が変にずれやすいです。
上記のせいで不安定な挙動をすることがあります。

🐣


お仕事のご相談こちらまで
rockyshikoku@gmail.com

Core MLを使ったアプリを作っています。
機械学習関連の情報を発信しています。

Twitter
[MLBoysチャンネル]
(https://www.youtube.com/channel/UCbHff-wfjTnB3rtXIP6y0xg)
Medium

相棒
note

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?