214 lines
7.2 KiB
Python
214 lines
7.2 KiB
Python
from compareImgs import compareImgs
|
|
import cv2 as cv
|
|
from glob import glob
|
|
import numpy as np
|
|
|
|
from write_csv import write_csv
|
|
|
|
|
|
# from compareImgs_hist import compareImgs_hist
|
|
from compareImgs_hist_rgb import compareImgs_hist_rgb
|
|
from SIFT import SIFT
|
|
from pprint import pprint
|
|
import os, sys
|
|
from countour_util import getContourDiff, checkCountourImg1
|
|
from color_layout_descriptor import getFullCLDDiff
|
|
from grayscale import grayscale
|
|
|
|
# the directory of the image database
|
|
database_dir = "image.orig"
|
|
img_src_path = ""
|
|
|
|
img_cache = {}
|
|
|
|
|
|
def retrieval(choice="1", para_test=0):
|
|
if choice == "1":
|
|
img_src_path = "beach.jpg"
|
|
img_src_rgb = cv.imread(img_src_path)
|
|
# print("You choose: %s - beach\n" % choice)
|
|
if choice == "2":
|
|
img_src_path = "building.jpg"
|
|
img_src_rgb = cv.imread(img_src_path)
|
|
# print("You choose: %s - building\n" % choice)
|
|
if choice == "3":
|
|
img_src_path = "bus.jpg"
|
|
img_src_rgb = cv.imread(img_src_path)
|
|
# print("You choose: %s - bus\n" % choice)
|
|
if choice == "4":
|
|
img_src_path = "dinosaur.jpg"
|
|
img_src_rgb = cv.imread(img_src_path)
|
|
# print("You choose: %s - dinosaur\n" % choice)
|
|
if choice == "5":
|
|
img_src_path = "flower.jpg"
|
|
img_src_rgb = cv.imread(img_src_path)
|
|
# print("You choose: %s - flower\n" % choice)
|
|
if choice == "6":
|
|
img_src_path = "horse.jpg"
|
|
img_src_rgb = cv.imread(img_src_path)
|
|
# print("You choose: %s - horse\n" % choice)
|
|
if choice == "7":
|
|
img_src_path = "man.jpg"
|
|
img_src_rgb = cv.imread(img_src_path)
|
|
# print("You choose: %s - man\n" % choice)
|
|
|
|
min_diff = 1e50
|
|
|
|
# src_input = cv.imread("man.jpg")
|
|
# cv.imshow("Input", src_input)
|
|
|
|
# read image database
|
|
database = sorted(glob(database_dir + "/*.jpg"))
|
|
|
|
weight_diff_gray = 0.3
|
|
weight_diff_rgb = 0.3
|
|
weight_diff_sift = 0.1
|
|
weight_diff_contour = 0
|
|
weight_diff_cld = 0.3
|
|
|
|
weight_diff_u = 0
|
|
weight_diff_d = 0
|
|
weight_diff_l = 0
|
|
weight_diff_r = 0
|
|
|
|
# check of contour applicable
|
|
check_contour = True
|
|
len_contour = len(checkCountourImg1(img_src_rgb)[0])
|
|
# len_contour = len(checkCountourImg1(img_src_rgb, 150)[0])
|
|
# len_contour_check_building = len(checkCountourImg1(img_src_rgb, 100)[0])
|
|
print(len_contour)
|
|
# sys.exit()
|
|
if len_contour == 0:
|
|
# no contour found
|
|
check_contour = False
|
|
|
|
else:
|
|
weight_diff_gray = 0.0
|
|
weight_diff_rgb = 0.1
|
|
weight_diff_sift = 0.1
|
|
weight_diff_contour = 0.75
|
|
weight_diff_cld = 0.05
|
|
|
|
weight_diff_u = 0
|
|
weight_diff_d = 0
|
|
weight_diff_l = 0
|
|
weight_diff_r = 0
|
|
|
|
# else:
|
|
# weight_diff_gray = 0.01
|
|
# weight_diff_rgb = 0.1
|
|
# weight_diff_sift = 0.0
|
|
# weight_diff_contour = 0.6
|
|
# weight_diff_cld = 0.2
|
|
|
|
# weight_diff_u = 0
|
|
# weight_diff_d = 0
|
|
# weight_diff_l = 0
|
|
# weight_diff_r = 0
|
|
|
|
print("contour mode applied")
|
|
|
|
raw_results = []
|
|
i = 0
|
|
# for img_test_path in database:
|
|
# for img_test_path in database[200:299] + database[600:699] + database[400:499] + database[700:799] + database[100:199]:
|
|
# for img_test_path in database[100:199] + database[500:599]:
|
|
for img_test_path in database[0:999]:
|
|
# for img_test_path in database[135:140]:
|
|
if img_test_path[-5:] == "0.jpg":
|
|
print("processing: " + img_test_path, end="\r")
|
|
|
|
img_test_rgb = None
|
|
# # read image
|
|
if img_test_path in img_cache:
|
|
img_test_rgb = img_cache[img_test_path]
|
|
else:
|
|
img_test_rgb = cv.imread(img_test_path)
|
|
img_cache[img_test_path] = img_test_rgb
|
|
|
|
# # convert to gray scale
|
|
# img_test_gray = cv.cvtColor(img_test_rgb, cv.COLOR_BGR2GRAY)
|
|
|
|
# change the image to gray scale
|
|
img_src_gray = grayscale(img_src_path)
|
|
img_test_gray = grayscale(img_test_path)
|
|
diff_gray = compareImgs(img_src_gray, img_test_gray)
|
|
|
|
# compare the two images by histogram, uncomment the following line to use histogram
|
|
diff_rgb = compareImgs_hist_rgb(img_src_path, img_test_path, 11)
|
|
|
|
diff_contour = getContourDiff(img_src_path, img_test_path) if check_contour else 0
|
|
|
|
d_cld_full, d_cld_up, d_cld_down, d_cld_left, d_cld_right = getFullCLDDiff(img_src_path, img_test_path, 8, 256)
|
|
|
|
sift_match = SIFT(img_src_path, img_test_path)
|
|
|
|
# sift_match = 1
|
|
raw_results.append(
|
|
[
|
|
img_test_path,
|
|
diff_gray,
|
|
diff_rgb,
|
|
sift_match,
|
|
diff_contour,
|
|
#
|
|
d_cld_full,
|
|
d_cld_up,
|
|
d_cld_down,
|
|
d_cld_left,
|
|
d_cld_right,
|
|
]
|
|
)
|
|
|
|
# if i > 3:
|
|
# break
|
|
i += 1
|
|
|
|
print("\nprocess done")
|
|
|
|
np_raw_results = np.array(raw_results)
|
|
np.savetxt("np_raw_results.csv", np_raw_results, delimiter=",", fmt="%s")
|
|
|
|
min_values = np.min(np_raw_results[:, 1:].astype(np.float64), axis=0)
|
|
max_values = np.max(np_raw_results[:, 1:].astype(np.float64), axis=0)
|
|
|
|
with np.errstate(divide="ignore", invalid="ignore"):
|
|
normalized_array = (np_raw_results[:, 1:].astype(np.float64) - min_values) / (max_values - min_values)
|
|
normalized_array = np.nan_to_num(normalized_array, nan=0.0)
|
|
|
|
np_raw_results = np.concatenate((np_raw_results, normalized_array), axis=1)
|
|
|
|
np.savetxt("np_raw_results_n.csv", np_raw_results, delimiter=",", fmt="%s")
|
|
|
|
start_col = 9
|
|
np_raw_results[:, start_col + 1] = np_raw_results[:, start_col + 1].astype(np.float64) * weight_diff_gray
|
|
|
|
np_raw_results[:, start_col + 2] = np_raw_results[:, start_col + 2].astype(np.float64) * weight_diff_rgb
|
|
np_raw_results[:, start_col + 3] = (1 - np_raw_results[:, start_col + 3].astype(np.float64)) * weight_diff_sift
|
|
if check_contour:
|
|
np_raw_results[:, start_col + 4] = np_raw_results[:, start_col + 4].astype(np.float64) * weight_diff_contour
|
|
else:
|
|
np_raw_results[:, start_col + 4] = 0
|
|
#
|
|
np_raw_results[:, start_col + 5] = np_raw_results[:, start_col + 5].astype(np.float64) * weight_diff_cld
|
|
np_raw_results[:, start_col + 6] = np_raw_results[:, start_col + 6].astype(np.float64) * weight_diff_u
|
|
np_raw_results[:, start_col + 7] = np_raw_results[:, start_col + 7].astype(np.float64) * weight_diff_d
|
|
np_raw_results[:, start_col + 8] = np_raw_results[:, start_col + 8].astype(np.float64) * weight_diff_l
|
|
np_raw_results[:, start_col + 9] = np_raw_results[:, start_col + 9].astype(np.float64) * weight_diff_r
|
|
|
|
diff_sums = np.sum(np_raw_results[:, 10:-1].astype(np.float64), axis=1)
|
|
|
|
np_raw_results = np.concatenate((np_raw_results, diff_sums[:, np.newaxis]), axis=1)
|
|
np_raw_results_sorted = np_raw_results[np.argsort(np_raw_results[:, -1].astype(np.float64))]
|
|
|
|
raw_results = np_raw_results_sorted.tolist()
|
|
|
|
min_raw_result = raw_results[0]
|
|
result = min_raw_result[0]
|
|
|
|
write_csv(raw_results, "raw_results.csv")
|
|
result_category = result.replace("image.orig/", "")[0]
|
|
|
|
print("r:" + result + " rc:" + result_category)
|
|
return result_category
|