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