stream opencv mat (webcam)frame throught tcp by boost asio
客户端(client):
/* * ===================================================================================== * * Filename: client.cpp * * Description: stream opencv Mat frame to server by tcp with boost asio * * * Version: 1.0 * Created: 2014/4/29 11:40:20 * Author: yuliyang * * Mail: wzyuliyang911@gmail.com * Blog: http://www.cnblogs.com/yuliyang * * ===================================================================================== */ #include <iostream> #include <boost/array.hpp> #include <boost/asio.hpp> #include <opencv2/objdetect/objdetect.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() { 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 */ //capture.read(frame); //frame=imread("g://tinyproxy.jpg"); while (true) { cap>>frame; imshow("client",frame); waitKey(100); frame = (frame.reshape(0,1)); // to make it continuous int num=frame.total(); /* num = 320*240 */ int num2=frame.elemSize(); /* mun2 =3 channel */ int imgSize = frame.total()*frame.elemSize(); try { boost::asio::io_service io_service; tcp::endpoint end_point(boost::asio::ip::address::from_string("127.0.0.1"), 3200); tcp::socket socket(io_service); socket.connect(end_point); boost::system::error_code ignored_error; /*time_t now = time(0); std::string message = ctime(&now); socket.write_some(boost::asio::buffer(message), ignored_error);*/ //std::string message(frame.begin<unsigned char>(),frame.end<unsigned char>()); std::string message((char *)frame.data,230400); /* the size of mat data is 320*240*3 */ cout<<"sending...."<<endl; socket.write_some(boost::asio::buffer(message), ignored_error); cout<<"send image finished"<<endl; } catch (std::exception& e) { std::cerr << e.what() << std::endl; } } return 0; //try //{ // boost::asio::io_service io_service; // tcp::resolver resolver(io_service); // char* serverName = "localhost"; // tcp::resolver::query query(serverName, "daytime"); // tcp::resolver::iterator endpoint_iterator = resolver.resolve(query); // tcp::socket socket(io_service); // while(true) // { // boost::asio::connect(socket, endpoint_iterator); // for (;;) // { // boost::array<char, 128> buf; // boost::system::error_code error; // size_t len = socket.read_some(boost::asio::buffer(buf), error); // if (error == boost::asio::error::eof) // break; // Connection closed cleanly by peer. // else if (error) // throw boost::system::system_error(error); // Some other error. // std::cout.write(buf.data(), len); // std::cout <<"\n"; // } // } //} //catch (std::exception& e) //{ // std::cerr << e.what() << std::endl; //} //return 0; }
服务器端(server)
/* * ===================================================================================== * * Filename: server.cpp * * Description: stream opencv mat frame server * * * Version: 1.0 * Created: 2014/4/29 11:44:51 * Author: yuliyang * * Mail: wzyuliyang911@gmail.com * Blog: http://www.cnblogs.com/yuliyang * * ===================================================================================== */ #include <iostream> #include <string> #include <boost/asio.hpp> #include <boost/array.hpp> #include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include <opencv2/objdetect/objdetect.hpp> #include <iostream> #include <vector> #include <boost/thread/thread.hpp> using boost::asio::ip::tcp; using namespace std; using namespace cv; Mat img = Mat::zeros( 320,240, CV_8UC3); bool flag = false; /* if flag is false ,the thread is not ready to show the mat frame */ void servershow() { while (true) { if (flag) { imshow("server",img); waitKey(20); } } } int main() { boost::thread thrd(&servershow); try { boost::asio::io_service io_service; boost::array<char, 230400> buf; /* the size of reciev mat frame is caculate by 320*240*3 */ tcp::acceptor acceptor(io_service, tcp::endpoint(tcp::v4(), 3200)); for (;;) { tcp::socket socket(io_service); acceptor.accept(socket); boost::system::error_code error; size_t len = socket.read_some(boost::asio::buffer(buf), error); cout<<"get data length :"<<len<<endl; /* disp the data size recieved */ std::vector<uchar> vectordata(buf.begin(),buf.end()); /* change the recieved mat frame(1*230400) to vector */ cv::Mat data_mat(vectordata,true); /* cout<<"cols:"<<data_mat.cols<<endl; cout<<"rows:"<<data_mat.rows<<endl; cout<<"total:"<<data_mat.total()<<endl; cout<<"elemSize:"<<data_mat.elemSize()<<endl;*/ img= data_mat.reshape(3,240); /* reshape to 3 channel and 240 rows */ cout<<"reshape over"<<endl; flag =true; /*imshow("server",img); waitKey(100);*/ //imwrite("save.jpg",img); /*std::string message = "This is the Server!"; boost::system::error_code ignored_error; boost::asio::write(socket, boost::asio::buffer(message), ignored_error);*/ } } catch (std::exception& e) { std::cerr << e.what() << std::endl; } thrd.join(); return 0; }
效果(run like follows):
source code: https://github.com/wzyuliyang/StreamOpencvMatDemo
作者:小菜鸟_yang
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。