递归期间代码执行中断
Code execution breaking during recursion
在图像中的单个字母模式中,我编写了一个小函数,它将从任何像素开始并遍历所有连续像素。简而言之,在矩阵中,它将把所有连续像素打开为 true,其余像素将为零。
此函数已正确完成。然而,由于输入模式几乎没有变化,它的行为异常。我发现这条线继续://process left-up diagonally
没有被召唤。
可能是什么原因?
此外,valgrind 没有显示内存损坏。输入 jpg 文件大小最大为 170x30 像素
系统乌班图-16
制作文件:
CFLAGS= -O2 -c -I$(INC_DIR) -fpermissive -std=c++11
CC=g++-5
%.o: %.cpp
$(CC) $(CFLAGS) -c $< -o $@
readpattern: readpattern.o
g++ -IPNG/include -o readpattern readpattern.o libcorona.a -lpng -ljpeg
法典
void do_start_extract_char(char **output, int x, int y, int width, int height) {
//if pixel location croses boundary then return
if (x < 0 || y < 0 || x > width - 1 || y > height - 1) {
return;
}
//if the same pixel has already been visited then just return
if (output[y][x]) {
return;
}
if (isUsefulPixel(x, y)) {
//store it
output[y][x] = 1;
} else {
return;
}
//process left
do_start_extract_char(output, x - 1, y, width, height);
//process down
do_start_extract_char(output, x, y + 1, width, height);
//process right
do_start_extract_char(output, x + 1, y, width, height);
//process up
do_start_extract_char(output, x, y - 1, width, height);
//process left-down diagonally
// /
// /
do_start_extract_char(output, x - 1, y + 1, width, height);
//process left-up diagonally
//
//
do_start_extract_char(output, x - 1, y - 1, width, height);
//process right-down diagonally
//
//
do_start_extract_char(output, x + 1, y + 1, width, height);
//process right-up diagonally
// /
// /
do_start_extract_char(output, x + 1, y - 1, width, height);
return;
}
从大多数像素开始,递归地向左、向下、向右和向上移动足以覆盖整个图像中的每个像素。
左下像素只是当无法通过左、下、右和上到达像素时到达像素的方式。
请注意,朴素递归在这里是一个糟糕的计划。 如果你的图像有几十亿像素,这意味着第一次调用可能最终会有几十亿个递归调用。 这可能会让你的堆栈大吃一惊。
相反,维护自己的像素堆栈以进行访问,并通过在其中排队更多任务来递归。
struct location {
int x,y;
};
bool visited_already(bool const*const* visit_flag, location l) {
return visit_flag[l.y][l.x];
}
struct size {
int x,y;
};
struct rectangle {
location l;
size s;
};
bool in_bounds( location l, rectangle b ) {
if (l.x < b.l.x || l.y < b.l.y) return false;
if (l.x >= b.l.x+b.s.x || l.y >= b.l.y+b.s.y) return false;
return true;
}
bool do_visit(char*const* output, location l) {
if (isUsefulPixel(l.x, l.y)) {
output[l.y][l.x] = 1;
return true;
} else {
return false;
}
}
using todo_list = std::vector<location>;
bool extract_char( char*const* output, bool*const*visited, location where, rectangle bounds) {
if (!in_bounds(where, bounds)) return false;
if (visited_already(visited, where)) return false;
visited[where.y][where.x] = 1;
return do_visit(output, where);
}
void extract_chars(char*const* output, bool*const*visited, todo_list& list, rectangle bounds)
{
while (!list.empty()) {
auto next = list.back();
list.pop_back();
if (extract_char(output, visited, next, bounds))
{
list.push_back( {l.x+1, l.y-1} );
list.push_back( {l.x+1, l.y+0} );
list.push_back( {l.x+1, l.y+1} );
list.push_back( {l.x+0, l.y-1} );
list.push_back( {l.x+0, l.y+0} );
list.push_back( {l.x+0, l.y+1} );
list.push_back( {l.x-1, l.y-1} );
list.push_back( {l.x-1, l.y+0} );
list.push_back( {l.x-1, l.y+1} );
}
}
}
void do_start_extract_char(char *const*output, bool*const*visited, location where, rectangle bounds) {
extract_chars( output, visited, {where}, bounds );
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 递归期间代码执行中断
- 为什么Visual Studio Release生成在未执行的代码行中断
- 中断代码执行
- 中断正在执行MKL或其他第三方函数的线程
- 在std::for_each的执行过程中累积结果,该异常被中断
- 为什么即使在执行atexit或SetConsoleCtrlhandler的ConsoleHandler时线程也会中断
- cuda 执行块是否可以在执行过程中中断
- 如果执行时间过长,我可以中断功能吗?
- 无法中断执行.此进程当前未执行该类型的代码
- 虽然循环中断,但之后的代码永远不会执行
- 一次执行后,嵌套for循环出现意外/未见的中断:高斯消去
- c++ opencv copyTo()函数执行中断
- c++程序完全执行,但在结束时中断
- 每X秒执行一次代码,但在事件发生时立即中断
- 为什么此中断不终止程序,而是继续执行下一个操作?
- 当代码执行进入Dll或Lib空间时中断
- 执行 int 3 中断是否会停止 Linux 上的整个过程,还是仅停止当前线程