红联Linux门户
Linux帮助

linux opencv调用手机摄像头

发布时间:2017-02-06 09:40:43来源:linux网站作者:lisarer
手机端安装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远程调用摄像头。
 
本文永久更新地址:http://www.linuxdiyf.com/linux/28173.html