linux opencv調用手機攝像頭

手機端安裝IP攝像頭

先上源碼:

#include "cv.h"
#include "highgui.h"
#include <iostream>


int main(int, char**) {
    cv::VideoCapture vcap;
     cv::Mat image;
CvCapture  *capture ;
    const std::string videoStreamAddress = "http://10.133.225.11:8080/video"; 
    

   
    if(!vcap.open(videoStreamAddress)) {
        std::cout << "Error opening video stream or file" << std::endl;
        return -1;
    }


    //Create output window for displaying frames. 
    //It's important to create this window outside of the `for` loop
    //Otherwise this window will be created automatically each time you call
    //`imshow(...)`, which is very inefficient. 
    cv::namedWindow("Output Window");




    for(;;) {
        if(!vcap.read(image)) {
            std::cout << "No frame" << std::endl;
            cv::waitKey();
        }
        cv::imshow("Output Window", image);


       if(cv::waitKey(1) >= 0) break;
  
    }   
}



編譯:g++ `pkg-config --cflags opencv` -o camera camera.cpp `pkg-config --libs opencv`

運行 即可


方法二:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;

int main()
{
    VideoCapture cap;
    Mat img;
    while (true)
    {
        cap.open("http://10.133.225.11:8080/video");
        if (cap.isOpened())
        {
            cap.read(img);
            imshow("win", img);
            if ((uchar)waitKey(1) == 27)
                break;
        }
        else
            break;
    }
}


方法三:不用VIdeoCapture,用cvCapture

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>    
#include <opencv2/highgui/highgui.hpp>    
#include <opencv2/imgproc/imgproc.hpp>  
using namespace cv;


int main()  
{  
     
    Mat img;  
    Mat mat(480, 640, CV_8UC4);  
    while (true)  
    {  
        CvCapture *camera=cvCaptureFromFile("http://10.133.225.11:8080/shot.jpg");
        IplImage *img=cvQueryFrame(camera);
      
            imshow("win", Mat(img));  
            if ((uchar)waitKey(1) == 27)  
                break;  
    }  
}  

關於使用yolo調用遠程攝像頭的方法:

在YOLO src文件夾下,編輯demo.c,

修改代碼

cap=cvCaptureFromFile("http://10.133.225.11:8080/shot.jpg")

在fetch_in_treads函數也添加代碼:

cap=cvCaptureFromFile("http://10.133.225.11:8080/shot.jpg")

即可實現YOLO遠程調用攝像頭。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章