Java OpenCV 图像处理26.0 HOG 特征提取

Java OpenCV 图像处理26.0 HOG特征提取

  • 1 HOG 简述
  • 2 HOG 特征提取
    • 2.1 检测窗口
    • 2.2 归一化图像
    • 2.3 计算梯度
    • 2.4 统计直方图
    • 2.5 梯度直方图归一化
    • 2.6 得到HOG特征向量
  • 3 Java HOG 特征提取测试
  • 4 C# HOG 特征提取测试

1 HOG 简述

HOG是Histogram of Oriented Gradient的缩写,是一种在计算机视觉和图像处理中用来进行目标检测的特征描述子。可结合OpenCV的SVM分类器等用于图像特征提取和识别。
Java OpenCV只要是通过 Java Native Interface 调用C++库完成。

/**
 *
 *     @param _winSize sets winSize with given value.
 *     @param _blockSize sets blockSize with given value.
 *     @param _blockStride sets blockStride with given value.
 *     @param _cellSize sets cellSize with given value.
 *     @param _nbins sets nbins with given value.
 *     @param _derivAperture sets derivAperture with given value.
 *     @param _winSigma sets winSigma with given value.
 *     @param _histogramNormType sets histogramNormType with given value.
 *     @param _L2HysThreshold sets L2HysThreshold with given value.
 *     @param _gammaCorrection sets gammaCorrection with given value.
 *     @param _nlevels sets nlevels with given value.
 *     @param _signedGradient sets signedGradient with given value.
 */
public HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride, Size _cellSize, int _nbins, int _derivAperture, double _winSigma, int _histogramNormType, double _L2HysThreshold, boolean _gammaCorrection, int _nlevels, boolean _signedGradient) {
    nativeObj = HOGDescriptor_1(_winSize.width, _winSize.height, _blockSize.width, _blockSize.height, _blockStride.width, _blockStride.height, _cellSize.width, _cellSize.height, _nbins, _derivAperture, _winSigma, _histogramNormType, _L2HysThreshold, _gammaCorrection, _nlevels, _signedGradient);
}

// C++:   cv::HOGDescriptor::HOGDescriptor(Size _winSize, Size _blockSize, Size _blockStride, Size _cellSize, int _nbins, int _derivAperture = 1, double _winSigma = -1, HOGDescriptor_HistogramNormType _histogramNormType = HOGDescriptor::L2Hys, double _L2HysThreshold = 0.2, bool _gammaCorrection = false, int _nlevels = HOGDescriptor::DEFAULT_NLEVELS, bool _signedGradient = false)
private static native long HOGDescriptor_1(double _winSize_width, double _winSize_height, double _blockSize_width, double _blockSize_height, double _blockStride_width, double _blockStride_height, double _cellSize_width, double _cellSize_height, int _nbins, int _derivAperture, double _winSigma, int _histogramNormType, double _L2HysThreshold, boolean _gammaCorrection, int _nlevels, boolean _signedGradient);
private static native long HOGDescriptor_2(double _winSize_width, double _winSize_height, double _blockSize_width, double _blockSize_height, double _blockStride_width, double _blockStride_height, double _cellSize_width, double _cellSize_height, int _nbins, int _derivAperture, double _winSigma, int _histogramNormType, double _L2HysThreshold, boolean _gammaCorrection, int _nlevels);
private static native long HOGDescriptor_3(double _winSize_width, double _winSize_height, double _blockSize_width, double _blockSize_height, double _blockStride_width, double _blockStride_height, double _cellSize_width, double _cellSize_height, int _nbins, int _derivAperture, double _winSigma, int _histogramNormType, double _L2HysThreshold, boolean _gammaCorrection);
private static native long HOGDescriptor_4(double _winSize_width, double _winSize_height, double _blockSize_width, double _blockSize_height, double _blockStride_width, double _blockStride_height, double _cellSize_width, double _cellSize_height, int _nbins, int _derivAperture, double _winSigma, int _histogramNormType, double _L2HysThreshold);
private static native long HOGDescriptor_5(double _winSize_width, double _winSize_height, double _blockSize_width, double _blockSize_height, double _blockStride_width, double _blockStride_height, double _cellSize_width, double _cellSize_height, int _nbins, int _derivAperture, double _winSigma, int _histogramNormType);
private static native long HOGDescriptor_6(double _winSize_width, double _winSize_height, double _blockSize_width, double _blockSize_height, double _blockStride_width, double _blockStride_height, double _cellSize_width, double _cellSize_height, int _nbins, int _derivAperture, double _winSigma);
private static native long HOGDescriptor_7(double _winSize_width, double _winSize_height, double _blockSize_width, double _blockSize_height, double _blockStride_width, double _blockStride_height, double _cellSize_width, double _cellSize_height, int _nbins, int _derivAperture);
private static native long HOGDescriptor_8(double _winSize_width, double _winSize_height, double _blockSize_width, double _blockSize_height, double _blockStride_width, double _blockStride_height, double _cellSize_width, double _cellSize_height, int _nbins);

HOGDescriptor 参数简介
_winSize 将整个图片按_winSize切分成相同大小的窗口。
_blockSize 将每个窗口按一定大小分割成多个相同的块。
_blockStride _winSize块滑动增量大小(每切分一次下一次切分移动的大小)。
_cellSize _winSize切分成多个相同的细胞
_nbins 一个_cellSize中统计梯度的方向数目。bins(可理解为划分的个数)决定方向的划分。一般bins取9,将梯度方向划分为9个区间。
_derivAperture
_winSigma
_histogramNormType
_L2HysThreshold
_gammaCorrection
_nlevels
_signedGradient

2 HOG 特征提取

2.1 检测窗口

HOG通过窗口(window)和块(block)将图像进行分割。通过以细胞(cell)为单位,对图像某一区域的像素值进行数学计算处理。在此先介绍窗口(window)、块(block)和细胞(cell)的概念及之间的联系。

2.2 归一化图像

归一化分为gamma空间和颜色空间归一化。为减少光照因素影响,将整个图像进行规范化(归一化)。(归一化公式:y=(x-MinValue)/(MaxValue-MinValue))。归一化同时可以避免在图像的纹理强度中,局部的表层曝光贡献度的比重较大的情况。

Core.normalize(mask, mask, 255, 0, Core.NORM_MINMAX);

2.3 计算梯度

计算图像横坐标和纵坐标方向的梯度,并根据横坐标和纵坐标的梯度,计算梯度方向。

2.4 统计直方图

HOG构建方向梯度直方图在细胞(cell)中完成

2.5 梯度直方图归一化

块内进行细胞归一化梯度直方图

2.6 得到HOG特征向量

最后组合所有的块,生成特征向量

3 Java HOG 特征提取测试

package com.xu.opencv.image;


import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.HOGDescriptor;

/**
 * HOGDescriptor 特征提取
 *
 * @ClassName: HOG
 * @Description: TODO
 * @author: hyacinth
 * @date: 2019年8月4日 上午10:10:53
 * @Copyright: hyacinth
 */
public class HOG {

    static {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

    public static void main(String[] args) {
        hog();
    }

    /**
     * OpenCV-4.1.0 HOGDescriptor 特征提取
     *
     * @return: void
     * @date: 2019年8月4日10:09:58
     */
    public static void hog() {
        Mat src = Imgcodecs.imread("C:\\Users\\hyacinth\\Desktop\\1.jpeg");
        Mat gary = new Mat();
        Imgproc.cvtColor(src, gary, Imgproc.COLOR_BGR2GRAY);
        Imgproc.resize(gary, gary, new Size(64, 128));
        HOGDescriptor hog = new HOGDescriptor(new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8), 9);
        MatOfFloat descriptors = new MatOfFloat();
        hog.compute(gary, descriptors, new Size(0, 0), new Size(0, 0));
        System.out.println(descriptors.size());
    }

}
1x3780

4 C# HOG 特征提取测试

//
// 摘要:
//     Creates the HOG descriptor and detector.
//
// 参数:
//   winSize:
//     Detection window size. Align to block size and block stride.
//
//   blockSize:
//     Block size in pixels. Align to cell size. Only (16,16) is supported for now.
//
//   blockStride:
//     Block stride. It must be a multiple of cell size.
//
//   cellSize:
//     Cell size. Only (8, 8) is supported for now.
//
//   nbins:
//     Number of bins. Only 9 bins per cell are supported for now.
//
//   derivAperture:
//
//   winSigma:
//     Gaussian smoothing window parameter.
//
//   histogramNormType:
//
//   l2HysThreshold:
//     L2-Hys normalization method shrinkage.
//
//   gammaCorrection:
//     Flag to specify whether the gamma correction preprocessing is required or not.
//
//   nlevels:
//     Maximum number of detection window increases.
public HOGDescriptor(Size? winSize = null, Size? blockSize = null, Size? blockStride = null, Size? cellSize = null, int nbins = 9, int derivAperture = 1, double winSigma = -1, HistogramNormType histogramNormType = HistogramNormType.L2Hys, double l2HysThreshold = 0.2, bool gammaCorrection = true, int nlevels = 64);
public static void Hog() {
	Mat src = new(@"C:\Users\hyacinth\Desktop\1.jpg", ImreadModes.Color);
	HOGDescriptor descriptor = new(new Size(64, 128), new Size(16, 16), new Size(8, 8), new Size(8, 8), 9);
	descriptor.Compute(src);
	Console.WriteLine(descriptor.GetDescriptorSize());
}

Java OpenCV 图像处理26.0 HOG 特征提取_第1张图片

你可能感兴趣的:(Java,OpenCV,Java,OpenCV-4.1.0,HOG特征提取,HOGDescriptor)