-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab_18.py
66 lines (49 loc) · 1.64 KB
/
lab_18.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#! /usr/bin/python
# autogenerated on 2020-05-28 17:10
# Local Image Descriptors
import cv2
import numpy as np
import os
from utils import *
def sub_histogram(img, size=4):
height, width, channels = img.shape
height //= size
width //= size
output = np.zeros((size, size, channels, 256), dtype=np.float32)
for i in range(size):
for j in range(size):
for c in range(channels):
tmp = img[height * i : height * (i + 1), width * j : width * (j + 1), c]
tmp = get_histogram(tmp, True)
output[i, j, c] = tmp
return output
def distance(mat_hist_a, mat_hist_b):
tmp = 0
for row_a, row_b in zip(mat_hist_a, mat_hist_b):
for a, b in zip(row_a, row_b):
tmp += cv2.compareHist(a, b, cv2.HISTCMP_BHATTACHARYYA)
return tmp
def search_similar(img, size, path):
color = len(img.shape) == 3
img_sub_hist = sub_histogram(img, size)
min_err = -1
min_img = None
for entry in os.scandir(path):
if entry.is_file:
name = entry.path
tmp = load_image(name, color=color)
tmp_sub_hist = sub_histogram(tmp, size)
err = distance(img_sub_hist, tmp_sub_hist)
print(err, name)
if min_err > err or min_err == -1:
min_err = err
min_img = name
return min_img
def main():
img = load_image_from_arg(color=True)
similar = search_similar(img, size=5, path="images")
print(similar)
similar = load_image(similar, color=True)
show_image(("given", img), ("similar", similar), wait=10)
if __name__ == "__main__":
main()