圖像識別概念
圖像識別是識別圖像中物體的類別(屬於哪一個類)的任務。圖像識別通常被稱為Image Classification、Categorization、Clustering。
卷積神經網絡(CNN)出現之前,一般用HOG、SIFT、SURF等方法先從圖像中提取特征,然后通過特征確定物體的類別。
利用圖像直方圖實現簡單的圖像識別任務
算法流程:
- 將訓練集中的圖像進行減色處理(圖像色彩量化:圖像色彩量化詳解)。RGB每個分量都只取4個值。
- 創建訓練集減色圖像的直方圖。RGB圖像的直方圖中,B=[1,4],G=[5,8]、R=[9,12],此時bin=12,但是我還需要保留每張訓練圖所屬的類別,所以,bin=13。數據這樣存儲:database = np.zeros( (訓練數據數,13),dtype=np.int )。所有訓練數據的柱狀圖如下:
database具有如下的形狀和內容(每一行最后一列是圖像所屬的類別):
- 將測試集圖像進行色彩量化,計算測試集圖像的直方圖與訓練集中每個直方圖的差,將差稱作特征向量。
- 直方圖差異總和最小的訓練集中圖像的類別就是我們預測的待測圖像的類別。換句話說,待測圖像的類別與近色圖像一致。
實驗代碼:
import cv2
import numpy as np
import matplotlib.pyplot as plt
from glob import glob
# 色彩量化
def dic_color(img):
img //= 63
img = img * 64 + 32
return img
# 創建 Database(db)
def get_DB():
# get training image path
train = glob("../dataset/train/*")
train.sort()
# prepare database
db = np.zeros((len(train), 13), dtype=np.int32)
# prepare path database
pdb = []
# each image
for i, path in enumerate(train):
# read image
img = dic_color(cv2.imread(path))
#get histogram
for j in range(4):
db[i, j] = len(np.where(img[..., 0] == (64 * j + 32))[0])
db[i, j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0])
db[i, j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0])
# get class
if 'akahara' in path:
cls = 0
elif 'madara' in path:
cls = 1
# store class label
db[i, -1] = cls
# store image path
pdb.append(path)
return db, pdb
# 判斷測試集中圖像類別
def test_DB(db, pdb):
# get test image path
test = glob("../dataset/test/*")
test.sort()
accurate_N = 0.
# each image
for path in test:
# read image
img = dic_color(cv2.imread(path))
# get histogram
hist = np.zeros(12, dtype=np.int32)
for j in range(4):
hist[j] = len(np.where(img[..., 0] == (64 * j + 32))[0])
hist[j+4] = len(np.where(img[..., 1] == (64 * j + 32))[0])
hist[j+8] = len(np.where(img[..., 2] == (64 * j + 32))[0])
# get histogram difference
difs = np.abs(db[:, :12] - hist)
# axis=1表示以行為單位,求每一行的和
difs = np.sum(difs, axis=1)
# get argmin of difference
pred_i = np.argmin(difs)
# get prediction label
pred = db[pred_i, -1]
if pred == 0:
pred_label = "akahara"
elif pred == 1:
pred_label = "madara"
gt = "akahara" if "akahara" in path else "madara"
if gt == pred_label:
accurate_N += 1
print(path, "is similar >>", pdb[pred_i], " Pred >>", pred_label)
accuracy = accurate_N / len(test)
print("Accuracy >>", accuracy, "({}/{})".format(int(accurate_N), len(test)))
if __name__ == '__main__':
db, pdb = get_DB()
test_DB(db, pdb)