简体   繁体   中英

Compare Different Images Stored Inside a Dictionary

{1: [52896, 34525, 13422, 18945, 55787],
 3: [66831, 32906, 44984, 18537, 51682],
 6: [49239, 53087, 59521, 3221, 11184],
 7: [6628, 30861, 15325, 64671, 51520],
 0: [47524, 12454, 42290, 5301, 16277],
 4: [48736, 6874, 49780, 25624, 25399],
 2: [16923, 30581, 42236, 6380, 9681]}

This is a dictionary with images of classes 1,3,6,7,0,4 and 2. The list elements in every key represents the image index in an array called x_data, which is an array of images.

I am trying to find structural similarity between image within the same class as well as images from other classes.

((Like comparing Class 1, Index 52896 image with Class 1, Index 18945 and also comparing with Class 3, index 66831 and so on))

And I want to do this to every Image

For structural similarity, I have thought to use:

from skimage.metrics import structural_similarity as ssim

But how to do the python acrobatics for the statement in Bold. Please help.

You have to iterate through every class and within every class you will have to iterate through every id. Now since you want to compare this with every other image you will have to repeat this once again.

for c1 in img_classes:
    for i1 in img_classes[c1]:
        for c2 in img_classes:
            for i2 in img_classes[c2]:
                # Compare image i1 of class c1 with image i2 of class c2
                val = ssim(images[i1], images[i2], multichannel=True)
                print(f"Comparing image {i1:5d} of class {c1} with image {i2:5d} of class {c2} || SSIM :{val:.4f}")

Output

Comparing image 47524 of class 0 with image 47524 of class 0 || SSIM :1.0000
Comparing image 47524 of class 0 with image 12454 of class 0 || SSIM :0.0111
Comparing image 47524 of class 0 with image 42290 of class 0 || SSIM :0.0431
Comparing image 47524 of class 0 with image  5301 of class 0 || SSIM :0.0237
Comparing image 47524 of class 0 with image 16277 of class 0 || SSIM :0.0302
Comparing image 47524 of class 0 with image 52896 of class 1 || SSIM :0.0179
Comparing image 47524 of class 0 with image 34525 of class 1 || SSIM :0.0316
Comparing image 47524 of class 0 with image 13422 of class 1 || SSIM :0.0217
Comparing image 47524 of class 0 with image 18945 of class 1 || SSIM :0.0186
Comparing image 47524 of class 0 with image 55787 of class 1 || SSIM :0.0180
Comparing image 47524 of class 0 with image 16923 of class 2 || SSIM :0.0123
Comparing image 47524 of class 0 with image 30581 of class 2 || SSIM :0.0165
Comparing image 47524 of class 0 with image 42236 of class 2 || SSIM :0.0122
Comparing image 47524 of class 0 with image  6380 of class 2 || SSIM :0.0400
Comparing image 47524 of class 0 with image  9681 of class 2 || SSIM :0.0173
Comparing image 47524 of class 0 with image 66831 of class 3 || SSIM :0.0180
Comparing image 47524 of class 0 with image 32906 of class 3 || SSIM :0.0178
Comparing image 47524 of class 0 with image 44984 of class 3 || SSIM :0.0121
Comparing image 47524 of class 0 with image 18537 of class 3 || SSIM :0.0112
Comparing image 47524 of class 0 with image 51682 of class 3 || SSIM :0.0208

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM