OpenCV双重释放或损坏(退出):已中止(核心已转储) [英] OpenCV double free or corruption (out): Aborted (core dumped)

查看:164
本文介绍了OpenCV双重释放或损坏(退出):已中止(核心已转储)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一种算法来查找和填充二值化图像上的区域,对于某些图像,该代码可以按预期工作,但是我不知道为什么,在第四个图像之后,我总是会遇到此错误:

*`./heli'中的错误:两次释放或损坏(出):0x0000000001ccb610 * 已中止(核心已弃用)

这是我的代码:

void fillRegion(Mat src, Mat &dst, Point origin, Vec3b color)
{
    int size = 0;
    list<Point> cadena;
    cadena.push_back(origin);
    while(!cadena.empty())
    {
        Point current = cadena.front();
        cadena.pop_front();
        Point top,bot,right,left;
        top = bot = right = left = current;
        top.y -= 1;
        bot.y += 1;
        right.x += 1;
        left.x -= 1;
        Vec3b cero = Vec3b(0,0,0);

        if(top.y >= 0 && dst.at<Vec3b>(top) == cero && src.at<uchar>(top)!= 0)
        {
            dst.at<Vec3b>(top) = color;
            cadena.push_back(top);
        }
        if(bot.y <= src.rows && dst.at<Vec3b>(bot) == cero && src.at<uchar>(bot)!= 0)
        {
            dst.at<Vec3b>(bot) = color;
            cadena.push_back(bot);
        }
        if(right.x <= src.cols && dst.at<Vec3b>(right) == cero && src.at<uchar>(right)!= 0)
        {
            dst.at<Vec3b>(right) = color;
            cadena.push_back(right);
        }
        if(left.y >= 0 && dst.at<Vec3b>(left) == cero && src.at<uchar>(left)!= 0)
        {
            dst.at<Vec3b>(left) = color;
            cadena.push_back(left);
        }
    }
}

void segment(Mat src, Mat &dst)
{
    for(int x = 0; x < src.cols; x++)
    {
        for(int y = 0; y < src.rows;  y++)
        {
            Point p = Point(x,y); 
            if(src.at<uchar>(p) != 0 && dst.at<Vec3b>(p) == Vec3b(0,0,0) )
            {
                fillRegion(src,dst,p,getRandomColor());
            }
        }    
    }
}
int main(int argc, char *argv[])
{
    namedWindow("Original", WINDOW_NORMAL); 
    namedWindow("Resultado", WINDOW_NORMAL);
    vector<string> archivos = vector<string>();
    getdir("../images",archivos);
    int tam = archivos.size();
    for(uint i = 0; i < tam; i++)
    {
        Mat img = imread(archivos[i],CV_LOAD_IMAGE_GRAYSCALE);
        Mat newImg = Mat(img.rows,img.cols, CV_8UC3, Scalar(0,0,0));;
        threshold(img,img,125,255,THRESH_BINARY);
        imshow("Original", img);
        segment(img,newImg);
        imshow("Resultado", newImg);
        waitKey(0);
    }
}

我调用"Segment"方法,然后为找到的每个区域调用"fillRegion"方法. 我知道错误出在"fillRegion"方法上,因为如果我从"segment"中对其进行注释,该错误就消失了,但是我只是找不到/不知道这是什么错误.

解决方案

对于像我一样遇到此问题的任何人,在代码的最后一行添加exit(0)都可以在ubuntu 16.04上为我修复段错误. >

Iḿ working on a algorithm to find and fill regions on a binarized image,the code work as expected for some images, but I don't know why, after the fourth image I always get this error:

* Error in `./heli': double free or corruption (out): 0x0000000001ccb610 * Aborted (core dumped)

This is my code:

void fillRegion(Mat src, Mat &dst, Point origin, Vec3b color)
{
    int size = 0;
    list<Point> cadena;
    cadena.push_back(origin);
    while(!cadena.empty())
    {
        Point current = cadena.front();
        cadena.pop_front();
        Point top,bot,right,left;
        top = bot = right = left = current;
        top.y -= 1;
        bot.y += 1;
        right.x += 1;
        left.x -= 1;
        Vec3b cero = Vec3b(0,0,0);

        if(top.y >= 0 && dst.at<Vec3b>(top) == cero && src.at<uchar>(top)!= 0)
        {
            dst.at<Vec3b>(top) = color;
            cadena.push_back(top);
        }
        if(bot.y <= src.rows && dst.at<Vec3b>(bot) == cero && src.at<uchar>(bot)!= 0)
        {
            dst.at<Vec3b>(bot) = color;
            cadena.push_back(bot);
        }
        if(right.x <= src.cols && dst.at<Vec3b>(right) == cero && src.at<uchar>(right)!= 0)
        {
            dst.at<Vec3b>(right) = color;
            cadena.push_back(right);
        }
        if(left.y >= 0 && dst.at<Vec3b>(left) == cero && src.at<uchar>(left)!= 0)
        {
            dst.at<Vec3b>(left) = color;
            cadena.push_back(left);
        }
    }
}

void segment(Mat src, Mat &dst)
{
    for(int x = 0; x < src.cols; x++)
    {
        for(int y = 0; y < src.rows;  y++)
        {
            Point p = Point(x,y); 
            if(src.at<uchar>(p) != 0 && dst.at<Vec3b>(p) == Vec3b(0,0,0) )
            {
                fillRegion(src,dst,p,getRandomColor());
            }
        }    
    }
}
int main(int argc, char *argv[])
{
    namedWindow("Original", WINDOW_NORMAL); 
    namedWindow("Resultado", WINDOW_NORMAL);
    vector<string> archivos = vector<string>();
    getdir("../images",archivos);
    int tam = archivos.size();
    for(uint i = 0; i < tam; i++)
    {
        Mat img = imread(archivos[i],CV_LOAD_IMAGE_GRAYSCALE);
        Mat newImg = Mat(img.rows,img.cols, CV_8UC3, Scalar(0,0,0));;
        threshold(img,img,125,255,THRESH_BINARY);
        imshow("Original", img);
        segment(img,newImg);
        imshow("Resultado", newImg);
        waitKey(0);
    }
}

I call the "Segment" method, which then calls the "fillRegion" method for each region found. I know the error is at the "fillRegion" method, because if I comment it from "segment", the error is gone, but I just can't find/don't know what's the error on it.

解决方案

For anyone who comes across this like I did, adding exit(0) as the last line in my code fixed the segfault for me on ubuntu 16.04.

这篇关于OpenCV双重释放或损坏(退出):已中止(核心已转储)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆