图片相似度比较_python

简介: 图片相似度比较_python
# -*- coding : UTF-8 -*-
 
import cv2 as cv
from PIL import Image
import os
import numpy as np
import copy
import matplotlib.pyplot as plt
 
def openImg_opencv(filename = 'new.jpg'):
    if os.path.exists(filename):
        image = cv.imread(filename) #opencv
        return image
    else:
        print("image not found")
 
def openImg_PIL(filename = 'new.jpg'):
    if os.path.exists(filename):
        temp = Image.open(filename) #PIL
        image = np.array(temp)
        return image
    else:
        print("image not found")
 
def averageGray(image):
    image = image.astype(int)
    for y in range(image.shape[1]): # y is width
        for x in range(image.shape[0]): # x is height
            gray = (image[x,y,0] + image[x,y,1] + image[x,y,2]) // 3
            image[x,y] = gray
    return image.astype(np.uint8)
 
def averageGrayWithWeighted(image):
    image = image.astype(int)
    for y in range(image.shape[1]): # y is width
        for x in range(image.shape[0]): # x is height
            gray = image[x,y,0] * 0.3 + image[x,y,1] * 0.59 + image[x,y,2] * 0.11
            image[x,y] = int(gray)
    return image.astype(np.uint8)
 
def maxGray(image):
    for y in range(image.shape[1]): # y is width
        for x in range(image.shape[0]):
            gray = max(image[x,y]) # x is height
            image[x,y] = gray
    return image
 
def resize_opencv(image,weight = 8,height = 8):
    smallImage = cv.resize(image,(weight,height),interpolation=cv.INTER_LANCZOS4)
    return smallImage
 
def calculateDifference(image,weight = 8,height = 8):
    differenceBuffer = []
    for x in range(weight):
        for y in range(height - 1):
            differenceBuffer.append(image[x,y,0] > image[x,y + 1,0])
    return differenceBuffer
 
def makeHash(differ):
    hashOrdString = "0b"
    for value in differ:
        hashOrdString += str(int(value))
    hashString = hex(int(hashOrdString,2))
    return hashString
 
def stringToHash(filename = 'new.jpg'):
    image1 = openImg_opencv(filename)
    grayImage1 = averageGrayWithWeighted(copy.deepcopy(image1))
    plt.imshow(grayImage1)
    plt.show()
    smallImage1 = resize_opencv(copy.deepcopy(grayImage1))
    plt.imshow(smallImage1)
    plt.show()
    differ = calculateDifference(copy.deepcopy(smallImage1))
    return makeHash(differ)
 
def calculateHammingDistance(differ1,differ2):
    difference = (int(differ1, 16)) ^ (int(differ2, 16))
    return bin(difference).count("1")
 
def main():
    pic1 = stringToHash('/Desktop/img/deng1.jpg')
    pic2 = stringToHash('/Desktop/img/deng2.jpg')
    print("this two picture is " + str((8 * 8 - calculateHammingDistance(pic1,pic2)) / (8 * 8) * 100) + "% similarity")
if __name__ == "__main__":
    main()

提供俩张图片给大家使用

目录
相关文章
|
13天前
|
存储 Python
python实现图片与视频转换:将视频保存为图片,将批量图片保存为视频
python实现图片与视频转换:将视频保存为图片,将批量图片保存为视频
|
8天前
|
计算机视觉 Python
【干货】Python玩转各种多媒体,视频、音频到图片
【干货】Python玩转各种多媒体,视频、音频到图片
12 1
|
1月前
|
Python 计算机视觉
2024年Python最新利用python进行数学公式识别_python 识别图片中的数学公式,2024年最新字节跳动技术岗位面试
2024年Python最新利用python进行数学公式识别_python 识别图片中的数学公式,2024年最新字节跳动技术岗位面试
2024年Python最新利用python进行数学公式识别_python 识别图片中的数学公式,2024年最新字节跳动技术岗位面试
|
22天前
|
数据采集 JSON API
自动化Reddit图片收集:Python爬虫技巧
自动化Reddit图片收集:Python爬虫技巧
|
7天前
|
数据采集 机器学习/深度学习 搜索推荐
Python第一章(图片与API接口)
Python第一章(图片与API接口)
|
12天前
|
机器学习/深度学习 算法 数据可视化
【深度学习实战】基于深度学习的图片风格快速迁移软件(Python源码+UI界面)
【深度学习实战】基于深度学习的图片风格快速迁移软件(Python源码+UI界面)
|
1月前
|
开发工具 Python
【分享Python代码】图片转化为素描画
【分享Python代码】图片转化为素描画
34 2
|
1月前
|
XML 数据格式 Python
python挑出训练集里图片对应的xml文件,方便统计标签框的类别与数目_python 统计voc2007xml中某一类别框个数(1)
python挑出训练集里图片对应的xml文件,方便统计标签框的类别与数目_python 统计voc2007xml中某一类别框个数(1)
|
1月前
|
数据安全/隐私保护 Python
python 图片打水印 透明图片合并
python 图片打水印 透明图片合并
21 1
|
1月前
|
存储 索引 Python
python图片九宫格图片处理
本篇文章介绍了一个Python项目的实现,项目能够处理图片并将其组合成九宫格或四宫格,同时还具备音乐播放功能,对于初学者来说是一个可以进行实战学习的初级项目。