加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
sm.py 2.67 KB
一键复制 编辑 原始数据 按行查看 历史
# -*- 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('image/a.jpg')
pic2 = stringToHash('image/b.jpg')
print("this two picture is " + str((8 * 8 - calculateHammingDistance(pic1, pic2)) / (8 * 8) * 100) + "% similarity")
if __name__ == "__main__":
main()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化