我是靠谱客的博主 沉静月饼,最近开发中收集的这篇文章主要介绍python open cv 图片对比_图片相似度比较_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()

提供俩张图片给大家使用

最后

以上就是沉静月饼为你收集整理的python open cv 图片对比_图片相似度比较_python的全部内容,希望文章能够帮你解决python open cv 图片对比_图片相似度比较_python所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(36)

评论列表共有 0 条评论

立即
投稿
返回
顶部