stream opencv mat (webcam)frame throught tcp by boost asio

客户端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
 * =====================================================================================
 *
 *       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;
}

 服务器端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * =====================================================================================
 *
 *       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;
}

 效果:

stream opencv mat (webcam)frame throught tcp by boost 
asio

stream opencv mat (webcam)frame throught tcp by boost asio,布布扣,bubuko.com

stream opencv mat (webcam)frame throught tcp by boost asio

上一篇:CSS架构


下一篇:按键精灵*ff