用例子讲解图像识别过程

图像识别概念

图像识别是识别图像中物体的类别(属于哪一个类)的任务。图像识别通常被称为Image Classification、Categorization、Clustering。
卷积神经网络(CNN)出现之前,一般用HOG、SIFT、SURF等方法先从图像中提取特征,然后通过特征确定物体的类别。

利用图像直方图实现简单的图像识别任务

算法流程:

  1. 将训练集中的图像进行减色处理(图像色彩量化:图像色彩量化详解)。RGB每个分量都只取4个值。
  2. 创建训练集减色图像的直方图。RGB图像的直方图中,B=[1,4],G=[5,8]、R=[9,12],此时bin=12,但是我还需要保留每张训练图所属的类别,所以,bin=13。数据这样存储:database = np.zeros( (训练数据数,13),dtype=np.int )。所有训练数据的柱状图如下:
    色彩量化后训练数据的柱状图
    database具有如下的形状和内容(每一行最后一列是图像所属的类别):
    database的形状和内容
  3. 将测试集图像进行色彩量化,计算测试集图像的直方图与训练集中每个直方图的差,将差称作特征向量。
  4. 直方图差异总和最小的训练集中图像的类别就是我们预测的待测图像的类别。换句话说,待测图像的类别与近色图像一致。

实验代码:

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)

实验输出(包含识别出的图像的类别和识别准确率):

图像识别结果及识别准确率

posted on 2020-03-26 21:24  我坚信阳光灿烂  阅读(1266)  评论(0编辑  收藏  举报

导航