繁体   English   中英

释放 3 维向量 C++

[英]Deallocation 3 dimensional vector C++

我在 c++ 中解除分配 3 维向量时遇到问题,我收到错误“CTR 检测到应用程序在堆缓冲区结束后写入 memory”。 有人可以告诉我我做错了什么吗? 先感谢您。

分配:

count = new int**[w];

        for (int i = 0; i < w; ++i)
        {
            count[i] = new int*[h];
            for (int j = 0; j < h; ++j)
            {
                count[i][j] = new int[120];
                for (int k = 20; k < 120; ++k)
                {
                    count[i][j][k] = 0;
                }
            }
        }

解除分配:

        for (int i = 0; i < w; ++i)
        {
            for (int j = 0; j < h; ++j)
            {
                delete [] count[i][j];
            }
            delete [] count[i];
        }
        delete [] count;
        count = NULL;

这是我的全部代码:

#include <filesystem>
#include <stdexcept>
#include "pch.h"
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <math.h>
using namespace std;
using namespace cv;

class cerc {
    int raza;
    int a;
    int b;
public:
    cerc()
    {
        a = 0;
        b = 0;
        raza = 0;
    }
    inline cerc(int raza, int a, int b)
    {
        this->a = a;
        this->b = b;
        this->raza = raza;
    }
    inline cerc(cerc& c)
    {
        a = c.a;
        b = c.b;
        raza = c.raza;
    }

    void print()
    {
        std::cout << " (" << (int)this->a << ", " << (int)this->b << ", " << (int)this->raza << ") ";
    }
    inline bool operator==(const cerc& other) const
    {
        if (this->a == other.a && this->b == other.b && this->raza == other.raza)
            return true;
        else return false;
    }
    inline bool egal(int raza, int a, int b) const
    {
        if (this->a == a && this->b == b && this->raza == raza)
            return true;
        else return false;
    }
    inline void copy(cerc &c2)
    {
        this->raza = c2.raza;
        this->a = c2.a;
        this->b = c2.b;
    }
    inline void copy(int raza, int a, int b)
    {
        this->a = a;
        this->raza = raza;
        this->b = b;
    }
    const int getA()
    {
        return a;
    }
    const int getB()
    {
        return b;
    }
    const int getRaza()
    {
        return raza;
    }
};

int min(int a, int b)
{
    if (a < b)
        return a;
    return b;
}

void createVector(int*** count, Mat img, int w, int h)
{
    int a = 0;
    int b = 0;
    int r = 0;

    int wminus = 5 * (w / 6);
    int hminus = 5 * (h / 6);

    int wstart = w / 6;
    int hstart = h / 6;

    int x, y;

    for (x = wstart; x < wminus; x++)
        for (y = hstart; y < hminus; y++)
        {
            if (((Scalar)(img.at<uchar>(Point(x, y)))).val[0] == 255)
            {
                for (a = wstart; a < wminus; a += 1)
                    for (b = hstart; b < hminus; b += 1)
                    {
                        r = (int)sqrt((b - y)*(b - y) + (a - x)*(a - x));

                        if (r >= 20 && r <= 120)
                            count[a][b][r]++;
                    }

            }
        }
}

void cercFinal(cerc &cercMax, int***count, int w, int h)
{
    int wminus = 5 * (w / 6);
    int hminus = 5 * (h / 6);

    int wstart = w / 6;
    int hstart = h / 6;

    int maxNr = count[0][0][0];

    for (int i = wstart; i < wminus; i += 1)
        for (int j = hstart; j < hminus; j += 1)
            for (int k = 20; k < 60; k++)
                if (maxNr < count[i][j][k])
                {
                    maxNr = count[i][j][k];
                    cercMax.copy(k, i, j);
                }

    cout << maxNr;
}
void cercFinalSecund(cerc &SecondCircle, cerc &FirstCircle, int***count, int w, int h)
{
    int wminus = 3 * (w / 4);
    int hminus = 3 * (h / 4);

    int wstart = w / 4;
    int hstart = h / 4;
    int minIrisRay = (int)(FirstCircle.getRaza() * 1.5);
    int maxIrisRay = FirstCircle.getRaza() * 4;
    int maxNr = count[0][0][0];

    for (int i = wstart; i < wminus; i += 1)
        for (int j = hstart; j < hminus; j += 1)
            for (int k = 40; k < 120; k++)
            {
                if (k >= minIrisRay && k <= maxIrisRay)
                {
                    if (maxNr < count[i][j][k] && !(cerc(k, i, j) == FirstCircle) && abs(j - FirstCircle.getB()) < 2 && abs(i - FirstCircle.getA()) < 2)
                    {
                        maxNr = count[i][j][k];
                        SecondCircle.copy(k, i, j);
                    }
                }
            }
}

int main()
{
    Mat imgCanny;
    Mat imgBlur;
    int ***count;
    for (int i = 10; i < 50; i++)
    {
        Mat_<uchar> img = imread("C://Users//Maria//Downloads//CASIA-IrisV2//CASIA-IrisV2//device1//00" + std::to_string(i) + "//00" + std::to_string(i) + "_000.bmp", IMREAD_GRAYSCALE);
        //img.convertTo(img, -1, 4, 0);
        //int t = img.at<uchar>(1, 2).val[0];
        int w = img.size().width;
        int h = img.size().height;

        medianBlur(img, imgBlur, 15);
        //GaussianBlur(img, imgBlur, Size(8, 8), 0);
        Canny(imgBlur, imgCanny, 30, 40);
        //imshow("canny", imgCanny);

        int m = min(w, h);

        count = new int**[w];

        for (int i = 0; i < w; ++i)
        {
            count[i] = new int*[h];
            for (int j = 0; j < h; ++j)
            {
                count[i][j] = new int[120];
                for (int k = 20; k < 120; ++k)
                {
                    count[i][j][k] = 0;
                }
            }
        }
        createVector(count, imgCanny, w, h);

        cerc final(0, 0, 0);
        cerc finalSecund(0, 0, 0);
        cercFinal(final, count, w, h);
        cercFinalSecund(finalSecund, final, count, w, h);

        cout << endl << "cerc=";
        final.print();
        Point p(final.getA(), final.getB());
        Mat imgColor = imread("C://Users//Maria//Downloads//CASIA-IrisV2//CASIA-IrisV2//device1//00" + std::to_string(i) + "//00" + std::to_string(i) + "_000.bmp", IMREAD_COLOR);
        circle(imgColor, p, final.getRaza(), Scalar(255, 0, 0), 4, 8, 0);

        Point ps(finalSecund.getA(), finalSecund.getB());
        cout << endl << "cerc=";
        finalSecund.print();
        circle(imgColor, ps, finalSecund.getRaza(), Scalar(255, 0, 0), 4, 8, 0);

        imshow("iris", imgColor);

        for (int i = 0; i < w; ++i)
        {
            for (int j = 0; j < h; ++j)
            {
                delete [] count[i][j];
            }
            delete [] count[i];
        }
        delete [] count;
        count = NULL;
        waitKey(0);
    }
    return 0;
}

如果我删除行count[a][b][r]++; 重新分配工作正常。 这个说法有什么问题吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM