【发布时间】:2014-11-04 15:46:10
【问题描述】:
我正在研究一种在二值化图像上查找和填充区域的算法,代码对某些图像按预期工作,但我不知道为什么,在第四张图像之后我总是得到这个错误:
* `./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”方法中,因为如果我从“段”注释它,错误就消失了,但我就是找不到/不知道它的错误是什么。
【问题讨论】: