首页 > 解决方案 > 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);
}

如下图所示,前两行进展顺利,而第三行则不然。我试图打印所有坐标,它似乎没问题。

主机截图:

主机截图

客户端截图:

在此处输入图像描述

标签: c++opencvwxwidgets

解决方案


推荐阅读