強大的openCV能作什麼我就不囉嗦,你能想到的一切圖像+視頻處理.html
這裏,咱們說說openCV的圖像類似度對比, 嗯,說好聽一點那叫圖像識別,但嚴格講, 圖像識別是在一個圖片中進行類聚處理,好比圖片人臉識別,眼部識別,但相識度對比是指兩個或兩個以上的圖片進行對比類似度.算法
先來幾張圖片app
(a.png) (a_cp.png) (t1.png) (t2.png)機器學習
其中,a_cp.png 是複製a.png,也就是說是同一個圖片, t1.png 與t2.png 看起來相同,但都是經過PIL裁剪的圖片,能夠認爲類似但不相同. 學習
operator.eq(a,b) 判斷a,b 對象是否相同ui
import operator from PIL import Image a=Image.open("a.png") a_cp=Image.open("a_cp.png") t1=Image.open("t1.png") t2=Image.open("t2.png") c=operator.eq(a,a_cp) e=operator.eq(t1,t2) print(c) print(e)
打印結果 c爲True, e爲Falsespa
import numpy as np from PIL import Image a = Image.open("a.png") a_cp = Image.open("a_cp.png") t1 = Image.open("t1.png") t2 = Image.open("t2.png") difference = np.subtract(a, a_cp) # 判斷imgv 與v 的差值,存在差值,表示不相同 c = not np.any(difference) # np.any 知足一個1即爲真, (圖片相同差值爲0,np.any爲false, not fasle 即爲真認爲存在相同的圖片) difference = np.subtract(t1, t2) e = not np.any(difference) print(c) print(e)
打印結果 c爲True, e爲False.net
import hashlib a = open("a.png","rb") a_cp = open("a_cp.png",'rb') t1 = open("t1.png",'rb') t2 = open("t2.png",'rb') cmd5=hashlib.md5(a.read()).hexdigest() ccmd5=hashlib.md5(a_cp.read()).hexdigest() emd5=hashlib.md5(t1.read()).hexdigest() eecmd5=hashlib.md5(t2.read()).hexdigest() print(cmd5) if cmd5==ccmd5: print(True) else: print(False) print(emd5) if emd5==eecmd5: print(True) else: print(False)
打印文件md5結果:code
928f9df2d83fa5656bbd0f228c8f5f46
True
bff71ccd5d2c85fb0730c2ada678feea
False
由 operator.eq 與 numpy.subtract 和 hashlib.md5 方法發現,這些方法得出的結論,要不相同,要不不相同,世界萬物皆如此.視頻
說的好! 你給個人是boolean值,我不要,不要,不......
咱們想要的就是獲得兩個圖片的類似值,某些場景,咱們須要這樣的值, 好比探頭監控中的人與真人照片對比,因受到距離, 分辨率,移動速度等影響,相同的人有可能沒法準確辨認,在好比,連連看中的小方塊,經過PIL裁剪後,相同的圖像圖片因灰度,尺寸大小不一樣咱們會認爲相同的圖片以上三個方法就返回False. 所以openCV更適合這種百分比的類似度計算.
以前用過sklearn 的 Linear Regression 作過線性迴歸的數據預處理計算機率,因數據量小,未作到樣本訓練,突發奇想,若是openCV能結合sklearn的機器學習,給一堆圖片,通過fit樣本訓練獲取圖片的各類特徵,隨便給一張圖片,而後便能知道圖片來自那個地方,拍攝時間,都有哪些人物...
回來,回來... 咱們繼續說openCV相識度問題.
通常經過三種哈希算法與灰度直方圖算法進行判斷
#均值哈希算法 def aHash(img): #縮放爲8*8 img=cv2.resize(img,(8,8)) #轉換爲灰度圖 gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) #s爲像素和初值爲0,hash_str爲hash值初值爲'' s=0 hash_str='' #遍歷累加求像素和 for i in range(8): for j in range(8): s=s+gray[i,j] #求平均灰度 avg=s/64 #灰度大於平均值爲1相反爲0生成圖片的hash值 for i in range(8): for j in range(8): if gray[i,j]>avg: hash_str=hash_str+'1' else: hash_str=hash_str+'0' return hash_str
def dHash(img): #縮放8*8 img=cv2.resize(img,(9,8)) #轉換灰度圖 gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) hash_str='' #每行前一個像素大於後一個像素爲1,相反爲0,生成哈希 for i in range(8): for j in range(8): if gray[i,j]>gray[i,j+1]: hash_str=hash_str+'1' else: hash_str=hash_str+'0' return hash_str
def pHash(img): #縮放32*32 img = cv2.resize(img, (32, 32)) # , interpolation=cv2.INTER_CUBIC # 轉換爲灰度圖 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 將灰度圖轉爲浮點型,再進行dct變換 dct = cv2.dct(np.float32(gray)) #opencv實現的掩碼操做 dct_roi = dct[0:8, 0:8] hash = [] avreage = np.mean(dct_roi) for i in range(dct_roi.shape[0]): for j in range(dct_roi.shape[1]): if dct_roi[i, j] > avreage: hash.append(1) else: hash.append(0) return hash
# 計算單通道的直方圖的類似值 def calculate(image1, image2): hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0]) hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0]) # 計算直方圖的重合度 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) return degree
RGB每一個通道的直方圖計算類似度
def classify_hist_with_split(image1, image2, size=(256, 256)): # 將圖像resize後,分離爲RGB三個通道,再計算每一個通道的類似值 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) sub_data = sub_data / 3 return sub_data
啥?
我爲何知道這三個哈希算法和通道直方圖計算方法,嗯, 我也是從網上查的.
上素材
(x1y2.png) (x2y4.png) (x2y6.png) (t1.png) (t2.png) (t3.png)
完整代碼:
import cv2 import numpy as np # 均值哈希算法 def aHash(img): # 縮放爲8*8 img = cv2.resize(img, (8, 8)) # 轉換爲灰度圖 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # s爲像素和初值爲0,hash_str爲hash值初值爲'' s = 0 hash_str = '' # 遍歷累加求像素和 for i in range(8): for j in range(8): s = s + gray[i, j] # 求平均灰度 avg = s / 64 # 灰度大於平均值爲1相反爲0生成圖片的hash值 for i in range(8): for j in range(8): if gray[i, j] > avg: hash_str = hash_str + '1' else: hash_str = hash_str + '0' return hash_str # 差值感知算法 def dHash(img): # 縮放8*8 img = cv2.resize(img, (9, 8)) # 轉換灰度圖 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) hash_str = '' # 每行前一個像素大於後一個像素爲1,相反爲0,生成哈希 for i in range(8): for j in range(8): if gray[i, j] > gray[i, j + 1]: hash_str = hash_str + '1' else: hash_str = hash_str + '0' return hash_str # 感知哈希算法(pHash) def pHash(img): # 縮放32*32 img = cv2.resize(img, (32, 32)) # , interpolation=cv2.INTER_CUBIC # 轉換爲灰度圖 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 將灰度圖轉爲浮點型,再進行dct變換 dct = cv2.dct(np.float32(gray)) # opencv實現的掩碼操做 dct_roi = dct[0:8, 0:8] hash = [] avreage = np.mean(dct_roi) for i in range(dct_roi.shape[0]): for j in range(dct_roi.shape[1]): if dct_roi[i, j] > avreage: hash.append(1) else: hash.append(0) return hash # 經過獲得RGB每一個通道的直方圖來計算類似度 def classify_hist_with_split(image1, image2, size=(256, 256)): # 將圖像resize後,分離爲RGB三個通道,再計算每一個通道的類似值 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) sub_data = sub_data / 3 return sub_data # 計算單通道的直方圖的類似值 def calculate(image1, image2): hist1 = cv2.calcHist([image1], [0], None, [256], [0.0, 255.0]) hist2 = cv2.calcHist([image2], [0], None, [256], [0.0, 255.0]) # 計算直方圖的重合度 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) return degree # Hash值對比 def cmpHash(hash1, hash2): n = 0 # hash長度不一樣則返回-1表明傳參出錯 if len(hash1)!=len(hash2): return -1 # 遍歷判斷 for i in range(len(hash1)): # 不相等則n計數+1,n最終爲類似度 if hash1[i] != hash2[i]: n = n + 1 return n img1 = cv2.imread('openpic/x1y2.png') # 11--- 16 ----13 ---- 0.43 img2 = cv2.imread('openpic/x2y4.png') img1 = cv2.imread('openpic/x3y5.png') # 10----11 ----8------0.25 img2 = cv2.imread('openpic/x9y1.png') img1 = cv2.imread('openpic/x1y2.png') # 6------5 ----2--------0.84 img2 = cv2.imread('openpic/x2y6.png') img1 = cv2.imread('openpic/t1.png') # 14------19---10--------0.70 img2 = cv2.imread('openpic/t2.png') img1 = cv2.imread('openpic/t1.png') # 39------33---18--------0.58 img2 = cv2.imread('openpic/t3.png') hash1 = aHash(img1) hash2 = aHash(img2) n = cmpHash(hash1, hash2) print('均值哈希算法類似度:', n) hash1 = dHash(img1) hash2 = dHash(img2) n = cmpHash(hash1, hash2) print('差值哈希算法類似度:', n) hash1 = pHash(img1) hash2 = pHash(img2) n = cmpHash(hash1, hash2) print('感知哈希算法類似度:', n) n = classify_hist_with_split(img1, img2) print('三直方圖算法類似度:', n)
https://blog.csdn.net/haofan_/article/details/77097473?locationNum=7&fps=1
http://www.javashuo.com/article/p-hrypcwqi-na.html