How to zero pad the cut image when it reached a boundary on actual image
3 次查看(过去 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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/164682/image.jpeg)
Failed Case:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/164683/image.jpeg)
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
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!