关于opencv视频传输卡顿现象

在局域网内通过socket和opencv进行视频传输,卡顿现象特别也重,各位大佬能不能帮忙看一下

下面是server端的代码

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#include
#include
#include
#include
#pragma comment (lib,"ws2_32.lib")
using namespace std;
using namespace cv;
enum { PORT = 16789 };

int main()
{
WSADATA wd;
WSAStartup(0x01, &wd);
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == sock)
{
cout << "创建套接字错误" << WSAGetLastError() << endl;
return -1;
}
sockaddr_in sa = { AF_INET };
sa.sin_port = htons(PORT);
if (SOCKET_ERROR == bind(sock, (const sockaddr*)&sa, sizeof(sa)))
{
cout << "绑定失败" << WSAGetLastError() << endl;
return -1;
}
if (SOCKET_ERROR == listen(sock, 5))
{
cout << "侦听失败" << WSAGetLastError() << endl;
return -1;
}
SOCKET socka = -1;
int nLen = sizeof(sa);
socka = accept(sock, (sockaddr*)&sa, &nLen);
char s[1000000];
Mat src_image = imread("2.jpg");
imshow("看看", src_image);
int rowNumber = src_image.rows;
int colNumber = src_image.cols*src_image.channels();
while (1)
{
int n = recv(socka, s, sizeof(s), 0);
if (n <= 0)
break;
//s[n] = 0;
for (int i = 0; i < rowNumber; i++)
{
uchar *data = src_image.ptr(i);
for (int j = 0; j < colNumber; j++)
{
// data[j] = s[i*colNumber + j];
data[j] = s[colNumber*i + j];
}
}
imshow("收到的图像", src_image);
waitKey(1);
}
closesocket(sock);
return 0;
}

下面是客户端的代码

#define _WINSOCK_DEPRECATED_NO_WARNINGS
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
#ifdef _WIN32
#include
#pragma comment (lib,"ws2_32.lib")
#else
#include /* See NOTES */
#include
#include
#include
#include
#include
typedef int SOCKET;
#define INVALID_SOCKET (SOCKET)(~0)
#define SOCKET_ERROR (-1)
#endif

using namespace cv;
using namespace std;

enum { PORT = 16789 };

int main()
{
#ifdef _WIN32
WSADATA wd;
WSAStartup(0x01, &wd);
#endif
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == sock)
{
cout << "Create socket Error" << /*WSAGetLastError() <<*/ endl;
return -1;
}
sockaddr_in sa = { AF_INET };
if (SOCKET_ERROR == bind(sock, (const sockaddr*)&sa, sizeof(sa)))
{
cout << "bind Error" /*<< WSAGetLastError()*/ << endl;
return -1;
}
sa.sin_addr.s_addr = inet_addr("192.168.1.100");
sa.sin_port = htons(PORT);
connect(sock, (const sockaddr*)&sa, sizeof(sa));
VideoCapture capture(0);
Mat src_image;
while (1)
{
capture >> src_image;
// imwrite("2.jpg", Frame);
// Mat src_image = imread("2.jpg");
imshow("这也是图片", src_image);
int rowNumber = src_image.rows;
int colNumber = src_image.cols*src_image.channels();
char SendData[1000000] = "";
for (int i = 0; i < rowNumber; i++)
{
uchar *data = src_image.ptr(i);
for (int j = 0; j < colNumber; j++)
{
SendData[colNumber*i + j] = data[j];
}
}
send(sock, SendData, sizeof(SendData), 0);
waitKey(30);
}
closesocket(sock);
return 0;
}

大概有哪些原因会导致视频传输特别卡顿啊,望大佬指点

1个回答

问题已经解决,将视频进行压缩后 在进行传输减少了带宽

weixin_43530100
weixin_43530100 方便留个联系方式交流一下吗
10 个月之前 回复
weixin_43530100
weixin_43530100 麻烦问一下,为什么转换之后输出的只是一幅图片,不是视频呢,你是怎麽解决的
10 个月之前 回复
qq_37406130
古天九等一缕 回复ppyyqqq: 不要用像素传输,可以压缩后进行传输。https://blog.csdn.net/qq_37406130/article/details/78820176#comments
大约一年之前 回复
ppyyqqq
ppyyqqq 这个问题怎么解决的
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐