hog+svm图像检测流程 --python

本文介绍如何使用HOG特征提取方法处理行人检测任务,通过Python实现正负样本数据集的准备,利用OpenCV计算梯度和直方图,然后结合SVM进行模型训练。过程包括特征提取、特征保存、样本统一和模型训练,最后展示模型应用与可视化检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 准备正负样本数据集,保证尺寸一致。其中正样本数据集是包含被测试物体的图片,如检测行人,车牌等此类图像,相反负样本则不包含被检测物体的图像;

 

2. 利用python实现hog特征的提取:可以直接利用opencv中封装好的函数cv.HOGDescriptor()

import math
import os
from itertools import chain

import cv2
import joblib
import numpy as np
import skimage.novice
from PIL import Image
from matplotlib import pyplot as plt
from sklearn.svm import LinearSVC


class Hog_feature_extraction():
    def __init__(self, img, cell_size=8, bin_size=8):
        self.img = img
        self.img = np.sqrt(img / float(np.max(img)))
        self.cell_size = cell_size
        self.bin_size = bin_size
        self.angle_unit = 360 // bin_size
        assert type(self.bin_size) == int, "bin_size should be integer,"
        assert type(self.cell_size) == int, "cell_size should be integer,"
        assert type(self.angle_unit) == int, "bin_size should be divisible by 360"

    # 1-计算cell梯度;3-可视化直方图;
    def caculate_block(self, img):
        height, width = self.img.shape
        gradient_value_x = cv2.Sobel(img, cv2.CV_64F, 1, 0, ksize=5)  # 1代表在x方向求导
        gradient_value_y = cv2.Sobel(img, cv2.CV_64F, 0, 1, ksize=5)
        gradient_magnitude = cv2.addWeighted(gradient_value_x, 0.5, gradient_value_y, 0.5, 0)  # 计算该点像素点的梯度大小和方向
        gradient_angle = cv2.phase(gradient_value_x, gradient_value_y, angleInDegrees=True)
        gradient_magnitude = abs(gradient_magnitude)
        cell_gradient_vector = np.zeros((height // self.cell_size, width // self.cell_size, self.bin_size))
        for i in range(cell_gradient_vector.shape[0]):  # 遍历cell的高
            for j in range(cell_gradient_vector.shape[1]):  # 遍历cell的宽
                cell_magnitude = gradient_magnitude[i * self.cell_size: (i + 1) * self.cell_size, j * self.cell_size: (j + 1) * self.cell_size]
                cell_
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值