在二进制图像matlab中获取形状的框架a

时间:2012-08-03 11:15:17

标签: matlab

我有以下二进制图片: enter image description here

我正在尝试的最终收到的是: enter image description here 有没有人有解决它的想法? 非常感谢!

1 个答案:

答案 0 :(得分:0)

对于这个问题,我们假设字母在黑色背景(0)上是白色的(1)。 (即图像的外部是黑色的)

以下matlab脚本使用侵蚀,扩张,填充和AND / OR / NOT逻辑运算的组合:

     %This script assumes the input image is a binary bitmap image
     %Otherwise use: 
     %BW = logical(BW); after loading grayscale image with BW = imread(filename); 

     BW = imread('img.bmp'); %read in the image
     figure
     imshow(BW) %show original image

     %for the outside edges
     BW2 = imfill(BW, 'holes'); %fill holes in letters
     BW3 = imerode(BW2, strel('disk',1));
     BW4 = BW2 & (~BW3); %outside boundary of letters (8 connected)
     figure
     imshow(BW4) %show outside edges

     %for the inside edges
     BW2 = imerode(BW3, strel('disk',1)); %erode filled letters again
     BW3 = ~(BW & BW2);  %logical AND operation of original image and doubly eroded filled letter image
     BW2 = ~imfill(BW3,1); %fill image with white (1), starting at 1,1 leaves
     BW3 = imfill(BW2, 'holes'); %fill inner holes
     BW2 = BW3 & (~BW2); %logical operation to isolate inner holes
     BW3 = imdilate(BW2,strel('disk',1))-BW2; %boundary of inner edges

     BW2 = BW3 | BW4; %logical OR operation to obtain inside and outside edges
     figure
     imshow(BW2) %final image