AspectCrop

これらのキーワードがハイライトされています:

概要

「アスペクト比を狂わせずにクロップする関数」です。

作者は、2ちゃんねるDTV板「お前らのショボイAvisynthスクリプト貼ってください」スレッドの230さんです。

関数

function AspectCrop(clip clip, int "left", int "top", int "right", int "bottom",
\int "minwidth", int "minheight", int "aspectx", int "aspecty",
\string "clrfmt", bool "interlaced", bool "cutoff", bool "debug")
{
left = default(left, 0)
top = default(top, 0)
right = Abs(default(right, 0))
bottom = Abs(default(bottom, 0))
minwidth = default(minwidth, 0)
minheight = default(minheight, 0)
aspectx = default(aspectx, 711)
aspecty = default(aspecty, 485)
clrfmt = default(clrfmt, "")
interlaced = default(interlaced, true)
cutoff = default(cutoff, true)
debug = default(debug, false)

unitx = clrfmt == "I420" ? 4 : ((clip.IsYV12 || clip.IsYUY2 || clrfmt == "YV12" || clrfmt == "YUY2") ? 2 : 1)
unity = (interlaced || clip.IsYV12 || clrfmt == "YV12") ? 2 : 1
rleft = Floor((left + (cutoff ? unitx - 1 : 0)) / unitx) * unitx
rtop = Floor((top + (cutoff ? unity - 1 : 0)) / unity) * unity
rright = Floor((right + (cutoff ? unitx - 1 : 0)) / unitx) * unitx
rbottom = Floor((bottom + (cutoff ? unity - 1 : 0)) / unity) * unity
w = clip.Width - left - right
h = clip.Height - top - bottom
wy = w * aspecty
hx = h * aspectx
amul = cutoff ? (wy < hx ? wy : hx) : (wy < hx ? hx : wy)
wy = minwidth * aspecty
hx = minheight * aspectx
amul = amul < wy ? wy : (amul < hx ? hx : amul)
wy = clip.Width * aspecty
hx = clip.Height * aspectx
amul = amul > wy ? wy : (amul > hx ? hx : amul)
rw = amul / aspecty
rh = amul / aspectx
rw = Floor((rw + (cutoff ? (((w - rw) > 3) ? 3 : w - rw) : 3)) / 4) * 4
rh = Floor((rh + ((cutoff && rh > h) ? 0 : 1)) / 2) * 2
rw = rw < minwidth ? minwidth : rw
rh = rh < minheight ? minheight : rh
rleft = Floor((rleft + (w - rw) / 2) / unitx) * unitx
rright = clip.Width - rw - rleft
rtop = Floor((rtop + (h - rh) / 2) / unity) * unity
rbottom = clip.Height - rh - rtop

clip = clip.Crop(rleft, rtop, rw, rh)
clip = debug ? clip.SubTitle("unitx : " + String(unitx), 10, 20) : clip
clip = debug ? clip.SubTitle("unity : " + String(unity), 10, 40) : clip
clip = debug ? clip.SubTitle("left : " + String(rleft), 10, 60) : clip
clip = debug ? clip.SubTitle("top : " + String(rtop), 10, 80) : clip
clip = debug ? clip.SubTitle("right : " + String(rright), 10, 100) : clip
clip = debug ? clip.SubTitle("bottom : " + String(rbottom), 10, 120) : clip
clip = debug ? clip.SubTitle("width : " + String(clip.Width) + "(" + String(Float(amul) / aspecty) + ")", 10, 140) : clip
clip = debug ? clip.SubTitle("height : " + String(clip.Height) + "(" + String(Float(amul) / aspectx) + ")", 10, 160) : clip
return clip
}

書式

AspectCrop(clip clip, int "left", int "top", int "right", int "bottom", int "minwidth", int "minheight", int "aspectx", int "aspecty", string "clrfmt", bool "interlaced", bool "cutOff", bool "debug")

引数

  • left
    • 切り抜く左端の長さ。デフォルトは0。
  • top
    • 切り抜く上端の長さ。デフォルトは0。
  • right
    • 切り抜く右端の長さ。デフォルトは0。
  • bottom
    • 切り抜く下端の長さ。デフォルトは0。
  • minwidth
    • 最低限保持する幅。デフォルトは0。
  • minheight
    • 最低限保持する高さ。デフォルトは0。
  • aspectx
  • aspecty
    • アスペクト比の指定。縦横比がaspectx:aspectyになるように調整。デフォルトは711:485。
  • clrfmt
    • クロップする際、現在の色空間の制限以外にこの色空間の制限を加える。デフォルトは指定なし。
  • interlaced
    • trueで縦の切り抜き開始終了位置を偶数にする。YV12だと意味無し。デフォルトはtrue。
  • cutOff
    • trueで黒縁がなくなるまで切り抜き、falseで黒縁をギリギリまで削る。デフォルトはtrue。
  • debug
    • trueでクロップする際に使った引数などを表示。デフォルトはfalse。

使用例

1.ソースクリップ

aspectcrop001_source_s.png

なるべくアスペクト比を保持したまま、こちらのクリップ(720x480*1)の黒縁をCropします。

2.debug=trueのみ

まずは、debug=trueとだけ指定します。スクリプトは、以下の通り。

AspectCrop(debug=true)

プレビューすると、クリップの左上にデバッグ情報が表示されます*2

aspectcrop002_debug_true_s.png

デフォルトでは、aspectx:aspecty=711:485に設定されています。このため、711:485に近い704:480になるように、自動的に左右8ずつCropされます。

他のアスペクト比で計算したい場合は、aspectxとaspectyを指定する必要があります。

なお、unitxは横方向、unityは縦方向における、Crop幅の制限値を表しています。デフォルトでは、クリップの色空間によって、自動的に決定されます*3。引数clrfmtを指定することによって、unitxとunity変更することも可能です。

3.引数の調整

AspectCrop(6,0,10,0, debug=true)

右端の黒縁が残っていたので、left=6, right=10(左右合計は16のまま)に変更してみました。

aspectcrop004_6_0_10_0_s.png

これで左右の黒縁は見えなくなりました。次に、上下の黒縁をCropします。

AspectCrop(6,8,10,4, debug=true)

top=8, bottom=4に変更しました。

aspectcrop005_6_8_10_4_s.png

上下の黒縁もなくなりました。

ただし、アスペクト比を保持するために、左右がさらにCropされている点に注意してください。

4.設定終了(debug=false)

納得の行く状態になるまで微調整を繰り返します。

調整が終わったら、debug=falseに変更するか、debugパラメータを省略します。

AspectCrop(14,8,18,4)

仮に、このような設定にしたものとします。

aspectcrop006_debug_false_s.png

設定終了です。


*1 このクリップのオリジナルの大きさは、720x480です。掲載にあたり、縮小処理を施しました。
*2 デバッグ情報の部分のみ拡大表示しています。
*3 例では、クリップがYUY2で、interlacedパラメータがtrue(デフォルト)であるため、unitxとunityは2に設定されています。

最終更新日時: 2014-03-11 (火) 03:44:28 (3692d)