跳到正文

算法·理论

泛洪算法

Genshigros约 9 分钟
原文
文章信息与版本

文章信息

发布
2025年3月22日
收录
2026年10月14日
更新
2026年11月15日
篇幅
约 9 分钟

版本

  1. v1当前

泛洪演算法(Flood Fill Algorithm)是一種用於填充連通區域的算法,通常應用於圖像處理、計算機圖形學和遊戲開發等領域。它的核心思想是從一個起始點開始,向周圍擴散,直到填充整個連通區域。以下是泛洪演算法的詳細講解,包括其基本概念、實現方法、應用場景以及代碼示例。


1. 基本概念

定義

泛洪演算法是一種用於填充連通區域的算法,通常用於圖像處理中的顏色填充。它從一個起始點(種子點)開始,檢查其周圍的像素是否符合填充條件,如果符合則填充並繼續擴散,直到整個連通區域被填充。

連通性

  • 4-連通:只考慮上下左右四個方向的像素。
  • 8-連通:考慮上下左右以及四個對角線方向的像素。

填充條件

  • 目標像素的顏色與起始像素的顏色相同。
  • 目標像素的顏色與填充顏色不同。

2. 實現方法

泛洪演算法可以通過遞歸、深度優先搜索(DFS) 或 廣度優先搜索(BFS) 來實現。以下是幾種常見的實現方法:

1. 遞歸實現

  • 從起始點開始,遞歸地填充周圍的像素。
  • 簡單直觀,但可能導致棧溢出(Stack Overflow),尤其是在處理大圖像時。

C++ 實現:

C++
#include <iostream>
#include <vector>
using namespace std;

void floodFill(vector<vector<int>>& image, int x, int y, int oldColor, int newColor) {
    if (x < 0 || x >= image.size() || y < 0 || y >= image[0].size() || image[x][y] != oldColor) {
        return;
    }
    image[x][y] = newColor;
    floodFill(image, x + 1, y, oldColor, newColor); // 下
    floodFill(image, x - 1, y, oldColor, newColor); // 上
    floodFill(image, x, y + 1, oldColor, newColor); // 右
    floodFill(image, x, y - 1, oldColor, newColor); // 左
}

int main() {
    vector<vector<int>> image = {
        {1, 1, 1, 1, 1},
        {1, 1, 1, 0, 0},
        {1, 0, 0, 1, 0},
        {1, 0, 1, 1, 0},
        {1, 1, 1, 1, 1}
    };

    int x = 2, y = 2; // 起始點
    int oldColor = image[x][y];
    int newColor = 2;

    if (oldColor != newColor) {
        floodFill(image, x, y, oldColor, newColor);
    }

    // 輸出填充後的圖像
    for (const auto& row : image) {
        for (int pixel : row) {
            cout << pixel << " ";
        }
        cout << endl;
    }

    return 0;
}

2. 深度優先搜索(DFS)實現

  • 使用棧來模擬遞歸,避免棧溢出問題。
  • 適合處理較大的圖像。

C++ 實現:

C++
#include <iostream>
#include <vector>
#include <stack>
using namespace std;

void floodFillDFS(vector<vector<int>>& image, int x, int y, int oldColor, int newColor) {
    if (oldColor == newColor) return;
    int rows = image.size();
    int cols = image[0].size();
    stack<pair<int, int>> stack;
    stack.push({x, y});

    while (!stack.empty()) {
        auto [cx, cy] = stack.top();
        stack.pop();
        if (cx < 0 || cx >= rows || cy < 0 || cy >= cols || image[cx][cy] != oldColor) {
            continue;
        }
        image[cx][cy] = newColor;
        stack.push({cx + 1, cy}); // 下
        stack.push({cx - 1, cy}); // 上
        stack.push({cx, cy + 1}); // 右
        stack.push({cx, cy - 1}); // 左
    }
}

int main() {
    vector<vector<int>> image = {
        {1, 1, 1, 1, 1},
        {1, 1, 1, 0, 0},
        {1, 0, 0, 1, 0},
        {1, 0, 1, 1, 0},
        {1, 1, 1, 1, 1}
    };

    int x = 2, y = 2; // 起始點
    int oldColor = image[x][y];
    int newColor = 2;

    floodFillDFS(image, x, y, oldColor, newColor);

    // 輸出填充後的圖像
    for (const auto& row : image) {
        for (int pixel : row) {
            cout << pixel << " ";
        }
        cout << endl;
    }

    return 0;
}

3. 廣度優先搜索(BFS)實現

  • 使用隊列來實現,適合處理較大的圖像。
  • 保證填充過程按層次進行。

C++ 實現:

C++
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

void floodFillBFS(vector<vector<int>>& image, int x, int y, int oldColor, int newColor) {
    if (oldColor == newColor) return;
    int rows = image.size();
    int cols = image[0].size();
    queue<pair<int, int>> queue;
    queue.push({x, y});

    while (!queue.empty()) {
        auto [cx, cy] = queue.front();
        queue.pop();
        if (cx < 0 || cx >= rows || cy < 0 || cy >= cols || image[cx][cy] != oldColor) {
            continue;
        }
        image[cx][cy] = newColor;
        queue.push({cx + 1, cy}); // 下
        queue.push({cx - 1, cy}); // 上
        queue.push({cx, cy + 1}); // 右
        queue.push({cx, cy - 1}); // 左
    }
}

int main() {
    vector<vector<int>> image = {
        {1, 1, 1, 1, 1},
        {1, 1, 1, 0, 0},
        {1, 0, 0, 1, 0},
        {1, 0, 1, 1, 0},
        {1, 1, 1, 1, 1}
    };

    int x = 2, y = 2; // 起始點
    int oldColor = image[x][y];
    int newColor = 2;

    floodFillBFS(image, x, y, oldColor, newColor);

    // 輸出填充後的圖像
    for (const auto& row : image) {
        for (int pixel : row) {
            cout << pixel << " ";
        }
        cout << endl;
    }

    return 0;
}

3. 應用場景

  1. 圖像處理:

    • 用於填充圖像中的連通區域,如畫圖軟件中的「油漆桶」工具。
    • 用於圖像分割和邊緣檢測。
  2. 遊戲開發:

    • 用於填充迷宮或地圖中的區域。
    • 用於實現遊戲中的地形生成和路徑規劃。
  3. 計算機圖形學:

    • 用於填充多邊形或圖形中的區域。
    • 用於實現顏色漸變和紋理填充。
  4. 地理信息系統(GIS):

    • 用於填充地圖中的區域,如湖泊、森林等。

4. 總結

泛洪演算法是一種簡單而強大的算法,廣泛應用於圖像處理、遊戲開發和計算機圖形學等領域。通過遞歸、DFS 或 BFS 實現,可以高效地填充連通區域。根據具體需求選擇合適的實現方法,可以進一步提高算法的性能和適用性。

如果有其他問題,歡迎隨時提出!


by DeepSeek

原文