compare_frames
2 つのフレームを比較し、同一であれば true を返すサブルーチンがあるとします。
次に、フォルダー 2 の最初の数フレームをループし、フレームを比較することで、オフセットを見つけることができます。
# return the offset of the first equal image,
# searching frames between "from" (defaulting to 1) and "upto" (defaulting to 15)
sub find_offset (%) {
my ($min_index, $max_index) = @{+{ from=>1, upto=>15, @_ }}{qw(from upto)};
for my $index ($min_index .. $max_index) { # assuming 1-based indexing scheme
return $index - 1 if compare_frames(1, $index);
}
warn "no offset found";
return undef; # or better, exit the program.
}
スクリプトの主要部分では、それを実行my $offset = find_offset upto=>15
して、後で処理の大部分に使用します。ただし、これは、フォルダー 1 が常にフォルダー 2 のサブセットであることを前提としています。
比較サブルーチンでは、できるだけ早く「false」を返したいと考えています。画像形式によっては、サイズが異なる場合、2 つの画像を同等にすることはできないと言えます。ただし、ビットマップのサイズは固定されているため、すべてのフレームの解像度が同じであるため、ここでは意味がありません。他の画像形式では圧縮密度が異なり、一方の画像がより圧縮されていても、2 つの画像は同等である場合があります。stat
このような場合は、コードを削除してください。
# decide if two frames are equal.
# $frame1 is from the first set of frames,
# $frame2 is the frame from the second set.
sub compare_frames {
my ($frame1, $frame2) = @_;
my $file1 = "folder1/$frame1";
my $file2 = "folder2/$frame1";
# stat the files for the size.
my $size1 = (stat $file1)[7];
my $size2 = (stat $file2)[7];
return 0 if $size1 != $size2; # strict equality, maybe you want to include some sort of tolerance.
# do main comparision
$cmp->set_image1($file1);
$cmp->set_image2($file2);
return $cmp->compare;
}
ここで、$cmp
は からのコンパレータ オブジェクトImage::Compare
であり、事前設定された比較メソッドです。
次に、メインの比較中に、compare_frames($i, $i + $offset)
.