在MATLAB中,你可以使用以下步驟來實現二值圖像的孔洞填充:
imread
函數讀取圖像并轉換為二值圖像。binaryImage = imread('image.png');
binaryImage = im2bw(binaryImage);
imfill
函數提取孔洞。holeImage = imfill(binaryImage, 'holes');
bwperim
函數獲取孔洞的邊界輪廓。holePerimeter = bwperim(holeImage);
imfill
函數填充孔洞。filledImage = imfill(holePerimeter, 'holes');
imshow
函數顯示填充后的圖像。imshow(filledImage);
完整的代碼如下所示:
binaryImage = imread('image.png');
binaryImage = im2bw(binaryImage);
holeImage = imfill(binaryImage, 'holes');
holePerimeter = bwperim(holeImage);
filledImage = imfill(holePerimeter, 'holes');
imshow(filledImage);
請確保將image.png
替換為你實際的圖像文件名。