我的opencv不起作用

时间:2017-04-22 11:46:36

标签: c++ opencv

我使用Opencv在ptz相机上识别数字。

但我的计划有很大问题。

“./main.out”出错:双重免费或损坏(!prev):

我认为这是在泄漏记忆。所以我试着'清除()'来释放内存。但它不起作用。

如何解决?

#include "opencv2/opencv.hpp"  

#include <iostream>  

#include <unistd.h>
#include <time.h>



using namespace cv;

using namespace std;
Mat3b canvas;
string buttonText("Click me!");
Mat frame1;

Mat frame2;

Mat frame3;

Rect rect, temp_rect;





double ratio, delta_x, delta_y, gradient;

int count, friend_count = 0, refinery_count = 0;


void testfunc()
{
    vector<Vec4i> hierarchy;
    vector<vector<Point> > contours;
    findContours(frame3, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point());

    vector<vector<Point> > contours_poly(contours.size());

    vector<Rect> boundRect(contours.size());

    vector<Rect> boundRect2(contours.size());



    for (int i = 0; i < contours.size(); i++) {

        approxPolyDP(Mat(contours[i]), contours_poly[i], 1, true);

        boundRect[i] = boundingRect(Mat(contours_poly[i]));

    }



    Mat drawing = Mat::zeros(frame3.size(), CV_8UC3);



    for (int i = 0; i < contours.size(); i++)

    {

        ratio = (double)boundRect[i].height / boundRect[i].width;



        if ((ratio <= 2.5) && (ratio >= 0.5) && (boundRect[i].area() <= 700) && (boundRect[i].area() >= 100))

        {

            drawContours(drawing, contours, i, Scalar(0, 255, 255), 1, 8, hierarchy, 0, Point());

            rectangle(drawing, boundRect[i].tl(), boundRect[i].br(), Scalar(255, 0, 0), 1, 8, 0);



            boundRect2[refinery_count] = boundRect[i];

            refinery_count++;

        }

    }



    boundRect2.resize(refinery_count);



    imshow("camera4", drawing);

    contours_poly.clear();
    boundRect.clear();
    boundRect2.clear();
    contours.clear();
    hierarchy.clear();

    return;
}


int main(int argc, char** argv)

{

    VideoCapture cap1(0);



    if (!cap1.isOpened())

    {

        printf("ERROR. \n");

        return -1;

    }

// ************* This is notebook camera **********



// ************* This is PTZ camera **********

/*VideoCapture cap2;

string vStreamArs = "rtsp://root:pass@192.168.10.235/ufirststream";
Mat video;


if (!cap2.open(vStreamArs))

{

cout << "[-] ERROR CODE 0 : Not connect camera!!" << endl;

return -1;

}

else

{

cout << "[+] Camera is connected!!" << endl;

}*/



// ************* This is PTZ camera **********



// ************* This is notebook camera **********




    int select, plate_width;


    while (1)
    {
        clock_t start = clock();
        while (1)
        {
            if (!cap1.read(frame1))
            {
                cout << "[-] ERROR CODE 2 : No camera" << endl;
                break;
            }
            imshow("output", frame1);

            if ((clock() - start) / CLOCKS_PER_SEC > 10)
            {
                cout << "[+]Find" << endl;
                break;
            }
            waitKey(1);
        }

        cvtColor(frame1, frame2, CV_BGR2GRAY);
        Canny(frame2, frame3, 100, 300, 3);


        imshow("camera3", frame3);

        testfunc();
    }


    getchar();

    return 0;

  }

1 个答案:

答案 0 :(得分:0)

尝试在使用它们之后对Mat图像使用release()方法。这将强制释放内存。请试着告诉我。