区域填充之扫描线种子法的Python实现

前言

最近在帮学长做语义地图导航与规划,遇到了这样的问题。
这里写图片描述
将学长分割好的地图里的每一个白色区块标识出来,以便于后面语义地图的导航。
最开始我想到了种子法,但是由于使用了递归,当地图规模增加时程序就跑不起来了,(Python默认最大递归次数为1000次,即使用sys.setrecursionlimit()强行设置成很大的数最多也只能递归20000多次便会崩溃,而MATLAB的递归更是慢的抠脚)。
幸好前几天浙大的同学给我发了他们计算机图形学的课件,当时看到了扫描线种子法,当时粗略看了一遍一些细节还没想明白,于是自己重新实现了一遍,结果速度也提高了,程序也不崩了,一口气上五楼不费劲儿。

算法思路

基本思路如下:(摘自课件)

将种子象素点压入堆栈
while 堆栈非空 do
begin
从堆栈中弹出一个种子象素
沿着扫描线对种子象素的左右象素进行填充,直至遇到边界
象素为止
标志区间内最左和最右象素为x left 和x right
if在x left ≤x≤x right 中检查与当前扫描线相邻的上下两条扫描线全
为边界象素或全为已填充过的象素 then goto 2
在x left ≤x≤x right 中标记每一个既不包含边界象素又不包含已 填
充过的象素的区间
将每一区间的最左、最右象素作为种子象素压入堆栈
end
end of algorithm

下面放代码。

#coding:utf8
import cv2
import numpy as np

frame = cv2.cvtColor(cv2.imread('fr_seg_open22_result.pgm'), cv2.COLOR_BGR2GRAY)
ret,frame = cv2.threshold(frame, 127,255,cv2.THRESH_BINARY)
contour_index = 1

def scan_line_seed(i, j):
    stack = []
    return_value = 0
    if frame[i, j] == 255:
        stack.append((i,j));
        while len(stack) != 0:
            seed = stack.pop()
            x,y=seed
            while frame[x, y] == 255:
                frame[x, y] = contour_index * 10;
                x += 1
            x_right = x-1
            x, y = seed
            x -= 1
            while frame[x, y] == 255:
                frame[x, y] = contour_index * 10;
                x -= 1
            x_left = x + 1
            #如果左右端点为空则加入种子点
            if frame[x_left, y - 1] == 255:
                stack.append((x_left, y-1))
            if frame[x_left, y + 1] == 255:
                stack.append((x_left, y+1))
            if frame[x_right, y - 1] == 255:
                stack.append((x_right, y-1))
            if frame[x_right, y + 1] == 255:
                stack.append((x_right, y+1))
            for x in range(x_left,x_right+1):
                #上左
                if frame[x,y-1] == 255 and frame[x-1, y-1] != 255:
                    stack.append((x,y-1))
                #下左
                if frame[x,y+1] == 255 and frame[x-1, y+1] != 255:
                    stack.append((x,y+1))
                #上右
                if frame[x,y-1] != 255 and frame[x-1, y-1] == 255:
                    stack.append((x-1,y-1))
                #下右
                if frame[x,y+1] != 255 and frame[x-1, y+1] == 255:
                    stack.append((x-1,y+1))

        return_value = 1
    return return_value

for x in range(frame.shape[0]):
    for y in range(frame.shape[1]):
        if scan_line_seed(x, y) == 1:
            contour_index = contour_index + 1
            print contour_index
cv2.namedWindow('frame',cv2.WINDOW_NORMAL)
cv2.imshow('frame', frame)
cv2.imwrite('numbered_intel.pgm',frame)
cv2.waitKey(0);

实现效果

运行效果是酱婶的
这里写图片描述
这个故事教育我们,好好看课件是多么滴重要~

  • 2
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
扫描线种子填充算法是一种用于图形填充算法,其基本思想是从种子点开始,沿着扫描线进行像素填充,直到遇到边界为止。以下是C++实现算法的基本步骤: 1. 定义一个像素点结构体,用于存储像素的坐标信息和颜色信息。 ```cpp struct Pixel { int x; int y; int color; }; ``` 2. 定义一个扫描线结构体,用于存储扫描线的起始和终止位置。 ```cpp struct ScanLine { int y; int left; int right; }; ``` 3. 定义一个函数用于判断像素点是否在边界内部。 ```cpp bool insideBoundary(int x, int y) { // 判断像素点是否在边界内部 // 如果在内部返回 true,否则返回 false } ``` 4. 定义一个函数用于判断像素点是否为种子点。 ```cpp bool isSeedPoint(int x, int y) { // 判断像素点是否为种子点 // 如果是返回 true,否则返回 false } ``` 5. 定义一个函数用于填充扫描线上的像素点。 ```cpp void fillScanLine(int y, int left, int right, int color) { for (int x = left; x <= right; x++) { // 填充像素点 } } ``` 6. 定义一个函数用于进行扫描线种子填充。 ```cpp void scanLineSeedFill(int x, int y, int color) { if (!insideBoundary(x, y) || isSeedPoint(x, y)) { return; } // 初始化扫描线 std::vector<ScanLine> scanLines; scanLines.push_back({y, x, x}); // 循环处理扫描线,直到所有像素点都填充完成 while (!scanLines.empty()) { // 取出当前扫描线 ScanLine scanLine = scanLines.back(); scanLines.pop_back(); // 填充当前扫描线上的像素点 fillScanLine(scanLine.y, scanLine.left, scanLine.right, color); // 处理下一扫描线 for (int i = scanLine.left; i <= scanLine.right; i++) { // 处理当前像素点的上方像素点 if (insideBoundary(i, scanLine.y - 1) && !isSeedPoint(i, scanLine.y - 1)) { int j = i; while (insideBoundary(j, scanLine.y - 1) && !isSeedPoint(j, scanLine.y - 1)) { j--; } scanLines.push_back({scanLine.y - 1, j + 1, i - 1}); } // 处理当前像素点的下方像素点 if (insideBoundary(i, scanLine.y + 1) && !isSeedPoint(i, scanLine.y + 1)) { int j = i; while (insideBoundary(j, scanLine.y + 1) && !isSeedPoint(j, scanLine.y + 1)) { j--; } scanLines.push_back({scanLine.y + 1, j + 1, i - 1}); } } } } ``` 以上是C++实现扫描线种子填充算法的基本步骤,可以根据具体需求进行调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值