【问题标题】:C++ Memory HoughLinesPC++ 内存 HoughLinesP
【发布时间】:2016-09-25 14:29:09
【问题描述】:

我使用 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;

}

【问题讨论】:

  • 卡住是什么意思?程序崩溃?您是否尝试过调试器来查看问题发生在哪里?
  • 谢谢,我在检查哪一行时发现了问题所在。在一部分中它没有找到任何垂直线并且它是循环( i=0,i

标签: c++ opencv visual-c++ memory-management memory-leaks


【解决方案1】:

问题大概出在这里:

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))
{
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-03
    • 2016-06-07
    • 2021-08-04
    • 1970-01-01
    • 2014-09-15
    • 2019-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多