LoginSignup
2
2

More than 5 years have passed since last update.

コマンドラインで画像のPSNRを測定する

Posted at

ImageMagickに同梱されているcompareコマンドが簡単で便利。

compare -metric PSNR ref.jpg target.jpg diffpng

2つのフォルダに入っている画像を連続で比較するrubyスクリプトは下記の通り。
compareの結果はなぜか標準エラー出力に出てくるので注意。

#!/bin/env ruby
# -*- coding: utf-8 -*-

#
# Usage
#   $ ruby comp.rb TARGET_FOLDER
#

require 'rubygems'
require 'systemu'

# http://www.imagemagick.org/script/command-line-options.php#metric
COMPARE_METRIC = "PSNR"

REF_FOLDER = "RefFolder"
REF_FORMAT_EXT = ".bmp"
TARGET_FOLDER = ARGV[0]

Dir.glob(File.join(TARGET_FOLDER, "*")).sort.each do |target_file|
    ref_file = File.join(REF_FOLDER, File.basename(target_file, ".jpg")) + REF_FORMAT_EXT
    command = "compare -metric #{COMPARE_METRIC} #{ref_file} #{target_file} diff.png"

    # Metrics value is outputed to stderr somehow
    status, stdout, stderr = systemu command
    puts "#{target_file}\t#{stderr}"
end
2
2
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
2
2