У меня есть простая функция заполнения, за исключением того, что она вылетает из-за слишком большого количества освобождений во время работы.
#include <vector>
cv::Mat fillLayer(cv::Mat filledEdge, int y, int x, int oldColor, float newColor){
cv::Size shape = filledEdge.size();
int h = shape.height;
int w = shape.width;
std::vector<int> theStackx = {x};
std::vector<int> theStacky = {y};
while (theStacky.size() > 0){
y = theStacky.back();
x = theStackx.back();
theStacky.pop_back();
theStackx.pop_back();
if (x == w){
continue;
}
if (x == -1){
continue;
}
if (y == -1){
continue;
}
if (y == h){
continue;
}
if (filledEdge.at<float>(y, x) != oldColor){
continue;
}
filledEdge.at<float>(y, x) = newColor;
//up
theStacky.push_back(y + 1);
theStackx.push_back(x);
//down
theStacky.push_back(y - 1);
theStackx.push_back(x);
//right
theStacky.push_back(y);
theStackx.push_back(x + 1);
//left
theStacky.push_back(y);
theStackx.push_back(x - 1);
}
return filledEdge;
}
Функция, которая проходит через заливку, называется fillSurface
. который проходит через все пиксели в мате и заполняет их другим цветом для каждой заливки
fillSurface(cv::Mat filledEdge, int oldColor) {
std::vector<float> layers; //list all the different colors in mat
cv::Size shape = filledEdge.size();
int h = shape.height;
int w = shape.width;
float newColor;
// run through all the pixels in Mat
for(int y = 0; y!= h; y++){
for(int x = 0; x!= w; x++){
// only run floodfill if current pixel is oldColor
if (filledEdge.at<float>(y, x) == oldColor){
//newColor is random float to fill in to floodfill
newColor = static_cast <float> ((rand()) / (static_cast <float> (RAND_MAX/253)) + 1);
// add newColor to list of layers
layers.push_back(newColor);
//run flood fill replacing old color with new color
filledEdge = fillLayer(filledEdge, y, x, oldColor, newColor);
}
}
}
}
Это ошибка, которую я получаю:
Incorrect checksum for freed object 0x7fea0d89dc00: probably modified after being freed.
Отладка, которую я сделал, заключается в том, чтобы установить точку останова на malloc_error_break(), чтобы увидеть, где именно я получаю перерыв. Это приводит к функции заполнения заливки.
Мне интересно, можно ли это как-то исправить. Если нет, то что было бы лучшей альтернативой?
main()
и по крайней мере одна функция#include
. Пожалуйста, отредактируйте свой код, чтобы он минимально воспроизводимый пример вашей проблемы (включая все необходимые входные данные, но желательно без них), то мы можем попытаться воспроизвести и решить ее. Вы также должны прочитать Как спросить. - person Toby Speight   schedule 15.11.2018