Prerequisites: Python OpenCV
Suppose we have two data images and a test image. Let’s find out which data image is more similar to the test image using python and OpenCV library in Python.
Let’s first load the image and find out the histogram of images.
Importing library
import cv2
Importing image data
image = cv2.imread('test.jpg')
Converting to gray image
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
Finding Histogram
histogram = cv2.calcHist([gray_image], [0],
None, [256], [0, 256])
Example:
Images used:
data1.jpg

data2.jpg

test.jpg

import cv2 # test image image = cv2.imread('cat.jpg') gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) histogram = cv2.calcHist([gray_image], [0], None, [256], [0, 256]) # data1 image image = cv2.imread('cat.jpeg') gray_image1 = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) histogram1 = cv2.calcHist([gray_image1], [0], None, [256], [0, 256]) # data2 image image = cv2.imread('food.jpeg') gray_image2 = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) histogram2 = cv2.calcHist([gray_image2], [0], None, [256], [0, 256]) c1, c2 = 0, 0 # Euclidean Distace between data1 and test i = 0while i<len(histogram) and i<len(histogram1): c1+=(histogram[i]-histogram1[i])**2 i+= 1c1 = c1**(1 / 2) # Euclidean Distace between data2 and test i = 0while i<len(histogram) and i<len(histogram2): c2+=(histogram[i]-histogram2[i])**2 i+= 1c2 = c2**(1 / 2) if(c1<c2): print("data1.jpg is more similar to test.jpg as compare to data2.jpg") else: print("data2.jpg is more similar to test.jpg as compare to data1.jpg") |
Output :
data1.jpg is more similar to test.jpg as compare to data2.jpg
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | Measure similarity between two sentences using cosine similarity
- Arithmetic Operations on Images using OpenCV | Set-2 (Bitwise Operations on Binary Images)
- Python | Word Similarity using spaCy
- Movie recommender based on plot summary using TF-IDF Vectorization and Cosine similarity
- Python | Test list element similarity
- NLP | WuPalmer - WordNet Similarity
- NLP | Leacock Chordorow (LCH) and Path similarity for Synset
- Python | Similarity metrics of strings
- Python | Percentage similarity of lists
- Measuring the Document Similarity in Python
- Python - Alternate elements Similarity
- Statistical Functions in Python | Set 1 (Averages and Measure of Central Location)
- Statistical Functions in Python | Set 2 ( Measure of Spread)
- Degree Centrality (Centrality Measure)
- Katz Centrality (Centrality Measure)
- ML | V-Measure for Evaluating Clustering Performance
- Python - Measure time taken by program to execute
- Python | Create video using multiple images using OpenCV
- Erosion and Dilation of images using OpenCV in python
- Get emotions of images using Microsoft emotion API in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

