-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwximg_compare.py
125 lines (105 loc) · 4.27 KB
/
wximg_compare.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
Weathercam image comparison software
By Jan Niklas Lorenz - June 2024
"""
import cv2 as cv
import pandas as pd
import numpy as np
#---CONFIG---
# OUTPUT OPTIONS
# single database-match output [False] or list output [True]?
listOutput = False
# INPUT OPTIONS
# path to input picture
picturePath = '/home/jan/wximg-compare/input/input.jpg'
# path to database and name of the weathertag file
databasePath = '/home/jan/wximg-compare/database/'
databaseFileSky = 'reference_Sky.csv'
databaseFileGnd = 'reference_Gnd.csv'
databaseFileDist = 'reference_Dist.csv'
# picture masks
# sky
sky_xmin = 0
sky_xmax = 1920
sky_ymin = 0
sky_ymax = 470
# ground
gnd_xmin = 0
gnd_xmax = 1600
gnd_ymin = 800
gnd_ymax = 1080
# distance
dist_xmin = 800
dist_xmax = 1600
dist_ymin = 500
dist_ymax = 800
# OTHER SETTINGS
# histogram compare method, see OpenCV documetation for details
histogramCompareMethod = cv.HISTCMP_CORREL
#------------
# import the input image
inputpic = cv.imread(picturePath)
inputSky = inputpic[sky_ymin:sky_ymax, sky_xmin:sky_xmax]
inputGnd = inputpic[gnd_ymin:gnd_ymax, gnd_xmin:gnd_xmax]
inputDist = inputpic[dist_ymin:dist_ymax, dist_xmin:dist_xmax]
# read the database, insert colum names and add colum for similarity value
databaseSky = pd.read_csv(databasePath + databaseFileSky, index_col='file')
databaseSky.insert(4, 'similarity', 0.0)
databaseGnd = pd.read_csv(databasePath + databaseFileGnd, index_col='file')
databaseGnd.insert(4, 'similarity', 0.0)
databaseDist = pd.read_csv(databasePath + databaseFileDist, index_col='file')
databaseDist.insert(4, 'similarity', 0.0)
# calculates the BGR histograms for an input image, returns a list containing the B- G- and R-channel histograms
def histogram(img):
histB = cv.calcHist([img], [0], None, [256], [0,256])
cv.normalize(histB, histB, 0, 1, cv.NORM_MINMAX)
histG = cv.calcHist([img], [1], None, [256], [0,256])
cv.normalize(histG, histG, 0, 1, cv.NORM_MINMAX)
histR = cv.calcHist([img], [2], None, [256], [0,256])
cv.normalize(histR, histR, 0, 1, cv.NORM_MINMAX)
hist = [histB, histG, histR]
return hist
# compares the two image-objects given in file1 and file2
def compareimage(file1, file2):
hist = histogram(file1)
histComp = histogram(file2)
similarityB = cv.compareHist(hist[0], histComp[0], histogramCompareMethod)
similarityG = cv.compareHist(hist[1], histComp[1], histogramCompareMethod)
similarityR = cv.compareHist(hist[2], histComp[2], histogramCompareMethod)
similarity = [similarityB, similarityG, similarityR]
return similarity
# compares the input image to all images in the database and adds the similarity value to the dataframe
for pictureSky in databaseSky.index.to_list():
comparepicSky = cv.imread(databasePath + pictureSky)[sky_ymin:sky_ymax, sky_xmin:sky_xmax]
databaseSky.at[pictureSky,'similarity'] = np.mean(compareimage(inputSky, comparepicSky))
for pictureGnd in databaseGnd.index.to_list():
comparepicGnd = cv.imread(databasePath + pictureGnd)[gnd_ymin:gnd_ymax, gnd_xmin:gnd_xmax]
databaseGnd.at[pictureGnd,'similarity'] = np.mean(compareimage(inputGnd, comparepicGnd))
for pictureDist in databaseDist.index.to_list():
comparepicDist = cv.imread(databasePath + pictureDist)[dist_ymin:dist_ymax, dist_xmin:dist_xmax]
databaseDist.at[pictureDist,'similarity'] = np.mean(compareimage(inputDist, comparepicDist))
# create List of [outputNumber] most-similar database-enteries
maxSimilarListSky = databaseSky.sort_values(by='similarity', ascending=False)
maxSimilarListGnd = databaseGnd.sort_values(by='similarity', ascending=False)
maxSimilarListDist = databaseDist.sort_values(by='similarity', ascending=False)
# list output or singel-match output
if (listOutput):
print('')
print('Sky :')
print(maxSimilarListSky)
print('')
print('Distance :')
print(maxSimilarListDist)
print('')
print('Ground :')
print(maxSimilarListGnd)
else:
print(maxSimilarListSky.iloc[0,0], end='')
if(maxSimilarListSky.iloc[0,0]!='night'):
if(maxSimilarListDist.iloc[0,0]!='default'):
print(', ', end='')
print(maxSimilarListDist.iloc[0,0], end='')
if(maxSimilarListGnd.iloc[0,0]!='default'):
print(', ', end='')
print(maxSimilarListGnd.iloc[0,0], end='')
print('')