在python中利用opencv簡單做圖片比對的方法
更新時間:2019年01月24日 10:03:06 投稿:jingxian
今天小編就為大家分享一篇在python中利用opencv簡單做圖片比對的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
下面代碼中利用了兩種比對的方法,一 對圖片矩陣(m x m)求解特征值,通過比較特征值是否在一定的范圍內(nèi),判斷圖片是否相同。二 對圖片矩陣(m x m)中1求和,通過比較sum和來比較圖片。
# -*- coding: utf-8 -*-
import cv2 as cv
import numpy as np
import os
file_dir_a='C:\Users\wt\Desktop\data\image1\\'
file_dir_b='C:\Users\wt\Desktop\data\image\\'
savepath='.\'
all_file_name_a=os.listdir(file_dir_a)
all_file_name_b=os.listdir(file_dir_b)
image_all_a=[]
image_all_b=[]
for name in all_file_name_a:
image_one=[]
image = cv.imread(file_dir_a+name, cv.IMREAD_GRAYSCALE)
"""arg是計算輸入圖片矩陣的特征值,通過對特征值的比較來實現(xiàn)圖片的比對
"""
# arg=np.linalg.eigvals(image)
"""arg是計算輸入二值圖片矩陣中1的個數(shù),通過1的總數(shù)來實現(xiàn)圖片的比對
"""
arg=sum(image)
image_one.append(name)
image_one.append(arg)
image_all_a.append(image_one)#將一個圖片的信息寫入
print '讀入a'
# np.save('img_a.npy',image_all_a)
for name in all_file_name_b:
image_one=[]
image = cv.imread(file_dir_b+name, cv.IMREAD_GRAYSCALE)
"""同上
"""
# arg=np.linalg.eigvals(image)
arg=sum(image)
image_one.append(name)
image_one.append(arg)
image_all_b.append(image_one)#將一個圖片的信息寫入
print '讀入b'
# np.save('img_b.npy',image_all_b)
print '開始比較'
result_all=[]
for a in image_all_a: #比較小的
result = []
for b in image_all_b:
# print sum(a[1]-b[1])
if abs(sum(a[1]-b[1]))<0.00001:
result.append(a[0])
result.append(b[0])
result_all.append(result)
print '比較結(jié)束'
print result_all
np.save('match_result1.npy',result_all)
以上這篇在python中利用opencv簡單做圖片比對的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python opencv 讀取本地視頻文件 修改ffmpeg的方法
- 解決python測試opencv時imread導致的錯誤問題
- python opencv判斷圖像是否為空的實例
- python用opencv批量截取圖像指定區(qū)域的方法
- 使用Python向C語言的鏈接庫傳遞數(shù)組、結(jié)構(gòu)體、指針類型的數(shù)據(jù)
- Python使用ctypes調(diào)用C/C++的方法
- Python判斷變量名是否合法的方法示例
- Python使用while循環(huán)花式打印乘法表
- Python實現(xiàn)程序判斷季節(jié)的代碼示例
- 理想高通濾波實現(xiàn)Python opencv示例
相關(guān)文章
python GUI庫圖形界面開發(fā)之PyQt5滑塊條控件QSlider詳細使用方法與實例
這篇文章主要介紹了python GUI庫圖形界面開發(fā)之PyQt5滑塊條控件QSlider詳細使用方法與實例,需要的朋友可以參考下2020-02-02
python作圖基礎(chǔ)之plt.contour實例詳解
contour和contourf都是畫三維等高線圖的,下面這篇文章主要給大家介紹了關(guān)于python作圖基礎(chǔ)操作之plt.contour的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-06-06
Python操作CouchDB數(shù)據(jù)庫簡單示例
這篇文章主要介紹了Python操作CouchDB數(shù)據(jù)庫簡單示例,本文講解了連接服務(wù)器、創(chuàng)建數(shù)據(jù)庫、創(chuàng)建文檔并插入到數(shù)據(jù)庫等操作實例,需要的朋友可以參考下2015-03-03

