Linux 下gocv 讀取USB攝像頭,使用MJPG


gocv 默認應該是YUV, YUV是原始數據,相對MJPEG表示圖片,數據量比較大,所以占用帶寬就很大,會出現視頻不流暢情況

所以需要顯示指定使用MJPEG,相對YUV會流暢很多,首先需要查看當前有幾個視頻設備:

ls /dev/video*
/dev/video0  /dev/video1

我當前只插了一個USB,但是會有兩個video,暫時不知道為什么...  O-O,當前我需要使用video0(通過video0,video1逐個試出來的...)

//"gocv.io/x/gocv"
var webcam *gocv.VideoCapture
var win *gocv.Window
func main()  {
    webcam, _ := gocv.VideoCaptureDevice(0)//0: video索引
    webcam.Set(gocv.VideoCaptureFOURCC, webcam.ToCodec("MJPG"))//指定MJPEG方式
    webcam.Set(gocv.VideoCaptureFrameWidth, 1920)//寬
    webcam.Set(gocv.VideoCaptureFrameHeight, 960)//高
    win = gocv.NewWindow("")//創建window窗口
    for {
        imgMat := gocv.NewMat()
        webcam.Read(&imgMat)//讀取一張圖片到imgMat
        win.IMShow(imgMat)//展示圖片
        win.WaitKey(1)//展示1毫秒立即關閉
    }
}

注: 寬高要符合攝像頭支持寬高,可以通過v4l2-ctl --all查看默認寬高和一些其他信息

v4l2-ctl --all
Driver Info:
    Driver name      : uvcvideo
    Card type        : HJ_800W: HJ_800W
    Bus info         : usb-0000:00:1a.7-1
    Driver version   : 5.8.18
    Capabilities     : 0x84a00001
        Video Capture
        Metadata Capture
        Streaming
        Extended Pix Format
        Device Capabilities
    Device Caps      : 0x04200001
        Video Capture
        Streaming
        Extended Pix Format
Priority: 2
Video input : 0 (Camera 1: ok)
Format Video Capture:
    Width/Height      : 1920/1080 Pixel Format : 'MJPG' (Motion-JPEG)
    Field             : None
    Bytes per Line    : 0
    Size Image        : 4147200
    Colorspace        : sRGB
    Transfer Function : Default (maps to sRGB)
    YCbCr/HSV Encoding: Default (maps to ITU-R 601)
    Quantization      : Default (maps to Full Range)
    Flags             : 
Crop Capability Video Capture:
    Bounds      : Left 0, Top 0, Width 1920, Height 1080
    Default     : Left 0, Top 0, Width 1920, Height 1080
    Pixel Aspect: 1/1
Selection Video Capture: crop_default, Left 0, Top 0, Width 1920, Height 1080, Flags: 
Selection Video Capture: crop_bounds, Left 0, Top 0, Width 1920, Height 1080, Flags: 
Streaming Parameters Video Capture:
    Capabilities     : timeperframe
    Frames per second: 30.000 (30/1)
    Read buffers     : 0
                     brightness 0x00980900 (int)    : min=0 max=127 step=1 default=0 value=0
                       contrast 0x00980901 (int)    : min=0 max=95 step=1 default=4 value=4
                     saturation 0x00980902 (int)    : min=0 max=255 step=1 default=100 value=100
                            hue 0x00980903 (int)    : min=-2000 max=2000 step=1 default=0 value=0
 white_balance_temperature_auto 0x0098090c (bool)   : default=1 value=1
                          gamma 0x00980910 (int)    : min=80 max=300 step=1 default=115 value=115
           power_line_frequency 0x00980918 (menu)   : min=0 max=2 default=1 value=1
                0: Disabled
                1: 50 Hz
                2: 60 Hz
      white_balance_temperature 0x0098091a (int)    : min=2806 max=6500 step=1 default=4600 value=4600 flags=inactive
                      sharpness 0x0098091b (int)    : min=0 max=100 step=1 default=0 value=0
         backlight_compensation 0x0098091c (int)    : min=0 max=1 step=1 default=0 value=0
                  exposure_auto 0x009a0901 (menu)   : min=0 max=3 default=3 value=3
                1: Manual Mode
                3: Aperture Priority Mode
              exposure_absolute 0x009a0902 (int)    : min=1 max=10000 step=1 default=156 value=156 flags=inactive
                 focus_absolute 0x009a090a (int)    : min=0 max=100 step=1 default=5 value=5 flags=inactive
                     focus_auto 0x009a090c (bool)   : default=1 value=1

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM