ImageMagick内部入門第十六回、Distortion-1

478 Views

April 12, 24

スライド概要

ImageMagick の -distort オプションの解説

profile-image

バイナリはともだち。こわくないよ

シェア

またはPlayer版

埋め込む »CMSなどでJSが使えない場合

関連スライド

各ページのテキスト
1.

ImageMagick 内部入門 第十六回 (Distortion〜空間歪曲#1) SRT 〜 Perspective 2024/4/12(金) “よや” <yoya@awm.jp>

2.

内部入門シリーズ(1/2) • • • • • • • • • • • • (第一回) 全体構成 (第二回) データ構造 〜 ImageList, ImageInfo (第三回) Magick API 〜 MagickWand / MagickCore (第四回) 画像形式処理 〜 Coders と Delegate (第五回) メタデータ 〜 XMP, Profile (第六回) システム設定 〜 policy.xml 等 (第七回) 減色 〜 ColorCube (第八回) ビットdepth 〜 depth 変換 (第九回) 2値化 〜 threshold (第十回) ディザリング 〜 組織 (拡散ディザは延期) (第十一回) ICCプロファイル 〜 RGB  CMYK 色空間 (第十二回) 色フィルタ 〜 Colorize ColorMatrix

3.

内部入門シリーズ (2/2) • • • • (第十三回) (第十四回) (第十五回) (第十六回) 色modulate 〜 modulate (変調) WebP 画像 画像比較 〜 Compare 空間歪曲#1 〜 Distortion SRT, Affine, Perspective 今回の話 • (第十七回) 空間歪曲#2 〜 Distortion Shepards & Composite Displacement • (第十八回) 空間歪曲#3 〜 Distortion Resize • (第十九回) 空間歪曲#4 〜 Distortion ARC, Polar, Barrel • (第二十回) 畳み込み 〜 Convolve • (第N回) 各画像処理 • 畳み込み、モルフォロジー、OpenMP, distribute-pixel-cache

4.

目次 • Distortion とは • Distortion の -/+ オプション • Distortion の種類 • Distortion の基礎 • SRT (ScaleRotateTranslate) • Affine / Helmert • Homography / Perspective / Bilinear

5.

-distort オプション • 画像を空間的に歪ませるメソッド -distort <method> <arguments> # bestfit:False +distort <method> <arguments> # bestfit:True • 分かりやすいのは Scale(拡大縮小)/Rotate(回転)/Translate(移動) # (0,0)を原点として、1倍スケール、45度回転、(200,0)移動 % magick logo: -distort SRT 0,0,1,45,200,0 output.jpg

6.

-distort/+distort オプション • -/+distort の違い # 画像が収まるようキャンバスを調整 (bestfit) % magick logo: +distort SRT 200,0,1,45 output.jpg # 元のキャンバスを維持する % magick logo: -distort SRT 200,0,1,45 output.jpg

7.

Distortion の種類 • magick –list distort で一覧表示。今回は SRT と Perspective 線形 % magick -list distort | column Affine PerspectiveProjection DePolar RigidAffine BilinearForward Barrel AffineProjection BilinearReverse BarrelInverse ScaleRotateTranslate Polynomial Shepards SRT Arc Resize Perspective Polar Affine RigidAffine AffineProjection ScaleRotateTranslate SRT Resize 射影 Perspective PerspectiveProjection BilinearForward BilinearReverse Polynomial 円環 Arc Polar DePolar レンズ歪み Barrel BarrelInverse 任意の点 Shepards

8.

Distortion 実装コード • https://github.com/ImageMagick/ImageMagick/blob/main/ MagickCore/distort.c

9.

Distortion の基礎 • Forward or Direct Pixel Mapping • src(入力画像) の座標を計算して dst(変換後)を割り出してレンダリン グすると、dst 隙間ができる。 拡大 (ドットのイメージ) 回転

10.

Distortion の基礎 • Reverse Pixel Mapping 例えば 2倍だったら ½ の計算 • Destination 側の pixel 位置から逆の計算をして src を求める • さすれば Destination が必ず埋まる 逆回転の計算

11.

Distortion の基礎 • Distortion は常に逆問題を突きつけられる • 拡大 => 縮小 • 回転 => 逆回転 • 移動 => 逆移動 • 行列演算 => 逆行列 • アフィン変換 • 射影変換

12.

Distortion SRT • 引数1つは R(Rotate)、2つで S(Scale),R(Rotate)。原点中心 % magick logo: -distort SRT 45 output.jpg #中心を原点として、45度回転 % magick logo: -distort SRT 2,45 output.jpg #中心原点、2倍して45度回転 % magick logo: +distort SRT 2,45 output.jpg # 〃 、 + サイズ調整 +

13.

Distortion SRT (続き) • S,R だけだと原点が中心、T を追加するのはひと手間いる % magick logo: -distort SRT 2,45,200,0 output.jpg #中心原点、2倍、45度回転、(200,0)移動 #(2,45)原点、200倍、0度回転 # 中心(w/2,h/2)原点、2倍、45度回転、(200,0)移動 % magick logo: -distort SRT "%[fx:w/2],%[fx:h/2],2,45,200,0" output.jpg

14.

Distortion SRT • 引数の数で以下のように解釈される (正直トリッキー) 中心を原点に SR X,Yを原点に SR 1 -distort SRT " 2 -distort SRT " 3 -distort SRT "X,Y, R " # X,Y を原点にする 4 -distort SRT "X,Y,S, R " 5 -distort SRT "X,Y,Sx,Sy,R 6 -distort SRT "X,Y,S, 7 -distort SRT "X,Y,Sx,Sy,R,Xt,Yt" S, R " # R:Rotate (中心原点) R " # S:Scale (中心原点) " # Sx,Sy: xy別Scale R,Xt,Yt" # Xt,Yt は移動

15.

Distortion SRT • 引数処理

16.

アフィン(Affine)変換 • 3x3 行列で SRT を表現できる © https://en.wikipedia.org/wiki/Affine_transformation

17.

ヘルマート(Helmert)変換 • Sx=Sy (縦横比不変)かつ同符号 (反転なし) Shear/Skew(剪断) 不要ならヘルマート変換(相似変換) © https://it.wikipedia.org/wiki/Trasformazione_di_Helmert

18.

アフィン変換 • アフィン変換の行列係数を直接指定できる • 単位行列の例 1 0 0 0 0 1 0 0 1 2 0 0 0 0 1 0 0 1 # 単位行列を指定 % magick logo: -distort AffineProjection "1,0, 0,1, 0,0”output.jpg # 横に2倍 % magick logo: +distort AffineProjection " 2,0, 0,1, 0,0”output.jpg

19.

• SRT もアフィン変換で処理してる

20.

アフィン変換の限界 • 見ての通り、平行四辺形までの変換 • 台形に変換するには射影変換が必要 • Homography (射影変換) • もしくは、 Perspective (遠近法) © https://en.wikipedia.org/wiki/Homography © https://zh.wikipedia.org/wiki/单应性

21.

射影変換(Homography)とは • 方程式 • 行列表現 • x, y に応じてスケールも変化させ、透視図法を表現する © https://speakerdeck.com/imagire/dan-wei-zheng-fang-xing-falseshe-ying-bian-huan-falsebian-huan-xi-shu

22.

Perspective • -distort Perspective は {src, dst} x 4組の8点指定 % convert sample.jpg -distort Perspective \ '173,73 0,0 457,77 640,0 420,409 640,480 129,356 0,480' \ pers.jpg 0,0 173,73 640,0 457,77 420,409 640,480 129,356 0,480

23.
[beta]
Perspective -verbose
• -verbose で射影変換の行列係数が出てくる
% convert sample.jpg –verbose -distort Perspective \
'173,73 0,0 457,77 640,0 420,409 640,480 129,356 0,480' \
pers.jpg
Perspective Projection:
-distort PerspectiveProjection \
'2.96457, 0.460922, -546.517, -0.0281816,
2.00089, -141.19, 0.00065016, 0.000276416'
Perspective Distort, FX Equivelent:
-fx 'ii=i+page.x+0.5; jj=j+page.y+0.5;
rr=-0.000220141*ii -8.74352e-05*jj + 1;
Reverse
xx=(+0.343145*ii -0.102946*jj +173)/rr;
Mapping
yy=(-0.0107009*ii +0.558456*jj +73)/rr;
rr>0 ? p{xx-page.x-0.5,yy-page.y-0.5} : blue' \
sample.jpg=>pers.jpg JPEG 640x480 640x480+0+0 8-bit sRGB 92709B 0.190u 0:00.027

24.

Perspective • 座標リストに最小二乗法&ガウスジョルダン消去法で係数算出

25.

射影変換の行列係数の導出 • https://speakerdeck.com/imagire/dan-wei-zheng-fang-xingfalseshe-ying-bian-huan-falsebian-huan-xi-shu 長々とした証明

26.

Bilinear • Perspective と引数は同じ • 中心を維持する。グリッドが等間隔。(遠くが縮まない) Original(元画像) Perspective(透視投影) Bilinear(双線形)

27.
[beta]
Bilinear -verbose
• -verbose で方程式が出てくる。
% % convert sample.jpg -verbose -distort Bilinear ¥
'173,73 0,0 457,77 640,0 420,409 640,480 129,356 0,480' ¥
𝑖 = 𝑎𝑥 + 𝑏𝑦 + 𝑐𝑥𝑦 + 𝑑
bili.jpg
𝑗 = 𝑒𝑥 + 𝑓𝑦 + 𝑔𝑥𝑦 + ℎ
BilinearForward Mapping Equations:
i = +2.275062*x +0.393838*y -0.000341*x*y -418.029409;
j = +0.042795*x +1.803775*y -0.000859*x*y -128.236482;
BilinearForward Distort, FX Equivalent:
-fx 'ii=i+page.x+418.529409; jj=j+page.y+128.736482;
bb=-0.000859*ii +0.000341*jj +4.086845;
Reverse
rt=bb*bb +0.001108*(0.042795*ii-2.275062*jj);
Mapping
yy=( -bb + sqrt(rt) ) / -0.000554;
( だいぶ複雑)
xx=(ii -0.393838*yy)/(2.275062 -0.000341*yy);
(rt < 0 ) ? red : p{xx-page.x-0.5,yy-page.y-0.5}'
sample.jpg=>bili.jpg JPEG 640x480 640x480+0+0 8-bit sRGB 91258B 0.070u 0:00.024

28.

Bilinear • Perspective と同様に行列を解くが、逆変換の式が複雑。

29.

参考 • https://www.imagemagick.org/Usage/distorts/ • https://en.wikipedia.org/wiki/Homography • https://speakerdeck.com/imagire/dan-wei-zheng-fang-xingfalseshe-ying-bian-huan-falsebian-huan-xi-shu • https://qiita.com/yoya/items/c54f3e2a77eef5468b1f • https://yoya.github.io/image.js/homography.html

30.

次回(以降)予告 • Displacement (ルックアップテーブルによる任意の変換) • Resize (実は –resize より高品質な –distort Resize) • ARC(円弧), Polar(極座標)