基于JavaCv并发读取本地视频流并提取每帧32位dhash特征

时间:2024-02-21 09:30:00

1.读取本地视频流,pom依赖

依赖于 org.bytedeco下的javacv/opencv/ffmpeg 包

        <dependency>
            <groupId>org.bytedeco</groupId>
            <artifactId>javacv</artifactId>
            <version>1.4.3</version>
        </dependency>
        <dependency>
            <groupId>org.bytedeco.javacpp-presets</groupId>
            <artifactId>opencv</artifactId>
            <version>3.4.3-1.4.3</version>
            <classifier>linux-x86_64</classifier>
        </dependency>
        <dependency>
            <groupId>org.bytedeco.javacpp-presets</groupId>
            <artifactId>ffmpeg</artifactId>
            <version>4.0.2-1.4.3</version>
            <classifier>linux-x86_64</classifier>
        </dependency>

2.读取本地视频流并解帧为 opencv_core.Mat

File file = new File("/home/lab/javacv/t11.mp4");
FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(file);

grabber.start();

// 解帧为opencv_core.Mat
List<opencv_core.Mat> mats = new ArrayList<>();
for (int i = 0; i < grabber.getLengthInFrames(); i++) {
    Frame frame = grabber.grabImage();
    OpenCVFrameConverter.ToMat toMat = new OpenCVFrameConverter.ToMat();
    opencv_core.Mat mat = toMat.convert(frame);
    if (mat != null) {
        mats.add(mat.clone());
    }
}

grabber.stop();

3.获取32位dhash特征

dhash特征提取思路,图片Mat转为单通道的灰度图,并重置为5*5的Size,最后将其转储为长度为 25 的byte数组用以求取32位dhash特征

// 声明空的灰度图 Mat
opencv_core.Mat grayImg = new opencv_core.Mat(mat.rows(), mat.cols(), opencv_imgcodecs.IMREAD_GRAYSCALE);
// 转储为灰度图
opencv_imgproc.cvtColor(mat, grayImg, opencv_imgproc.COLOR_RGB2GRAY);
// 修改Mat长宽size
opencv_core.Mat resizedImg = new opencv_core.Mat();
opencv_core.Size size = new opencv_core.Size(5,5);
opencv_imgproc.resize(grayImg,resizedImg,size);
// 转为 5*5 byte 数组
byte[] bytePixels = new byte[5 * 5];
resizedImg.data().get(bytePixels);
int[] pixels = new int[bytePixels.length];
for (int i=0; i<pixels.length; i++) {
    pixels[i] = bytePixels[i] & 0xff;
}
// 获取32位dhash特征
int feature = 0;
for (int j=0; j<4; j++) {
    for (int i=0; i<4; i++) {
        int colBit = pixels[i*5+j] > pixels[(i+1)*5+j] ? 1 : 0;
        feature = (feature << 1) + colBit;
        int rowBit = pixels[i*5+j] > pixels[i*5+j+1] ? 1 : 0 ;
        feature = (feature << 1) + rowBit;
    }
}

多线程部分,可参考该博: https://www.cnblogs.com/nyatom/p/10119306.html