天天看點

OpenCV-視訊讀寫(java版)

@​​TOC​​

​​

​OpenCV 2 中提供了兩個類來實作視訊的讀寫。讀視訊的類是​

​VideoCapture​

​,寫視訊的類是 ​

​VideoWriter

讀視訊

VideoCapture 既可以從視訊檔案讀取圖像,也可以從攝像頭讀取圖像。該類提供了一系列構造函數和方法打開視訊檔案或攝像頭。下方對VideoCapture的常用方法進行說明

方法 說明

open()

VideoCapture()

open(index)

VideoCapture(index)

index表示要打開的視訊捕獲裝置的ID

通過構造函數的方式打開第index個攝像頭

(下方不再說明)

open(int index, int apiPreference)

apiPreference:媒體類型,預設時預設為  CAP_ANY

(更多檢視Videoio類)

open(int index, int apiPreference, MatOfInt params)
open(String filename)

打開一個視訊檔案。

filename:檔案位址

open(String filename, int apiPreference)
open(String filename, int apiPreference, MatOfInt params)

示例:

public static void main(String[] args) {
        String libraryPath= System.getProperty("user.dir")+"\\lib\\opencv_java460.dll";
        System.load(libraryPath);
        VideoCapture vc = new VideoCapture();
        //打開攝像頭
        vc.open(0);
        Mat mat = new Mat();
        while(vc.read(mat)){
            for(int i=0;i<mat.rows();i++){
                for (int j=0;j<mat.cols();j++){
                    if (i>(mat.rows()/2)   ){
                        //給相應位置賦予像素值
                        mat.put(i,j,144,238,144);
                    }
                }
            }
            HighGui.imshow("test", mat);
            HighGui.waitKey(1);
        }
        
        vc.release();
        //關閉視窗
        HighGui.destroyAllWindows();
  
    }      

參考文檔: