通過javacv對視訊每隔1秒鐘擷取1張圖片
阿新 • • 發佈:2019-02-18
之前每一秒鐘擷取一張圖片,發現有些圖片報了“[mpeg4 @ 05938aa0] warning: first frame is no keyframe”這個警告,而且截出的圖片都是灰屏,根本沒有圖片。後來在網上找了很久,終於弄明白了,原來是ffmpeg它有“關鍵幀”這個說法,所以如果設定的幀的位置不是關鍵幀的位置的話,就可能截出的圖片有問題。後來經過改進,終於搞定了。
public static void main(String[] args) { System.out.println(System.getProperty("java.library.path")); // System.out.println("Welcome to OpenCV " + Core.VERSION); // System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // Mat m = Mat.eye(3, 3, CvType.CV_8UC1); // System.out.println("m = " + m.dump()); // 載入本地的OpenCV庫,這樣就可以用它來呼叫Java API System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Test t = new Test(); // t.test(); // t.run(); // t.run2(); t.run3(); // System.out.println(t.CmpPic("d:/img/219.jpg")); }
public void run3() { CvCapture capture = opencv_highgui.cvCreateFileCapture("D:/085402.crf"); //幀率 int fps = (int) opencv_highgui.cvGetCaptureProperty(capture, opencv_highgui.CV_CAP_PROP_FPS); System.out.println("幀率:"+fps); IplImage frame = null; double pos1 = 0; int rootCount = 0; while (true) { //讀取關鍵幀 frame = opencv_highgui.cvQueryFrame(capture); rootCount = fps; while(rootCount > 0 ){ //這一段的目的是跳過每一秒鐘的幀數,也就是說fps是幀率(一秒鐘有多少幀),在讀取一幀後,跳過fps數量的幀就相當於跳過了1秒鐘。 frame = opencv_highgui.cvQueryFrame(capture); rootCount--; } //獲取當前幀的位置 pos1 = opencv_highgui.cvGetCaptureProperty(capture,opencv_highgui.CV_CAP_PROP_POS_FRAMES); System.out.println(pos1); if (null == frame) break; opencv_highgui.cvSaveImage("d:/img/" + pos1 + ".jpg",frame); } opencv_highgui.cvReleaseCapture(capture); }