『Ponge画像類似度調査』を作った


 最終的には「似たような画像を消すソフト」が欲しいので、前段階として作った。

テスト

DALL·E 2023-01-31 16.42.49.png
DALL·E 2023-01-31 16.44.16.png

 上の2つの画像をDALL·E 2で作成。



 同じ画像だと類似度は100。



 違う画像だと71くらい。


類似度95以上で同じ画像とみなしてもいいかもしれない。

ソフト置き場

drive.google.com

コード

//MIT License

//Copyright (c) 2017 Coen Munckhof

//Permission is hereby granted, free of charge, to any person obtaining a copy
//of this software and associated documentation files (the "Software"), to deal
//in the Software without restriction, including without limitation the rights
//to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//copies of the Software, and to permit persons to whom the Software is
//furnished to do so, subject to the following conditions:

//The above copyright notice and this permission notice shall be included in all
//copies or substantial portions of the Software.

//THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//SOFTWARE.


using CoenM.ImageHash;
using CoenM.ImageHash.HashAlgorithms;
using Microsoft.Win32;
using System.IO;
using System.Windows;

namespace Ponge画像類似度調査
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Btn_CalcSimilarity_Click(object sender, RoutedEventArgs e)
        {
            var hashAlorithm = new PerceptualHash();

            var fstream1 = File.OpenRead(this.Path1.Text);
            var fstream2 = File.OpenRead(this.Path2.Text);

            var hash1 = hashAlorithm.Hash(fstream1);
            var hash2 = hashAlorithm.Hash(fstream2);

            var similarity = CompareHash.Similarity(hash1, hash2);

            fstream1?.Dispose();
            fstream2?.Dispose();

            this.SimilarityValue.Content = similarity;
        }

        private void Path1_GotFocus(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog();

            dialog.Filter = "全てのファイル (*.*)|*.*";

            if (dialog.ShowDialog() == true)
            {
                this.Path1.Text = dialog.FileName;
            }
        }

        private void Path2_GotFocus(object sender, RoutedEventArgs e)
        {
            var dialog = new OpenFileDialog();

            dialog.Filter = "全てのファイル (*.*)|*.*";

            if (dialog.ShowDialog() == true)
            {
                this.Path2.Text = dialog.FileName;
            }
        }
    }
}

ImageHash

github.com
 ライセンスはMIT。
画像をハッシュ化して類似度を求めるならコレ、らしい。
NuGetでインストール後、VisualStudioからライセンスを見ると謎の歯抜け状態になってて困った。
結局GitHubからライセンス表記を取ってきた。