tcp+opencv 视频传输+raspberry

来源:互联网 发布:bugclose 源码下载 编辑:程序博客网 时间:2024/06/10 03:21

OpenCV能够为我们带来便捷的图像处理接口,但是其处理速度在一块树莓派上肯定是不尽如人意的。尤其当我们想要使用复杂的算法时,只能把算法托到服务器上才有可能。这里介绍了一种方法,实现树莓派传输Mat至电脑。

准备工作

1、配置好树莓派上的opencv.
2、配置好电脑上的OpenCV.

思路

Socket实现方法较为简单,但在此处需要注意的是:
树莓派上的,是Linux系统;而我电脑上是windows系统,需要注意这一点来进行编程。

代码

树莓派作为客户端,发送Mat.

#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <string.h>#include <sys/types.h>#include <sys/socket.h>#include <netinet/in.h>#include <netdb.h> #include <opencv2/opencv.hpp>using namespace cv;using namespace std;void error(const char *msg){    perror(msg);    exit(0);}int main(){    int sockfd, portno, n;    struct sockaddr_in serv_addr;    struct hostent *server;    portno = atoi("8888");    sockfd = socket(AF_INET, SOCK_STREAM, 0);    if (sockfd < 0)        error("ERROR opening socket");    server = gethostbyname("10.138.216.104");//这里填IP地址    if (server == NULL) {        fprintf(stderr, "ERROR, no such host\n");        exit(0);    }    bzero((char *)&serv_addr, sizeof(serv_addr));    serv_addr.sin_family = AF_INET;    bcopy((char *)server->h_addr,        (char *)&serv_addr.sin_addr.s_addr,        server->h_length);    serv_addr.sin_port = htons(portno);    if (connect(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)        error("ERROR connecting");    VideoCapture cap(0);    Mat frame;    while (true)    {        cap >> frame;        cvtColor(frame, frame, CV_BGR2GRAY);        // Send data here        int bytes = send(sockfd, frame.data, frame.total()*frame.elemSize(), 0));    }    waitKey(0);    return 0;}
  • 1
  • 2
  • 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
  • 5

服务器接收并显示

    #include <winsock2.h>      #include <opencv.hpp>    using namespace cv;    using namespace std;    #pragma comment(lib,"ws2_32.lib")      #define imgSize 307200 //这里需要算一下,因为Visual Studio不能支持用变量初始化数组    //imgSize=frame.total()*frame.elemSize();    //即:如果为灰度图:图像的宽*图像的高 这里是640*480    int main(int argc, char* argv[])    {    //初始化WSA      WORD sockVersion = MAKEWORD(2, 2);    WSADATA wsaData;    if (WSAStartup(sockVersion, &wsaData) != 0)    {        return 0;    }    //创建套接字      SOCKET slisten = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);    if (slisten == INVALID_SOCKET)    {        printf("socket error !");        return 0;    }    //绑定IP和端口      sockaddr_in sin;    sin.sin_family = AF_INET;    sin.sin_port = htons(8888);    sin.sin_addr.S_un.S_addr = INADDR_ANY;    if (bind(slisten, (LPSOCKADDR)&sin, sizeof(sin)) == SOCKET_ERROR)    {        printf("bind error !");    }    //开始监听      if (listen(slisten, 5) == SOCKET_ERROR)    {        printf("listen error !");        return 0;    }    SOCKET sClient;    sockaddr_in remoteAddr;    int nAddrlen = sizeof(remoteAddr);    cout << "Wait.." << endl;    do    {        sClient = accept(slisten, (SOCKADDR *)&remoteAddr, &nAddrlen);    } while (sClient == INVALID_SOCKET);    cout<<"Received Information come from"<< inet_ntoa(remoteAddr.sin_addr)<<endl;    int bytes = 0;    while (1)    {        char socketData[imgSize];        for (int i = 0; i < imgSize; i += bytes) {            if ((bytes = recv(sClient, socketData + i, imgSize - i, 0)) == -1)            {                cout << "!Fault" << endl;                exit(-1);            }        }        // change the last loop to below statement        Mat img(Size(640, 480), CV_8UC1, socketData);//根据摄像头大小自行修改        imshow("Face_Socket", img);        waitKey(1);    }    closesocket(slisten);    WSACleanup();    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

先运行服务器,后运行客户端(其实无所谓),我们就可以在服务器上看到树莓派通过摄像头获取到的帧,那么就可以对齐进行很多操作了。

原创粉丝点击