亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python圖片檢索之以圖搜圖

 更新時(shí)間:2021年05月31日 10:31:24   作者:代碼小白的成長(zhǎng)  
由于很多論文里面的測(cè)試圖片沒(méi)有標(biāo)號(hào),就不能確定它們是Testset數(shù)據(jù)集中哪幾張圖片.為了能解決這個(gè)問(wèn)題,需要完成以圖片去搜索整個(gè)數(shù)據(jù)集文件目錄的任務(wù).本文有非常詳細(xì)的圖文示例,需要的朋友可以參考下

一、待搜索圖

在這里插入圖片描述

二、測(cè)試集

在這里插入圖片描述

三、new_similarity_compare.py

# -*- encoding=utf-8 -*-

from image_similarity_function import *
import os
import shutil

# 融合相似度閾值
threshold1 = 0.70
# 最終相似度較高判斷閾值
threshold2 = 0.95


# 融合函數(shù)計(jì)算圖片相似度
def calc_image_similarity(img1_path, img2_path):
    """
    :param img1_path: filepath+filename
    :param img2_path: filepath+filename
    :return: 圖片最終相似度
    """

    similary_ORB = float(ORB_img_similarity(img1_path, img2_path))
    similary_phash = float(phash_img_similarity(img1_path, img2_path))
    similary_hist = float(calc_similar_by_path(img1_path, img2_path))
    # 如果三種算法的相似度最大的那個(gè)大于0.7,則相似度取最大,否則,取最小。
    max_three_similarity = max(similary_ORB, similary_phash, similary_hist)
    min_three_similarity = min(similary_ORB, similary_phash, similary_hist)
    if max_three_similarity > threshold1:
        result = max_three_similarity
    else:
        result = min_three_similarity

    return round(result, 3)


if __name__ == '__main__':

    # 搜索文件夾
    filepath = r'D:\Dataset\cityscapes\leftImg8bit\val\frankfurt'

    #待查找文件夾
    searchpath = r'C:\Users\Administrator\Desktop\cityscapes_paper'

    # 相似圖片存放路徑
    newfilepath = r'C:\Users\Administrator\Desktop\result'

    for parent, dirnames, filenames in os.walk(searchpath):
        for srcfilename in filenames:
            img1_path = searchpath +"\\"+ srcfilename
            for parent, dirnames, filenames in os.walk(filepath):
                for i, filename in enumerate(filenames):
                    print("{}/{}: {} , {} ".format(i+1, len(filenames), srcfilename,filename))
                    img2_path = filepath + "\\" + filename
                    # 比較
                    kk = calc_image_similarity(img1_path, img2_path)
                    try:
                        if kk >= threshold2:
                            # 將兩張照片同時(shí)拷貝到指定目錄
                            shutil.copy(img2_path, os.path.join(newfilepath, srcfilename[:-4] + "_" + filename))
                    except Exception as e:
                        # print(e)
                        pass

四、image_similarity_function.py

# -*- encoding=utf-8 -*-

# 導(dǎo)入包
import cv2
from functools import reduce
from PIL import Image


# 計(jì)算兩個(gè)圖片相似度函數(shù)ORB算法
def ORB_img_similarity(img1_path, img2_path):
    """
    :param img1_path: 圖片1路徑
    :param img2_path: 圖片2路徑
    :return: 圖片相似度
    """
    try:
        # 讀取圖片
        img1 = cv2.imread(img1_path, cv2.IMREAD_GRAYSCALE)
        img2 = cv2.imread(img2_path, cv2.IMREAD_GRAYSCALE)

        # 初始化ORB檢測(cè)器
        orb = cv2.ORB_create()
        kp1, des1 = orb.detectAndCompute(img1, None)
        kp2, des2 = orb.detectAndCompute(img2, None)

        # 提取并計(jì)算特征點(diǎn)
        bf = cv2.BFMatcher(cv2.NORM_HAMMING)
        # knn篩選結(jié)果
        matches = bf.knnMatch(des1, trainDescriptors=des2, k=2)

        # 查看最大匹配點(diǎn)數(shù)目
        good = [m for (m, n) in matches if m.distance < 0.75 * n.distance]
        similary = len(good) / len(matches)
        return similary

    except:
        return '0'


# 計(jì)算圖片的局部哈希值--pHash
def phash(img):
    """
    :param img: 圖片
    :return: 返回圖片的局部hash值
    """
    img = img.resize((8, 8), Image.ANTIALIAS).convert('L')
    avg = reduce(lambda x, y: x + y, img.getdata()) / 64.
    hash_value = reduce(lambda x, y: x | (y[1] << y[0]), enumerate(map(lambda i: 0 if i < avg else 1, img.getdata())),
                        0)
    return hash_value


# 計(jì)算兩個(gè)圖片相似度函數(shù)局部敏感哈希算法
def phash_img_similarity(img1_path, img2_path):
    """
    :param img1_path: 圖片1路徑
    :param img2_path: 圖片2路徑
    :return: 圖片相似度
    """
    # 讀取圖片
    img1 = Image.open(img1_path)
    img2 = Image.open(img2_path)

    # 計(jì)算漢明距離
    distance = bin(phash(img1) ^ phash(img2)).count('1')
    similary = 1 - distance / max(len(bin(phash(img1))), len(bin(phash(img1))))
    return similary


# 直方圖計(jì)算圖片相似度算法
def make_regalur_image(img, size=(256, 256)):
    """我們有必要把所有的圖片都統(tǒng)一到特別的規(guī)格,在這里我選擇是的256x256的分辨率。"""
    return img.resize(size).convert('RGB')


def hist_similar(lh, rh):
    assert len(lh) == len(rh)
    return sum(1 - (0 if l == r else float(abs(l - r)) / max(l, r)) for l, r in zip(lh, rh)) / len(lh)


def calc_similar(li, ri):
    return sum(hist_similar(l.histogram(), r.histogram()) for l, r in zip(split_image(li), split_image(ri))) / 16.0


def calc_similar_by_path(lf, rf):
    li, ri = make_regalur_image(Image.open(lf)), make_regalur_image(Image.open(rf))
    return calc_similar(li, ri)


def split_image(img, part_size=(64, 64)):
    w, h = img.size
    pw, ph = part_size
    assert w % pw == h % ph == 0
    return [img.crop((i, j, i + pw, j + ph)).copy() for i in range(0, w, pw) \
            for j in range(0, h, ph)]

五、結(jié)果

在這里插入圖片描述

到此這篇關(guān)于Python圖片檢索之以圖搜圖的文章就介紹到這了,更多相關(guān)Python以圖搜圖內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python異步爬蟲(chóng)之多線程

    python異步爬蟲(chóng)之多線程

    這篇文章主要介紹了python異步爬蟲(chóng)之多線程,多線程可以為相關(guān)阻塞的操作單獨(dú)開(kāi)啟線程或者進(jìn)程,阻塞操作可以異步執(zhí)行,但是無(wú)法無(wú)限制開(kāi)啟多線程或多進(jìn)程,下面我們一起學(xué)習(xí)詳細(xì)內(nèi)容,需要的朋友可以參考一下
    2022-02-02
  • Pycharm最新激活碼2019(推薦)

    Pycharm最新激活碼2019(推薦)

    這篇文章主要介紹了Pycharm最新激活碼2019,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-12-12
  • 淺談python數(shù)據(jù)類(lèi)型及其操作

    淺談python數(shù)據(jù)類(lèi)型及其操作

    今天帶大家了解python數(shù)據(jù)類(lèi)型的相關(guān)知識(shí),文中介紹的非常詳細(xì),對(duì)正在學(xué)習(xí)python的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • python+pygame實(shí)現(xiàn)簡(jiǎn)易五子棋小游戲的三種方式

    python+pygame實(shí)現(xiàn)簡(jiǎn)易五子棋小游戲的三種方式

    這篇文章主要介紹了使用python實(shí)現(xiàn)簡(jiǎn)易五子棋小游戲,文中提供了三種實(shí)現(xiàn)方式,解決思路和部分實(shí)現(xiàn)代碼,感興趣的朋友可以參考下
    2023-03-03
  • 基于Python實(shí)現(xiàn)炸彈人小游戲

    基于Python實(shí)現(xiàn)炸彈人小游戲

    這篇文章主要介紹了基于Python中的Pygame模塊實(shí)現(xiàn)的炸彈人小游戲,文中的示例代碼講解詳細(xì),對(duì)學(xué)習(xí)Python有一定的幫助,感興趣的小伙伴可以學(xué)習(xí)一下
    2021-12-12
  • 一文詳解測(cè)試Python讀寫(xiě)xml配置文件

    一文詳解測(cè)試Python讀寫(xiě)xml配置文件

    這篇文章主要介紹了一文詳解測(cè)試Python讀寫(xiě)xml配置文件,xml也是常用的配置文件格式之一,Python中的xml.etree.ElementTree模塊支持解析和創(chuàng)建xml數(shù)據(jù)
    2022-09-09
  • Python封裝shell命令實(shí)例分析

    Python封裝shell命令實(shí)例分析

    這篇文章主要介紹了Python封裝shell命令,實(shí)例分析了Python將各種常用shell命令封裝進(jìn)一個(gè)類(lèi)中以便調(diào)用的方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-05-05
  • Python語(yǔ)法垃圾回收機(jī)制原理解析

    Python語(yǔ)法垃圾回收機(jī)制原理解析

    這篇文章主要介紹了Python語(yǔ)法垃圾回收機(jī)制原理解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • 解決django中form表單設(shè)置action后無(wú)法回到原頁(yè)面的問(wèn)題

    解決django中form表單設(shè)置action后無(wú)法回到原頁(yè)面的問(wèn)題

    這篇文章主要介紹了解決django中form表單設(shè)置action后無(wú)法回到原頁(yè)面的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • wtfPython—Python中一組有趣微妙的代碼【收藏】

    wtfPython—Python中一組有趣微妙的代碼【收藏】

    Wtfpython講解了大量的Python編譯器的內(nèi)容。這篇文章主要介紹了wtfPython-Python中一些奇妙的代碼,感興趣的朋友跟隨腳本之家小編一起看看吧
    2018-08-08

最新評(píng)論