Files
004_comission/vinniesniper-54816/task1/_lab/001-test-integration/retrieval copy 2.py
louiscklaw b1cd1d4662 update,
2025-01-31 22:56:58 +08:00

232 lines
8.3 KiB
Python

from compareImgs import compareImgs
import cv2 as cv
from glob import glob
# 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"):
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"))
# check of contour applicable
check_contour = True
len_contour = len(checkCountourImg1(img_src_rgb)[0])
if len_contour == 0:
# no contour found
check_contour = False
weight_diff_gray = 0.25
weight_diff_rgb = 0.25
weight_diff_sift = 0
weight_diff_contour = 0.25
weight_diff_cld = 0.25
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[000:999]:
# for img_test_path in database[135:140]:
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)
diff_contour = getContourDiff(img_src_rgb, img_test_rgb) if check_contour else 0
diff_cld_full, diff_cld_up, diff_cld_down, diff_cld_left, diff_cld_right = getFullCLDDiff(
img_src_path, img_test_path
)
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,
diff_cld_full,
diff_cld_up,
diff_cld_down,
diff_cld_left,
diff_cld_right,
]
)
# if i > 3:
# break
i += 1
print("\nprocess done")
np_raw_results = np.array(raw_results)
arr_normalized = np_raw_results / np_raw_results.sum(axis=0)
# normalize
min_diff_gray = min(raw_result[1] for raw_result in raw_results)
max_diff_gray = max(raw_result[1] for raw_result in raw_results)
min_diff_rgb = min(raw_result[2] for raw_result in raw_results)
max_diff_rgb = max(raw_result[2] for raw_result in raw_results)
min_diff_sift_match = min(raw_result[3] for raw_result in raw_results)
max_diff_sift_match = max(raw_result[3] for raw_result in raw_results)
min_diff_contour = min(raw_result[4] for raw_result in raw_results)
max_diff_contour = max(raw_result[4] for raw_result in raw_results)
min_diff_cld_full = min(raw_result[5] for raw_result in raw_results)
max_diff_cld_full = max(raw_result[5] for raw_result in raw_results)
min_diff_cld_up = min(raw_result[6] for raw_result in raw_results)
max_diff_cld_up = max(raw_result[6] for raw_result in raw_results)
min_diff_cld_down = min(raw_result[7] for raw_result in raw_results)
max_diff_cld_down = max(raw_result[7] for raw_result in raw_results)
min_diff_cld_left = min(raw_result[8] for raw_result in raw_results)
max_diff_cld_left = max(raw_result[8] for raw_result in raw_results)
min_diff_cld_right = min(raw_result[9] for raw_result in raw_results)
max_diff_cld_right = max(raw_result[9] for raw_result in raw_results)
for i in range(len(raw_results)):
normalize_contour = 0
if check_contour:
normalize_contour = (raw_results[i][4] - min_diff_contour) / (max_diff_contour - min_diff_contour)
raw_results[i] = [
raw_results[i][0],
(raw_results[i][1] - min_diff_gray) / (max_diff_gray - min_diff_gray),
(raw_results[i][2] - min_diff_rgb) / (max_diff_rgb - min_diff_rgb),
(raw_results[i][3] - min_diff_sift_match) / (max_diff_sift_match - min_diff_sift_match),
normalize_contour,
(raw_results[i][5] - min_diff_cld_full) / (max_diff_cld_full - min_diff_cld_full),
(raw_results[i][6] - min_diff_cld_up) / (max_diff_cld_up - min_diff_cld_up),
(raw_results[i][7] - min_diff_cld_down) / (max_diff_cld_down - min_diff_cld_down),
(raw_results[i][8] - min_diff_cld_left) / (max_diff_cld_left - min_diff_cld_left),
(raw_results[i][9] - min_diff_cld_right) / (max_diff_cld_right - min_diff_cld_right),
]
for raw_result in raw_results:
img_test_path, diff_gray, diff_rgb, sift_match, diff_contour, diff_cld_full = raw_result
w_diff_gray = weight_diff_gray * diff_gray
w_diff_rgb = weight_diff_rgb * diff_rgb
w_sift_match = weight_diff_sift * (1 - sift_match)
w_diff_contour = weight_diff_contour * diff_contour
w_diff_cld_full = weight_diff_cld * diff_cld_full
w_diff_cld_up = weight_diff_cld * diff_cld_up
w_diff_cld_down = weight_diff_cld * diff_cld_down
w_diff_cld_left = weight_diff_cld * diff_cld_left
w_diff_cld_right = weight_diff_cld * diff_cld_right
diff_sum = sum(
[
#
w_diff_gray,
w_diff_rgb,
# 1 - match = difference
w_sift_match,
w_diff_contour,
w_diff_cld_full,
]
)
raw_result.extend([w_diff_gray, w_diff_rgb, w_sift_match, w_diff_contour, w_diff_cld_full, diff_sum])
raw_results.sort(key=lambda raw_result: raw_result[-1])
# min_raw_result = min(raw_results, key=lambda raw_result: raw_result[-1])
min_raw_result = raw_results[0]
result = min_raw_result[0]
import csv
with open("raw_results.csv", mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(
[
#
"Image",
"diff_gray",
"diff_RGB",
"SIFT_Match",
"diff_contour",
"diff_cld",
#
"w_diff_gray",
"w_diff_RGB",
"w_diff_sift",
"w_diff_contour",
"w_diff_cld",
"sum diff",
"image",
]
)
for raw_result in raw_results:
writer.writerow(raw_result + [raw_result[0]])
result_category = result.replace("image.orig/", "")[0]
print("r:" + result + " rc:" + result_category)
return result_category