How to zero pad the cut image when it reached a boundary on actual image

1 次查看(过去 30 天)
Hi, I am trying to cut some images into smaller images based on some segmentation results.
Actual images size: 665x512 double
Cut Images size: 411x223 uint8
img = imread('FK0123000_1002_RML.jpg');
imgSize = size(img);
BW = bwlabel(img);
props = regionprops(BW,'centroid');
centroids = round(cat(1, props.Centroid));
cutImg = img(centroids(2)-204:centroids(2)+ 206, centroids(1)-110:centroids(1)+ 112);
figure, imshow(cutImg,[]);
In case if the segment in the actual image is within the below condition I am able to cut it, else I get an error. how to adjust this so it pads zeros for columns or rows if error condition comes?
The problem comes here:
cutImg = img(centroids(2)-204:centroids(2)+ 206, centroids(1)-110:centroids(1)+ 112);
Working Case:
Failed Case:
The second or failed case crosses the matrix dimensions to get the cut image, is there any way to automatically zero pad missing rows or columns?
Thanks

采纳的回答

Abel Babu
Abel Babu 2017-6-2
Hi Gopichandh,
In order to cut out the portion of the image that has the segmented section, the best way would be to use a bounding box that is specified by the 'regionprops' function directly. This can be obtained by adding the 'Bounding Box' property for the 'regionprops' function. More about the same can be found in this link: https://in.mathworks.com/help/images/ref/regionprops.html#buoixjn-3
In effect, 'Bounding Box' will give you the smallest bounding box that can enclose the segmented section. This would be a better approach than hard-coding the values.
Abel
  1 个评论
Gopichandh Danala
Thanks, BoundingBox helps me in getting the cords of rect around blob, I can append zeros around in all regions and get what I needed.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by