簡體   English   中英

內存不足錯誤Opencv / c ++

[英]Insufficient memory error Opencv/c++

當我運行此代碼時,它會給出“內存不足錯誤”

#include "stdafx.h"
#include "PixelProcess.h"
#include "ContourProcess.h"

using namespace std;
using namespace cv;

int _tmain(int argc, _TCHAR* argv[])
{

    int width = 480, height = 640, N = 3000, framesToLearn = 500, i, w, h, carCounter, PixelCounter, R, G, B, MyResult, c;
    float circularity;
    String fileName; 
    IplImage* image;
    Mat Output  = Mat::zeros(width, height,CV_8UC1);
    Mat Draw = Mat::zeros(width, height,CV_8UC3);
    vector<vector<Point> > contours;
    vector<Point> hull;
    vector<Point>  keys;
    vector<Vec4i> hierarchy;
    vector<Point> polygonRegion;
    Point centroid;
    GaussianMex *F = new GaussianMex[480*640];

    int Y[5] = {35,35,360,360,35};
    int X[5] = {223,340,590,1,223};

    clock_t begin = 0,end = 0;

    for(i = 1; i <= N ; i++){
        if (i>framesToLearn){begin = clock();}
        carCounter = 0;
        PixelCounter = 0;
        fileName = intToStr(i,"D:/Datasets/Elda2ry1/(");
        image = cvLoadImage(fileName.c_str(),CV_LOAD_IMAGE_COLOR);
        // Update the pixel gaussian model
        for(w = 0; w < width ; w++)
            for(h = 0; h < height ; h++)
            {
                R = uchar((image->imageData+w*image->widthStep)[h*image->nChannels+0]);
                G = uchar((image->imageData+w*image->widthStep)[h*image->nChannels+1]);
                B = uchar((image->imageData+w*image->widthStep)[h*image->nChannels+2]);
                MyResult = F[PixelCounter].Process(R,G,B);
                Output.row(w).col(h) = MyResult*255;
                PixelCounter ++;
            }
            if(i>framesToLearn){ //Extract Cars
                //Output dilation
                dilate(Output,Output,Mat());
                //Matrix to draw on
                cvtColor(Output,Draw,CV_GRAY2RGB);
                polygonRegion = DrawRegionOfInterest(Draw,X,Y,5);
                findContours(Output,contours,hierarchy,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE,Point (0,0));

                for(c = 0; c< contours.size(); c++ )//for each contour
                {
                    //get convex hull and it's centroid
                    convexHull(contours[c],hull,false);
                    centroid = GetCentroidOfConvexHull(hull);
                    //check if the centroid is within the region of interest
                    if (pointPolygonTest(polygonRegion,centroid,false) >=0 )
                        // Check area and Solidity of the convex hull
                        if( contourArea(hull) > 800 && contourArea(contours[c])/contourArea(hull) > 0.7)
                        {
                            circularity = GetCircularity(hull,contourArea(hull),centroid);
                            // Check circularity wrt the hull position
                            if((centroid.y < width/3 && circularity <= 0.42)||(centroid.y > width/3 && (circularity >= 0.33) && (circularity <= 0.4)))
                            {

                                // Crop the car and save it
                                fileName = "D:\\Dataset\\F" + itos(i) + "C" + itos(carCounter) + ".jpg";
                                Rect myRect = cropMyImage(hull,image,fileName);
                            }
                        }
                }
            }
            clrscr();
    }
}

我在這里找到: OpenCV錯誤:內存不足

findcontours函數可能導致此錯誤,但是在我的情況下,我在代碼開頭聲明了vector> 輪廓

錯誤是“內存不足”,因為您沒有足夠的內存...您有內存泄漏。 您正在從磁盤讀取圖像,但從未釋放它們使用的內存。

而且您不應該首先使用IplImage。 這是被Mat(您也使用過)替換的舊格式。 Mat不會發生這種情況,因為它是一個智能指針,並且會釋放自己的內存。

罪魁禍首:

image = cvLoadImage(fileName.c_str(),CV_LOAD_IMAGE_COLOR);

您正在加載3000個IplImages,並且從不釋放它們...

相反,您應該使用c ++ api:

Mat image = imread(fileName,CV_LOAD_IMAGE_COLOR);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM