c++ - OpenCV 线粘在一起
问题描述
环境:Windows10上的OpenCV3.4.12、wxWidgets和C++WinSock。
当我将坐标发送到客户端时,它会使两条线不时粘在一起。(它完全在主机上)
源代码(主机):
void send_painting(int client_id, int index){ //
char recvBuf[2000]; // buffer size
string sendString = encodeLine(index, myApp.mainWindow->panel->shortVec_lines[index]);
// shortVec_lines is a vector stores all coordinates
// encodeLine(int, vector<short>) returns a string contains coordinates of shortVec_lines[index]
for(int i = 0; i< sendString.size(); i++)
recvBuf[i] = sendString[i];
cout<<sClient_painting.size() <<endl <<endl;
send(sClient_painting[0], recvBuf, 2000, 0);
cout << recvBuf;
}
void wxImagePanel::updateFrame(wxTimerEvent &evt) {
Mat temp;
resize (frame, temp, {1530, 860});
for (auto i: shortVec_lines) // redraw all lines before wxImage updates the screen
for (int k = 2; k < i.size(); k += 2)
line(temp, {i[k - 2], i[k - 1]}, {i[k], i[k + 1]}, {255, 0, 255});
wxImage tmp = wxImage(1530, 860, temp.data, TRUE);
image = wxBitmap(tmp);
wxClientDC dc(this);
render(dc);
}
源代码(客户端):
void recv_painting(){
Sleep(2000); // sleep for 2sec to ensure the connection is stable
fd_set readSet;
FD_ZERO(&readSet);
FD_SET(sClient_painting, &readSet);
int& refIndex = myApp.mainWindow->panel->index;
vector<vector<int>>& refVec = myApp.mainWindow->panel->shortVec_lines;
while(true){
fd_set tmpSet;
tmpSet = readSet;
int ret = select(0, &tmpSet, NULL, NULL, NULL);
if (ret == SOCKET_ERROR) {
cout << "error\n";
continue;
}
char buf[2000] = {0};
SOCKET s = tmpSet.fd_array[0];
ret = recv(s, buf, 2000, 0);
string ss;
for(int i = 0; buf[i]!=0; i++)
ss.push_back(buf[i]);
vector<int>tempVec = decodeLine(ss); // vector<short> decodeLine(string) returns the original vector from host(the data received here is correct)
int index = tempVec[0];
while(index >= refVec.size())
refVec.emplace_back();
try{
refVec.at(index) = vector<int>(tempVec.begin()+1, tempVec.end());
} catch(out_of_range& e) {
cout << "OOR\n";
} catch(exception& e){
cout << "other Exception\n";
}
refIndex++;
}
}
void wxImagePanel::updateFrame(wxTimerEvent &evt) {
Mat temp;
random_device rd;
resize (frame, temp, {1530, 860});
for (auto i: shortVec_lines) {
for (int k = 2; k < i.size(); k += 2) {
line(temp, {i[k - 2], i[k - 1]}, {i[k], i[k + 1]},
{(double) (rd() % 256), (double) (rd() % 256), (double) (rd() % 256)}); // rand color to see each coordinate
}
}
if (!fn)
rectangle(temp, start, end, {155, 0, 255});
wxImage tmp = wxImage(1530, 860, temp.data, TRUE);
image = wxBitmap(tmp);
wxClientDC dc(this);
render(dc);
}
如下图所示,前两行进展顺利,而第三行则不然。我试图打印所有坐标,它似乎没问题。
主机截图:
客户端截图:
解决方案
推荐阅读
- javascript - 如何简单地测试 Web 应用程序的数据是否会在被浏览器删除之前警告用户?
- python - run_until_complete 和 ensure_future 之间的 Python 关系
- apache - 我从 localhost 移动到 webhost 的 php 文件无法正常工作
- types - F# 类型“int list”与类型“int”不匹配
- git-commit - 为什么在 git 中不推送就提交?
- ios - 更新元组数组中的每个键
- python - 两个 32 字节数字的模
- python - 在 api 端点内发出 http 请求
- php - 如何在附件页面上调用图像标题(Wordpress)
- database - 远程mysql可以操作写命令吗?