水曜日のダウンタウンに出てきた
「じゃねーよカルタ」

このカルタに出てきた人の顔
誰が一番春菜さんの顔と近いのかを
調べてみました。


解説動画はこちら



今回は「じゃねーよカルタ」に出てきた人の
顔の画像と近藤春菜さんの顔の画像を
比べて、誰が一番近いのかを検証しました。

画像の類似度の判定には
opencvのAKAZE特徴量を用いて
判定することとします。

さて今回の比較対象はこの5名

download-2

ハロネン元大統領
角野卓造
森永卓郎
和泉節子
マイケル・ムーア監督
です。

どんな感じに比較しているのか
AKAZEを使って特徴点を見てみましょう。

import matplotlib.pyplot as plt
import cv2
import os
%matplotlib inline

img1 = cv2.imread('画像パス') 
img2 = cv2.imread('画像パス') 
gray1 = cv2.cvtColor(img1,cv2.COLOR_BGR2GRAY) 
gray2 = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) 
akaze = cv2.AKAZE_create() 
kp1, des1 = akaze.detectAndCompute(gray1,None) 
kp2, des2 = akaze.detectAndCompute(gray2,None) 
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(des1, des2)
matches = sorted(matches, key = lambda x:x.distance)
img3 = cv2.drawMatches(img1, kp1, img2, kp2, matches[0:10], None, 
                       flags = cv2.DrawMatchesFlags_NOT_DRAW_SINGLE_POINTS)
plt.figure(figsize=(10,10))
plt.imshow(cv2.cvtColor(img3, cv2.COLOR_BGR2RGB))
plt.axis("off")
plt.show()
download

こんな感じで似ている点を出しています。

今回比較する春菜さんの顔はこちら

download-1

先ほどの5名と
和泉節子さん風に変装した春菜さんの
6名で比較して、一番近い顔の人を出してみます。

対象の人の顔画像は
画像ディレクトリに入れています。

判定に使ったコードはこちら
dir_path = '画像ディレクトリ'
img_size = (300,300)
target_img_path = '画像パス'
target_img = cv2.imread(target_img_path, cv2.IMREAD_GRAYSCALE)
target_img = cv2.resize(target_img, img_size)
bf = cv2.BFMatcher(cv2.NORM_HAMMING)
detector = cv2.AKAZE_create()
(target_kp, target_des) = detector.detectAndCompute(target_img, None)

files = os.listdir(dir_path)
result = {}
for file in files:
    img_path = dir_path + file
    try:
        img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
        img = cv2.resize(img, img_size)
        (kp, des) = detector.detectAndCompute(img, None)
        matches = bf.match(target_des, des)
        dist = [m.distance for m in matches]
        ret = sum(dist) / len(dist)
        result[file.replace('.png','')] = ret
    except cv2.error:
        ret = 999999

for k,v in sorted(result.items(),reverse=True,key=lambda x:x[1]):
    img = cv2.imread('img/{0}.png'.format(k)) 
    plt.figure(figsize=(8,8))
    plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    plt.title('{0} : {1:.06}'.format(k , v),fontsize=20)
    plt.axis("off")
    plt.show()


結果は動画の方で
お楽しみくださいませ。

結構意外な結果が出ましたね!!!

opencvを使うと
画像の特徴量を比較して
似ているかどうかを数値化する事ができます。

面白いので試してみてくださいね。
今回はこれまでで
それでは。