Labeling object in a binary image MATLAB

放肆的年华 提交于 2019-12-23 03:11:16

问题


I am trying to label white objects in a binary image using MATLAB. Basically, my aim is to detect water bodies in a aerial map image and then convert the cimage to binary and then label the bodies. Here is an image example of my output after detecting the bodies and converting to binary. How can I now possibly create a create rectangle around the white objects and label them with text(using connected components)? Thank you!


回答1:


Try this -

%%// Read in the image file
img = im2bw(imread(FILE));

%%// Get bounding box stats
stats = regionprops(bwlabel(img),'Area','Centroid','Perimeter','BoundingBox');
Boxes=cat(1,stats.BoundingBox);

%%// Placeholder to store the final result
maskm = false(size(img,1),size(img,2));

for k1 = 1:size(Boxes,1)

    %%// Initialize a mask representing each bounding box
    mask1 = false(size(img,1),size(img,2));

    %%// Get the coordinates of the boxes
    starty = round(Boxes(k1,1));
    stopy = starty+round(Boxes(k1,3))-1;
    startx = round(Boxes(k1,2));
    stopx = startx+round(Boxes(k1,4))-1;

    %%// Finaly create the mask
    mask1(startx:stopx,starty:stopy) = true;
    maskm = maskm + imdilate(edge(mask1,'sobel'),strel('disk',2));
end

%%// Get the boxes around the original blobs
maskm = maskm + img;
figure,imshow(maskm);

Output

Look into text on how to label the boxes.



来源:https://stackoverflow.com/questions/22618824/labeling-object-in-a-binary-image-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!