raspberry pi (树莓PI)使用socket向PC发送视频流
安装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
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
代码(opencv Mat 图像jpeg压缩后通过socket传输,性能6倍提升)
http://pan.baidu.com/s/1sjO9pmd
作者:小菜鸟_yang
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 
                    
                
                
            
        
浙公网安备 33010602011771号