回到首頁

形態學運算(Morphological Operations)

形態學運算主要應用於二值影像,用來分析與處理影像中物件的形狀、結構與分布。相關功能集中於 Process > Binary > ...子選單中。

基本操作

侵蝕與膨脹(Erode & Dilate)

組合操作

其他操作

設定選項(Options)

透過 Process > Binary > Options... 可調整以下參數:

可用下列方式設定:

    // Plugin
    Prefs.blackBackground = true;

    // Macro
    setOption("black background", true);

實作

幾何圖形

執行以下Macro,進行各種二值化操作,觀察這些圖形的變化

二值化影像

// 建立空白影像
newImage("BinaryDemo", "8-bit black", 512, 512, 1);
setForegroundColor(255, 255, 255);

// ---------- 粗圓 + 毛刺 ----------
for (i = 0; i < 3; i++) {
    x = 80 + i * 60;
    y = 80;
    r = 25;
    makeOval(x - r, y - r, r * 2, r * 2);
    fill();
}

// 毛刺圓形
centerX = 250;
centerY = 80;
nPoints = 36;
xPoints = newArray(nPoints);
yPoints = newArray(nPoints);
for (i = 0; i < nPoints; i++) {
    angle = 2 * PI * i / nPoints;
    r = 25 + random() * 10;
    xPoints[i] = centerX + r * cos(angle);
    yPoints[i] = centerY + r * sin(angle);
}
makeSelection("polygon", xPoints, yPoints); fill();

// ---------- 細線 ----------
for (i = 0; i < 4; i++) {
    y = 150 + i * 10;
    makeLine(50, y, 200, y);
    run("Draw", "slice");
}

// ---------- 貼邊形狀 ----------
makeRectangle(0, 300, 60, 60); fill();
makeRectangle(450, 300, 60, 60); fill();
makeRectangle(200, 450, 100, 60); fill();

// ---------- 破碎物件 ----------
setColor(255, 255, 255);
makeRectangle(300, 300, 20, 20); fill();
makeRectangle(321, 300, 20, 20); fill();
makeRectangle(342, 300, 20, 20); fill();
makeRectangle(321, 321, 20, 20); fill();

// ---------- 密集群圓 ----------
x0 = 400;
y0 = 400;
r = 15;
for (i = 0; i < 3; i++) {
    for (j = 0; j < 3; j++) {
        dx = x0 + i * 25;
        dy = y0 + j * 25;
        makeOval(dx - r, dy - r, r * 2, r * 2);
        fill();
    }
}

// 二值化
run("Make Binary");

細胞分佈

執行以下macro,產生三張圖片,模擬不同的粒子分佈,試試看執行Voronoi分隔。

// 參數設定
width = 512;
height = 512;
pointRadius = 3;

// 建立空白影像
newImage("Multi-Pattern Particles", "8-bit black", width, height, 1);
setForegroundColor(255, 255, 255);

// -------------------- 區域1:隨機分佈 --------------------
nRandom = 50;
for (i = 0; i < nRandom; i++) {
    x = random()*160 + 10;    // 區域X: [10,170]
    y = random()*160 + 10;    // 區域Y: [10,170]
    makeOval(x - pointRadius, y - pointRadius, pointRadius*2, pointRadius*2);
    fill();
}

// -------------------- 區域2:密集群聚 --------------------
nClusters = 3;
pointsPerCluster = 20;
for (c = 0; c < nClusters; c++) {
    cx = 200 + c * 30 + random()*10; // 區域X: 約在 200-300
    cy = 60 + random()*60;
    for (i = 0; i < pointsPerCluster; i++) {
        dx = random()*20 - 10;
        dy = random()*20 - 10;
        x = cx + dx;
        y = cy + dy;
        makeOval(x - pointRadius, y - pointRadius, pointRadius*2, pointRadius*2);
        fill();
    }
}

// -------------------- 區域3:規則排列 --------------------
for (i = 0; i < 6; i++) {
    for (j = 0; j < 6; j++) {
        x = 350 + i * 20;
        y = 50 + j * 20;
        makeOval(x - pointRadius, y - pointRadius, pointRadius*2, pointRadius*2);
        fill();
    }
}

// 完成後進行二值化
run("Make Binary");

Top-hat 濾波

Top-hat 濾波是一種基於形態學開運算的背景校正與特徵增強方法,分為: - 白 Top-hat:原圖 - 開運算結果,突顯比結構元素小的亮特徵(常用)。 - 黑 Top-hat:閉運算結果 - 原圖,突顯比結構元素小的暗特徵。

主要應用

ImageJ 操作

結構元素半徑建議大於目標特徵,且小於背景變化尺度。

Macro 範例

run("Blobs (25K)");
run("Invert LUT");
run("Duplicate...", "title=topHat50");
run("Top Hat...", "radius=50");