stream opencv mat (webcam)frame throught tcp by boost asio
2020-11-15 18:54
标签:des cWeb com http blog style class div img code java 客户端: 服务器端 效果:
/*
* =====================================================================================
*
* 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
#include
#include
#include
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include
#include
#include
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
std::string message((
char
*)frame.data,230400);
/* the size of mat data is 320*240*3 */
cout
"sending...."
socket.write_some(boost::asio::buffer(message), ignored_error);
cout
"send image finished"
}
catch
(std::exception& e)
{
std::cerr
}
}
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
// 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
// }
// }
//}
//catch (std::exception& e)
//{
// std::cerr
//}
//return 0;
}
/*
* =====================================================================================
*
* 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
#include
#include
#include
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include
#include
#include
#include
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 :"
/* disp the data size recieved */
std::vector
/* change the recieved mat frame(1*230400) to vector */
cv::Mat data_mat(vectordata,
true
);
/* cout
cout
cout
cout
img= data_mat.reshape(3,240);
/* reshape to 3 channel and 240 rows */
cout
"reshape over"
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
}
thrd.join();
return
0;
}
文章标题:stream opencv mat (webcam)frame throught tcp by boost asio
文章链接:http://soscw.com/essay/21498.html