How to improve the performance of image matching

I am working on a project about giving a test image, I have to retrieve similar images from a folder with 1000 images inside.

For example, my test image:

The images expected to be retrieved are some ancient Rome architectures (cannot post due to new user)

However, there are some images that are to confuse my program which are some mountains, which the colors are quite similar to my test image.

These are my functions :

import cv2 as cv
import numpy as np
from glob import glob

#Comparing images
def compareImgs(img1, img2):
# resize img2 to img1
img2 = cv.resize(img2, (img1.shape[1], img1.shape[0]))
diff = cv.absdiff(img1, img2)
return diff.sum()

#Comparing Histogram
def compareImgs_hist(img1, img2):
width, height = img1.shape[1], img1.shape[0]
img2 = cv.resize(img2, (width, height))
num_bins = 10
hist1 = [0] * num_bins
hist2 = [0] * num_bins
bin_width = 255.0 / num_bins + 1e-4
hist1 = cv.calcHist([img1], [0], None, [num_bins], [0, 255])
hist2 = cv.calcHist([img2], [0], None, [num_bins], [0, 255])
sum = 0
for i in range(num_bins):
sum += abs(hist1[i] - hist2[i])
return sum / float(width * height)

def retrieval():
src_input = cv.imread(“building.jpg”)
src_gray = cv.cvtColor(src_input, cv.COLOR_BGR2GRAY)
# read image database (1000 images)
database = sorted(glob(database_dir + “/*.jpg”))
for img in database:
# read image
img_rgb = cv.imread(img)
# convert to gray scale
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
# compare the two images
diff = compareImgs(src_gray, img_gray)
# compare the two images by histogram, uncomment the following line to use histogram
diff = compareImgs_hist(src_gray, img_gray)
# find the minimum difference
if diff <= min_diff:
# update the minimum difference
min_diff = diff
# update the most similar image
closest_img = img_rgb
result = img

print("the most similar image is %s, the pixel-by-pixel difference is %f " % (result, min_diff))

cv.imshow("Result", closest_img)

The result is always the mountain but not buildings, I know there are some differences like the test image is a skyscraper while the expected images are some ancient architecture. But is that a possible way that I can improve the performance?

This is my first time posting here so if I have something stated not clearly or wrong please let me know. Thanks a lot!

Expected image:

Image to confuse the program:

Similar histogram doesn’t tell much… I would find contours in the images. In the building there would be many rectangular ones, whereas in the mountains, very different ones.

1 Like

this is a huge field of research. you should not imagine that this will be trivial.

1 Like

So it is hard to use histogram in this condition?