# Measuring the similarity percentage between two images

**URL:** <https://forum.opencv.org/t/measuring-the-similarity-percentage-between-two-images/17724>\
**Category:** Python\
**Created:** [May 23, 2024, 8:09am UTC](https://forum.opencv.org/t/measuring-the-similarity-percentage-between-two-images/17724 "2024-05-23T08:09:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nsnp](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@nsnp](https://forum.opencv.org/u/nsnp)\
**Post date:** [May 23, 2024, 8:09am UTC](https://forum.opencv.org/t/measuring-the-similarity-percentage-between-two-images/17724/1 "2024-05-23T08:09:18Z")

</div>

Hi, what is the recommended approach to measure the percentage of similarity between images?  
The use-case is extracting images from drone video and I want to select images above X percent similarity

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [May 23, 2024, 8:30am UTC](https://forum.opencv.org/t/measuring-the-similarity-percentage-between-two-images/17724/2 "2024-05-23T08:30:02Z")

</div>

define ‘similarity’, please …

and again, what is it for, later ?  
are you trying to build a map ? track something ?

---

<div class="post-metadata">

**Author:** ![nsnp](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@nsnp](https://forum.opencv.org/u/nsnp)\
**Post date:** [May 23, 2024, 8:55am UTC](https://forum.opencv.org/t/measuring-the-similarity-percentage-between-two-images/17724/3 "2024-05-23T08:55:11Z")

</div>

For example, suppose the drone stands still for 5 seconds, then all these frames will be classified as 100% (or close) similarity (0% of change), but if the drone continues to move, then a difference will be created, meaning a frame that part of it overlaps with the previous one but part is new, and I would like to measure the percentage of this change.  
Hope I managed to explain well, thanks.

---

<div class="post-metadata">

**Author:** ![3b017033](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/3b017033/32/10117_2.png) [@3b017033](https://forum.opencv.org/u/3b017033)\
**Post date:** [May 23, 2024, 9:11am UTC](https://forum.opencv.org/t/measuring-the-similarity-percentage-between-two-images/17724/4 "2024-05-23T09:11:17Z")

</div>

This might give you some direction: [Structural similarity index measure - Wikipedia](https://en.wikipedia.org/wiki/Structural_similarity_index_measure)

---

<div class="post-metadata">

**Author:** ![nsnp](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@nsnp](https://forum.opencv.org/u/nsnp)\
**Post date:** [May 23, 2024, 10:34am UTC](https://forum.opencv.org/t/measuring-the-similarity-percentage-between-two-images/17724/5 "2024-05-23T10:34:26Z")

</div>

I’ll check it out, thanks!

---

<div class="post-metadata">

**Author:** ![nsnp](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@nsnp](https://forum.opencv.org/u/nsnp)\
**Post date:** [May 23, 2024, 10:57am UTC](https://forum.opencv.org/t/measuring-the-similarity-percentage-between-two-images/17724/6 "2024-05-23T10:57:55Z")

</div>

Hi so i try and used in this code -  
from skimage.metrics import structural\_similarity  
import cv2

first = cv2.imread(‘frame\_16.png’)  
second = cv2.imread(‘frame\_17.png’)

# Convert images to grayscale

first\_gray = cv2.cvtColor(first, cv2.COLOR\_BGR2GRAY)  
second\_gray = cv2.cvtColor(second, cv2.COLOR\_BGR2GRAY)

# Compute SSIM between two images

score, diff = structural\_similarity(first\_gray, second\_gray, full=True)  
print(f"Similarity Score: {score \* 100 :.3f}%")

on the attached photo and get - Similarity Score: 8.233%.  
But I would expect to score at least 60% if not more in this case.

 ![Screenshot 2024-05-23 135625](https://us1.discourse-cdn.com/flex020/uploads/opencv/original/2X/6/6203a9c5325cf10140d75af029cca2c158cd0335.jpeg)

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 23, 2024, 11:22am UTC](https://forum.opencv.org/t/measuring-the-similarity-percentage-between-two-images/17724/7 "2024-05-23T11:22:15Z")

</div>

that’s because SSIM is not applicable here.

---

<div class="post-metadata">

**Author:** ![nsnp](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@nsnp](https://forum.opencv.org/u/nsnp)\
**Post date:** [May 23, 2024, 11:35am UTC](https://forum.opencv.org/t/measuring-the-similarity-percentage-between-two-images/17724/8 "2024-05-23T11:35:40Z")

</div>

> [@crackwitz](#):
>
> applicable

Can you please recommend another recommended way?

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [May 23, 2024, 12:10pm UTC](https://forum.opencv.org/t/measuring-the-similarity-percentage-between-two-images/17724/9 "2024-05-23T12:10:55Z")

</div>

if it’s successive frames of a camera view, then the view changes gradually.

you could want to find an alignment (affine or perspective transform) and then, based on how the quads encompassing the frames overlap, evaluate intersection over union, or other measures of area.

for the alignment, you could use optical flow (dense or sparse), or feature matching. I’d recommend optical flow. low resolution frames should be good enough for your estimation. an optical flow field is quite “rich” for such a simple alignment but it’s at least giving you enough vectors and fairly robust ones at that. sparse optical flow sounds like less work. it may be faster, but need not be.

---

<div class="post-metadata">

**Author:** ![nsnp](https://avatars.discourse-cdn.com/v4/letter/n/b19c9b/32.png) [@nsnp](https://forum.opencv.org/u/nsnp)\
**Post date:** [May 23, 2024, 5:43pm UTC](https://forum.opencv.org/t/measuring-the-similarity-percentage-between-two-images/17724/10 "2024-05-23T17:43:43Z")

</div>

I will check this solution. Thanks you.
