码迷,mamicode.com
首页 > Web开发 > 详细

raspberry pi (树莓PI)使用socket向PC发送视频流

时间:2014-10-21 12:05:34      阅读:371      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   color   io   os   ar   使用   java   

安装boost

sudo apt-get install libboost1.50-all

 

测试boost

1.cpp

#include <string>
#include <boost/regex.hpp>
 
int main() {
 
        std::string text("a fat cat sat on the mat");
        boost::regex re("\\w+");
 
        boost::sregex_token_iterator i(text.begin(), text.end(), re, 0);
        boost::sregex_token_iterator end;
 
        for( ; i != end ; ++i ) {
                std::cout << *i << ‘ ‘;
        }
 
        std::cout << std::endl;
 
        return 0;
 
}

 编译

g++ -std=c++0x -lstdc++ -lboost_regex it.cpp

 运行

./a.out

 

安装opencv

apt-cache search opencv
sudo apt-get install libopencv-dev

 

测试opencv

我使用的webcam型号是罗技c270

bubuko.com,布布扣

 2.cpp

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>

using namespace cv;
int main()
{
	VideoCapture cap(0);
	if(!cap.isOpened())
	{
		return -1;
	}
	Mat frame;
	Mat edges;
		cap>>frame;
		cvtColor(frame, edges, CV_BGR2GRAY);
		GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
		Canny(edges, edges, 0, 30, 3);
		imwrite("frame.jpg",frame);
		imwrite("edges.jpg",edges);
	return 0;
}

 编译

 g++ -std=c++0x -lstdc++ -lopencv_highgui -lopencv_video -lopencv_core -lopencv_calib3d -lopencv_contrib -lopencv_imgproc  2.cpp

 

编写传输图像代码

 

#include <iostream>
#include <boost/array.hpp>
#include <boost/asio.hpp>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/core/core.hpp>  
#include <ctime>
#include <string>
using boost::asio::ip::tcp;
using namespace  std;
using namespace cv;

int main(int args, char *argv[])
{
	VideoCapture cap(0);                        /* open webcam */
	if(!cap.isOpened())  
	{  
		return -1;  
	}  
	Mat frame;
	cap.set(CV_CAP_PROP_FRAME_WIDTH, 320);      /* set width */
	cap.set(CV_CAP_PROP_FRAME_HEIGHT, 240);     /* set height */
	try
	{
		boost::asio::io_service io_service;
		tcp::endpoint end_point(boost::asio::ip::address::from_string(argv[1]), 3200);
		tcp::socket socket(io_service);
        	boost::system::error_code ignored_error;
		socket.connect(end_point);
		while (true)
		{
			cap>>frame;
			char c=(char)waitKey(100);
			if (c==27)
			{
				break;
			}
			frame = (frame.reshape(0,1)); // to make it continuous
			std::string message((char *)frame.data,230400); /* the size of mat data is 320*240*3 */
			socket.write_some(boost::asio::buffer(message), ignored_error);
			cout<<"send image finished"<<endl;
		}
		socket.close();
	}
	catch (std::exception& e)
	{
		std::cerr << e.what() << std::endl;
	}

		return 0;
}

 

编译

 

 g++ -std=c++0x -lstdc++ -lopencv_highgui -lopencv_video -lopencv_core -lopencv_calib3d -lopencv_contrib -lopencv_imgproc  -lboost_system  -lboost_thread  -lpthread  3.cpp

 

 

raspberry pi (树莓PI)使用socket向PC发送视频流

标签:des   blog   http   color   io   os   ar   使用   java   

原文地址:http://www.cnblogs.com/yuliyang/p/4039807.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!