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

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

        <dependency>
<groupId>org.bytedeco</groupId>
<artifactId>javacv</artifactId>
<version>1.4.</version>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>opencv</artifactId>
<version>3.4.-1.4.</version>
<classifier>linux-x86_64</classifier>
</dependency>
<dependency>
<groupId>org.bytedeco.javacpp-presets</groupId>
<artifactId>ffmpeg</artifactId>
<version>4.0.-1.4.</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 = ; 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(,);
opencv_imgproc.resize(grayImg,resizedImg,size);
// 转为 5*5 byte 数组
byte[] bytePixels = new byte[ * ];
resizedImg.data().get(bytePixels);
int[] pixels = new int[bytePixels.length];
for (int i=; i<pixels.length; i++) {
pixels[i] = bytePixels[i] & 0xff;
}
// 获取32位dhash特征
int feature = ;
for (int j=; j<; j++) {
for (int i=; i<; i++) {
int colBit = pixels[i*+j] > pixels[(i+)*+j] ? : ;
feature = (feature << ) + colBit;
int rowBit = pixels[i*+j] > pixels[i*+j+] ? : ;
feature = (feature << ) + rowBit;
}
}

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

04-26 13:10