C++记忆霍夫莱恩斯·

C++ Memory HoughLinesP

本文关键字:记忆 C++      更新时间:2023-10-16

我用HoughLinesP做了一个程序,现在它只是在第17帧时卡住了,它正在运行一些东西但没有结果。使用另一个视频,它会卡在第 13 帧(即对象进入视频的那一刻)。我认为这是一个内存问题,但是我已经清理了我在论坛上发现的所有矢量。我也发现了类似的东西,解决方案是:

将您的应用程序链接到与 OpenCV 库版本相同的 CRT

我不明白该怎么做,我正在使用MSVC 2013,更新4。有我的代码:

 struct myclass {
   bool operator() (Vec4i l1, Vec4i l2) { return (l1[0] < l2[0]); }
 } myobjectv;
struct myclass1 {
    bool operator() (Vec4i l1, Vec4i l2) { return (l1[1] < l2[1]); }
} myobjecth;
int main(int argc, char** argv)
{
vector<Rect*> components(250, (Rect *)NULL);
VideoCapture video1;
int vektor, ukupno_komponenti;
int width, height, frames1, fps1;
video1.open(argv[1]);
fps1 = video1.get(CV_CAP_PROP_FPS);
width = video1.get(CAP_PROP_FRAME_WIDTH);
height = video1.get(CAP_PROP_FRAME_HEIGHT);
frames1 = video1.get(CAP_PROP_FRAME_COUNT);
int i = 0;
int j = 0;
Mat src, dst, cdst, krug;
Mat frameTime1(height, width, CV_8UC3, Scalar(0, 0, 0));
int fvd = 0;
while (1)
{
    fvd++;
    cout << fvd;
    video1 >> src;
    bool bSuccess = video1.read(src);
    if (!bSuccess) //if not success, break loop
    {
        cout << "ERROR: Cannot read a frame from video file" << endl;
        break;
    }
    Mat roi_w1;
    roi_w1 = src(Rect(150, 50, 320, 320));
    GaussianBlur(roi_w1, roi_w1, Size(11, 11), 0);
    Canny(roi_w1, dst, 50, 200, 3);
    cvtColor(dst, cdst, CV_GRAY2BGR);
    vector<Vec4i> lines;
    vector<Vec4i> h_lines;
    vector<Vec4i> v_lines;
    HoughLinesP(dst, lines, 1, CV_PI / 220, 50, 150, 30);
    for (size_t i = 0; i < lines.size(); i++)
    {
        Vec4i l = lines[i];
        double Angle = atan2(l[3] - l[1], l[2] - l[0]) * 180.0 / CV_PI;
        if (Angle < 0) Angle = Angle + 360;
        //vertikalne
        if ((abs(Angle) > 88) | (abs(Angle) == 90)){
            v_lines.push_back(lines[i]);
        }
        //horizontalne
        if ((abs(Angle) == 0) | (abs(Angle) < 2)){
            h_lines.push_back(lines[i]);
        }
    }
    int broj[10] = { 0 };
    sort(v_lines.begin(), v_lines.end(), myobjectv);
    vector<Vec4i> vv_lines;
    for (size_t i = 0; i < v_lines.size() - 1; i++)
    {
        for (size_t j = i + 1; j < v_lines.size(); j++)
        {
            Vec4i l1 = v_lines[i];
            Vec4i l2 = v_lines[j];
            if ((abs(l1[2] - l2[2]) < 20)){
                if ((broj[i] == 0) && (broj[j] == 0))
                {
                    vv_lines.push_back(l1);
                    broj[i]++;
                    broj[j]++;
                }
            }
            else{
                if ((broj[i] == 0)){ vv_lines.push_back(l1); }
                if ((broj[j] == 0)){ vv_lines.push_back(l2); }
            }
        }
    }
    for (size_t i = 0; i < vv_lines.size(); i++)
    {
        Vec4i lcr = vv_lines[i];
        line(cdst, Point(lcr[0], lcr[1]), Point(lcr[2], lcr[3]), Scalar(0, 255, 0), 3, CV_AA);
    }
    int brojb[10] = { 0 };
    sort(h_lines.begin(), h_lines.end(), myobjecth);
    vector<Vec4i> hh_lines;
    for (size_t i = 0; i < h_lines.size(); i++)
    {
        for (size_t j = i; j < h_lines.size() - 1; j++)
        {
            Vec4i l1 = h_lines[i];
            Vec4i l2 = h_lines[j];
            if ((abs(l1[1] - l2[1]) < 20)){
                if ((brojb[i] == 0) && (brojb[j] == 0))
                {
                    hh_lines.push_back(l1);
                    brojb[i]++;
                    brojb[j]++;
                }
            }
            else{
                if ((brojb[i] == 0)){ hh_lines.push_back(l1); }
                if ((brojb[j] == 0)){ hh_lines.push_back(l2); }
            }
        }
    }
    for (size_t i = 0; i < hh_lines.size(); i++)
    {
        Vec4i lcr = hh_lines[i];
        line(cdst, Point(lcr[0], lcr[1]), Point(lcr[2], lcr[3]), Scalar(0, 255, 0), 3, CV_AA);
    }
    vector<Point> grid;
    Point P;
    int s = 0;
    for (size_t i = 0; i < hh_lines.size(); i++)
    {
        for (size_t j = 0; j < vv_lines.size(); j++)
        {
            Vec4i lb1 = hh_lines[i];
            Vec4i lb2 = vv_lines[j];
            float p1startx = lb1[0];
            float p1starty = lb1[1];
            float p1endx = lb1[2];
            float p1endy = lb1[3];
            float p2startx = lb2[0];
            float p2starty = lb2[1];
            float p2endx = lb2[2];
            float p2endy = lb2[3];
            if ((p2startx>p1startx)&(p2startx < p1endx)){
                P.x = p2startx;
                P.y = p1starty;
                grid.push_back(P);
                circle(cdst, P, 3, Scalar(0, 0, 255), -1, 8, 0);
                s++;
            }
        }
    }
    vector<Vec4i>().swap(lines);
    vector<Vec4i>().swap(h_lines);
    vector<Vec4i>().swap(hh_lines);
    vector<Vec4i>().swap(v_lines);
    vector<Vec4i>().swap(vv_lines);
}
return 0;

}

问题可能在这里:

if ((abs(Angle) > 88) | (abs(Angle) == 90))
{
    v_lines.push_back(lines[i]);
}
//horizontalne
if ((abs(Angle) == 0) | (abs(Angle) < 2))
{
    h_lines.push_back(lines[i]);
}

您需要使用逻辑 OR ( || ) 运算符,而不是错误使用的按位 OR ( |)。

if ((abs(Angle) > 88) || (abs(Angle) == 90))
{
    ...
}
if ((abs(Angle) == 0) || (abs(Angle) < 2))
{
    ...
}