How to split a binary mask in square 1000x1000 and keep only ROI?

1 view (last 30 days)
Hello! I have got a binary mask (0 background and 1 inside the ROI) and I need to split it in square 1000x1000 and keep only square where there is ROI, e.g. where sum of pixel is more than 80% Many thanks in advance

Answers (1)

Image Analyst
Image Analyst on 22 Oct 2019
Use indexing. For example
croppedImage = originalImage(row1:row2, column1:column2, :);
You need to figure out what row1, row2, column1, and column2 are. Should be easy.
Not sure of what your definition of ROI is. There are lots and lots of squares in the image that contain 80% of the number of pixels in the image.
  2 Comments
France
France on 23 Oct 2019
Thanks for your reply. My binary mask comes from a nevus on a skin (you can image the shape). The mask has all 1 inside the ROI (white) and all 0 outside, in the background (black). So I need to crop the binary mask into square 1000x1000 and then keep the index only of the square in which there is ROI for more than 80%. I guess I need to crop the image first, look each square (maybe with a for cycle) and if the sum of each pixel in a square is >80%, I should keep the index correspondig to that square (ROI) otherwise not, because that is background....
Image Analyst
Image Analyst on 23 Oct 2019
Fill, take largest blob, find centroid, crop.
mask = imfill(mask, 'holes');
mask = bwareafilt(mask, 1);
props = regionprops(mask, 'Centroid');
col1 = round(props.Centroid(1) - 500);
col2 = col1 + 499;
row1 = round(props.Centroid(2) - 500);
row2 = row1 + 499;
croppedImage = originalImage(row1 : row2, col1 : col2, :);
Or, if you want the blob to be 80%, do this
mask = imfill(mask, 'holes');
mask = bwareafilt(mask, 1);
props = regionprops(mask, 'Centroid');
width = sqrt(props.Area / 0.8);
halfWidth = width / 2;
col1 = round(props.Centroid(1) - halfWidth);
col2 = col1 + width - 1;
row1 = round(props.Centroid(2) - halfWidth);
row2 = row1 + width - 1;
croppedImage = originalImage(row1 : row2, col1 : col2, :);
It will probably not be 1000x1000 though.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!