新增图片相似度比较接口

This commit is contained in:
yanchunhuo 2020-02-26 11:30:20 +08:00
parent 14021e1088
commit 3f921bdd3d
3 changed files with 40 additions and 0 deletions

3
common/image/__init__.py Normal file
View File

@ -0,0 +1,3 @@
# 作者 yanchunhuo
# 创建时间 2020/2/26 10:00
# github https://github.com/yanchunhuo

View File

@ -0,0 +1,35 @@
# 作者 yanchunhuo
# 创建时间 2020/2/26 10:00
# github https://github.com/yanchunhuo
from skimage.metrics import structural_similarity
import cv2
class ImageCompare:
@classmethod
def compareTwoImage(cls,image1_path,image2_path,zoom_type='in'):
"""
比较两张图片相识度,返回值范围为0~1,提供比较的两张图片尺寸应该一致,如果不一致会进行缩放
:param image1_path:
:param image2_path:
:param zoom_type: in:放大,out:缩小,当图片尺寸一致时,该参数被忽略
:return:
"""
image1=cv2.imread(image1_path)
image2=cv2.imread(image2_path)
height1,width1,channel1=image1.shape
height2,width2,channel2=image2.shape
if not height1==height2 and not width1==width2:
height=0
width=0
if zoom_type.lower()=='in':
height=max(height1,height2)
width=max(width1,width2)
elif zoom_type.lower()=='out':
height=min(height1,height2)
width=min(width1,width2)
image1 = cv2.resize(image1, (height, width))
image2 = cv2.resize(image2, (height, width))
image1_gray=cv2.cvtColor(image1,cv2.COLOR_BGR2GRAY)
image2_gray=cv2.cvtColor(image2,cv2.COLOR_BGR2GRAY)
score,diff=structural_similarity(image1_gray,image2_gray,full=True)
return score

View File

@ -10,6 +10,8 @@ cx_Oracle==6.3.1
JPype1==0.6.3
paramiko==2.4.0
Pillow==5.3.0
scikit-image==0.16.2
opencv-python==4.2.0.32
PyMySQL==0.8.0
redis==2.10.6
ujson==1.35