Java OpenCV 图像处理26.2 HOG特征提取 实时视频行人检测

Java OpenCV 图像处理26.2 HOG特征提取 实时视频行人检测

1 读取视频中的图片
2 图片转灰度图片
3 加载 HOGDescriptor 自带行人检测
4 循环检测结果画出检测结果轮廓
5 显示结果

package com.xu.detector;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDouble;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.highgui.HighGui;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.HOGDescriptor;
import org.opencv.videoio.VideoCapture;

/**
 * All rights Reserved, Designed By Hyacinth
 *
 * @version V1.0
 * @Title: OpenCVDetector.java
 * @Package com.xu.detector
 * @Description:
 * @author: hyacinth
 * @date: 2021年1月30日 23点27分
 * @Copyright:
 */
public class OpenCVDetector {


    static {
        // 在使用OpenCV前必须加载Core.NATIVE_LIBRARY_NAME类,否则会报错
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    }

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

    /**
     * 从视频获取视频
     *
     * @throws
     * @Title: readVideo
     * @Description: TODO
     * @param:
     * @date: 2021年1月30日 23点27分
     * @return: void
     */
    public static void readVideo(String path) {
        VideoCapture capture = new VideoCapture();
        capture.open(path);
        Mat image = new Mat();
        int index = 0;
        if (capture.isOpened()) {
            while (true) {
                capture.read(image);
                HighGui.imshow("行人检测", pedestrianDetection(image));
                index = HighGui.waitKey(1);
                if (index == 27) {// 需要按住退出键1s
                    System.exit(0);
                }
            }
        }
    }

    /**
     * 从摄像头获取视频
     *
     * @throws
     * @Title: readCamera
     * @Description: TODO
     * @param:
     * @date: 2021年1月30日 23点27分
     * @return: void
     */
    public static void readCamera() {
        VideoCapture capture = new VideoCapture(0);
        Mat image = new Mat();
        int index = 0;
        if (capture.isOpened()) {
            while (true) {
                capture.read(image);
                HighGui.imshow("行人检测", pedestrianDetection(image));
                index = HighGui.waitKey(1);
                if (index == 27) {// 需要按住退出键1s
                    System.exit(0);
                }
            }
        }
    }


    /**
     * OpenCV-4.1.0 HOGDescriptor 自带行人检测
     *
     * @return
     * @return: void
     * @date: 2021年1月30日 23点27分
     */
    public static Mat pedestrianDetection(Mat src) {
        Mat gary = new Mat();
        Imgproc.cvtColor(src, gary, Imgproc.COLOR_BGR2GRAY);
        HOGDescriptor hog = new HOGDescriptor();
        hog.setSVMDetector(HOGDescriptor.getDefaultPeopleDetector());
        MatOfRect rect = new MatOfRect();
        hog.detectMultiScale(gary, rect, new MatOfDouble(), 1.05, new Size(4, 4), new Size(32, 32));
        Rect[] rects = rect.toArray();
        for (int i = 0; i < rects.length; i++) {
            Imgproc.rectangle(src, new Point(rects[i].x, rects[i].y), new Point(rects[i].x + rects[i].width, rects[i].y + rects[i].height), new Scalar(0, 0, 255), 2, Imgproc.LINE_AA);
        }
        return src;
    }

}

Java OpenCV 图像处理26.2 HOG特征提取 实时视频行人检测_第1张图片

你可能感兴趣的:(Java,OpenCV,opencv,计算机视觉,cv,人脸识别,视频处理)