I tried to make an image similarity function with Python + OpenCV

Overview

A memo when I made a function in Python to compare an image with multiple images with a grayscale histogram

environment

--opencv 2.4.8 (installed with conda)

Implementation

image_compare.py



import cv2

def matching(self):

    im = cv2.imread('/to/image/path/image.jpg', cv2.IMREAD_GRAYSCALE) #ImageFile to compare
    image_hist = cv2.calcHist([im], [0], None, [256], [0, 256])

    target = self.compare_target_hist(image_hist)

    result = []

    while True:
        try:
            result.append(target.next())

        except StopIteration:
            break

    result.sort(reverse=True)

    return result[0][1]

#ImageFile to be compared
def gen_target(self):
    yield "to/image/path/4.jpg "
    yield "to/image/path/3.jpg "
    yield "to/image/path/5.jpg "
    yield "to/image/path/2.jpg "
    yield "to/image/path/1.jpg "

def compare_target_hist(self, image_hist):

    target_files = self.gen_target()

    while True:
        try:
            target_file = target_files.next()
            im = cv2.imread(target_file, cv2.IMREAD_GRAYSCALE)
            target_hist = cv2.calcHist([im], [0], None, [256], [0, 256])
            yield (self.compare_hist(image_hist, target_hist), target_file)

        except StopIteration:
            break

#Histogram comparison calculation
def compare_hist(self, hist1, hist2):
    total = 0
    for i in range(len(hist1)):
        total += min(hist1[i], hist2[i])
    return float(total) / sum(hist1)


if __name__ == '__main__':
    matching()

Recommended Posts

I tried to make an image similarity function with Python + OpenCV
I tried "smoothing" the image with Python + OpenCV
How to crop an image with Python + OpenCV
I tried "binarizing" the image with Python + OpenCV
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried to implement an artificial perceptron with python
I tried to make an OCR application with PySimpleGUI
Find image similarity with Python + OpenCV
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I tried to make various "dummy data" with Python faker
I tried to make GUI tic-tac-toe with Python and Tkinter
I tried non-photorealistic rendering with Python + opencv
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
I tried to process the image in "sketch style" with OpenCV
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
I tried to process the image in "pencil style" with OpenCV
I tried to make a todo application using bottle with python
[4th] I tried to make a certain authenticator-like tool with python
[Python] Simple Japanese ⇒ I tried to make an English translation tool
[1st] I tried to make a certain authenticator-like tool with python
[Python] I tried to make an application that calculates salary according to working hours with tkinter
I tried to get an image by scraping
I tried to detect motion quickly with OpenCV
I want to make a game with Python
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried to detect an object with M2Det!
I tried to automate sushi making with python
I tried sending an email with SendGrid + Python
Post an article with an image to WordPress with Python
Python: I tried to make a flat / flat_map just right with a generator
I tried to make an open / close sensor (Twitter cooperation) with TWE-Lite-2525A
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
[Zaif] I tried to make it easy to trade virtual currencies with Python
I tried to learn the sin function with chainer
I want to convert an image to WebP with lollipop
I tried fp-growth with python
I tried scraping with Python
I tried to make a periodical process with CentOS7, Selenium, Python and Chrome
I tried to extract features with SIFT of OpenCV
Image editing with python OpenCV
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to touch the CSV file with Python
I tried to draw a route map with Python
I tried to solve the soma cube with python
Continuation ・ I tried to make Slackbot after studying Python3
I tried to get started with blender python script_Part 02
Make a function to describe Japanese fonts with OpenCV
I tried to become an Ann Man using OpenCV
I tried to extract a line art from an image with Deep Learning
I tried to automatically generate a password with Python3
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
I tried gRPC with Python
I tried scraping with python
I tried my best to make an optimization function, but it didn't work.
I tried to find an alternating series with tensorflow