首页 / JAVA / java-在当前帧之前存储视频的帧?
java-在当前帧之前存储视频的帧?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-在当前帧之前存储视频的帧?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3668字,纯文字阅读大概需要6分钟。
内容图文
![java-在当前帧之前存储视频的帧?](/upload/InfoBanner/zyjiaocheng/662/1341cec7a3f54ebd9088293e2aeb5a32.jpg)
我目前有一段显示视频的代码,我正在尝试对其进行扩展,以便显示当前帧与上一帧之间的差异,以便检测运动.我知道我将不得不使用absdiff()函数来显示两个图像之间的对比度,但是我不确定如何将前一帧存储为Mat.有人可以看看下面的代码,告诉我应该在哪里添加“上一帧”代码以及应该写什么.我以为用不了多久,我只是在网上找不到任何教程…
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.VideoCapture;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
// ********************************************************
public class CaptureVideo {
public static void main(String[] args) throws InterruptedException {
// load the Core OpenCV library by name
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
// create video capture device object
VideoCapture cap = new VideoCapture();
// try to use the hardware device if present
int CAM_TO_USE = 0;
// create a new image object
Mat matFrame = new Mat();
// try to open first capture device (0)
try {
cap.open(CAM_TO_USE);
} catch (Exception e1) {
System.out.println("No webcam attached");
// otherwise try opening a video file
try{
cap.open("files/video.mp4");
} catch (Exception e2) {
System.out.println("No video file found");
}
}
// if the a video capture source is now open
if (cap.isOpened())
{
// create a new window object
Imshow ims = new Imshow("From video source ... ");
boolean keepProcessing = true;
while (keepProcessing)
{
// grab the next frame from video source
cap.grab();
// decode and return the grabbed video frame
cap.retrieve(matFrame);
// if the frame is valid (not end of video for example)
if (!(matFrame.empty()))
{
// *** to any processing here***
// display image with a delay of 40ms (i.e. 1000 ms / 25 = 25 fps)
ims.showImage(matFrame);
Thread.sleep(40);
} else {
keepProcessing = false;
}
}
} else {
System.out.println("error cannot open any capture source - exiting");
}
// close down the camera correctly
cap.release();
}
}
我尝试在else语句中的任何一个之后添加以下代码,但是产生的全部是黑屏(我认为是因为“ previousFrame”只是“ matFrame”的重复):
previousFrame = matFrame;
Core.absdiff(matFrame, previousFrame, diffFrame);
ims.showImage(diffFrame);
谁能指出我要去哪里了?
解决方法:
做previousFrame = matFrame;使previousFrame指向matFrame.您没有在复制矩阵-先前的帧和matFrame都引用同一对象.因此,您实际上是在将矩阵与其本身相减,这就是为什么您得到黑框的原因.
您需要进行适当的复制,因此需要使用Mat.clone()克隆矩阵.但是,如果要查找两个帧之间的差异,请记住,至少需要读取两个帧才能实现此目的.当我们仅读视频的第一帧时,我们真的找不到差异帧,因此我们应该检查一下这是否是第一帧.如果是第一帧,则只显示相应的普通帧.在那之后,我们可以通过确保在复制下一帧之前先复制前一帧来显示差异帧.
因此,请为您的循环执行此操作:
// ****** Change - add a flag to check to see if the first frame has been read
boolean firstFrame = true;
// ****** Change - declare previous frame here
Mat previousFrame;
while (keepProcessing)
{
// ****** Change - Save previous frame before getting next one
// Only do this if the first frame has passed
if (!firstFrame)
previousFrame = matFrame.clone();
// grab the next frame from video source
cap.grab();
// decode and return the grabbed video frame
cap.retrieve(matFrame);
// if the frame is valid (not end of video for example)
if (!(matFrame.empty()))
{
// **** Change - If we are on the first frame, only show that and
// set the flag to false
if (firstFrame) {
ims.showImage(matFrame);
firstFrame = false;
}
// ***** Change - now show absolute difference after first frame
else {
Core.absdiff(matFrame, previousFrame, diffFrame);
ims.showImage(diffFrame);
}
// display image with a delay of 40ms (i.e. 1000 ms / 25 = 25 fps)
Thread.sleep(40);
} else {
keepProcessing = false;
}
}
内容总结
以上是互联网集市为您收集整理的java-在当前帧之前存储视频的帧?全部内容,希望文章能够帮你解决java-在当前帧之前存储视频的帧?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。