Matlab提供了現成的函數,可以快速實現二值圖像的閉合區域標記,這裡介紹三種常見情形:
方法1:bwlabel
im = imread('label.png');
im_bin = im2bw(im, 0.5);
lab = bwlabel(im_bin);
imagesc(wcodemat(lab));
方法2:bwboundaries
im = imread('label.png');
im_bin = im2bw(im, 0.5);
[B,L] = bwboundaries(im_bin,'holes');
imshow(label2rgb(L, @jet, [.5 .5 .5]));
方法1:imfill
+ bwlabel
im = imread('label2.png');
im_bin = im2bw(im, 0.5);
fill = imfill(im_bin, 'holes');
lab = bwlabel(fill);
imagesc(wcodemat(lab));
方法2:bwboundaries
im = imread('label2.png');
im_bin = im2bw(im, 0.5);
[B,L] = bwboundaries(im_bin,'holes');
imshow(label2rgb(L, @jet, [.5 .5 .5]))
[B,L] = bwboundaries(im_bin,'holes');
imshow(label2rgb(L, @jet, [.5 .5 .5]))
[B,L] = bwboundaries(im_bin,'noholes');
imshow(label2rgb(L, @jet, [.5 .5 .5]))
fill = imfill(im_bin, 'holes');
imagesc(wcodemat(bwlabel(fill)));
imagesc(wcodemat(bwlabel(im_bin)));