Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ---- memo -----------------------
- # フォルダの中にある画像の中で
- # 類似したものを見つける
- # ---------------------------------
- import cv2
- from sys import argv
- from glob import glob
- from itertools import combinations
- import networkx as nx
- import subprocess as sb
- # global variances
- THRESHOLD = 0.15
- def usage():
- print('Usage:\n\tpython3 {f} folder_name'.format(f=__file__))
- exit()
- def matching():
- # 画像リストの作成
- Pictures = glob('{folder}/*'.format(folder=FolderName))
- # ヒストグラムの計算
- image_hists = dict()
- for picture in Pictures:
- im = cv2.imread(picture)
- image_hists[picture] = cv2.calcHist([im], [0], None, [256], [0, 256])
- # 類似度の計算
- result = list()
- for pictA, pictB in combinations(Pictures, 2):
- image_histA, image_histB = image_hists[pictA], image_hists[pictB]
- tmp = cv2.compareHist(image_histA, image_histB, 3)
- if tmp < THRESHOLD:
- result.append((tmp, pictA, pictB))
- # クラスタの計算
- G = nx.Graph()
- for _, nodeA, nodeB in result:
- G.add_edge(nodeA, nodeB)
- # print('類似度:{0:0.3f} 画像名:{1} {2}'.format(result[i][0], result[i][1], result[i][2]))
- # 結果の出力
- # 類似画像なし
- if G.number_of_nodes() == 0:
- print('Same pictures are not found.')
- exit()
- # 類似画像あり
- print('Maybe same pictures ...')
- for i, component in enumerate(nx.connected_components(G)):
- print('[{}] {}'.format(i, ' '.join(component)))
- # 画像をOpenするかどうか
- print('Open those?(y or n)')
- ans = input()
- if ans in ['y', 'yes']:
- for component in nx.connected_components(G):
- component = list(component)
- print(['open'] + component)
- sb.run(['open'] + component)
- return 1
- if __name__ == '__main__':
- if len(argv) == 2:
- FolderName = argv[1] if argv[1] != '/' else argv[1][:-1]
- matching()
- else:
- usage()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement