栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python opencv 相似图像匹配_opencv图片对比度计算?

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python opencv 相似图像匹配_opencv图片对比度计算?

# coding: utf-8
# @Author: 一棵柚子树
# @time: 2022/3/26 0:28
import time
import numpy
import cv2
from PIL import Image


def calculate(image1, image2):
    hist1 = cv2.calcHist([image1], [0], None, [256], [0, 256])
    hist2 = cv2.calcHist([image2], [0], None, [256], [0, 256])
    # 计算直方图的重合度
    degree = 0
    for i in range(len(hist1)):
        if hist1[i] != hist2[i]:
            degree = degree + (1 - abs(hist1[i] - hist2[i]) / max(hist1[i], hist2[i]))
        else:
            degree = degree + 1
    degree = degree / len(hist1)
    print(degree)
    return degree


def picture_similarity(image1, image2, size=(256, 256)):
    image1 = Image.open(image1)
    image2 = Image.open(image2)
    # 将图像resize后,分离为RGB三个通道,再计算每个通道的相似值
    image1 = cv2.cvtColor(numpy.asarray(image1), cv2.COLOR_RGB2BGR)
    image2 = cv2.cvtColor(numpy.asarray(image2), cv2.COLOR_RGB2BGR)
    image1 = cv2.resize(image1, size)
    image2 = cv2.resize(image2, size)
    sub_image1 = cv2.split(image1)
    sub_image2 = cv2.split(image2)
    sub_data = 0
    for im1, im2 in zip(sub_image1, sub_image2):
        sub_data += calculate(im1, im2)
    print(f"长度:{len(sub_image1)}")
    sub_data = sub_data / len(sub_image1)
    return int(sub_data*100)


if __name__ == '__main__':
    img1_path = "desktop.png"
    img2_path = "desktop2.png"
    result1 = picture_similarity(img1_path, img2_path)
    print(f"相似度为:{result1}")
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/783509.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号