Masking by pre-selected ROI and subtracting all other areas

4 views (last 30 days)
I'm trying to create an algorithm that can generate a binary mask within a pre-determined ROI that is different for each image in the set. For example, the input images would look something like this, with a red rectangle drawn around the ROI. I need to do calculations on the area and intensity of the pixels within the ROI, so I first need to isolate the region by essentially designating the intensity of all pixels outside of the ROI to be 0. My best idea for accomplishing this is to filter using the shift in contrast between the image and the rectangle which is drawn over the image, but I'm not sure of the matlab functions that will allow me to do this. Any help would be appreciated! Thanks!

Answers (1)

Image Analyst
Image Analyst on 13 Jan 2022
You just need poly2mask() and regionprops(). Assuming you have your ROI in a list of (x,y) coordinates:
[rows, columns, numberOfColorChannels] = size(rgbImage);
mask = poly2mask(x, y, rows, columns); % Turn (x, y) into a binary image.
[r, g, b] = imsplit(rgbImage);
redProps = regionprops(mask, r, 'Area', 'MeanIntensity');
greenProps = regionprops(mask, g, 'Area', 'MeanIntensity');
blueProps = regionprops(mask, b, 'Area', 'MeanIntensity');
  3 Comments
Image Analyst
Image Analyst on 13 Jan 2022
That's fine. It will still work. It really doesn't matter what the (x,y) values are or where they came from. You can have different sets of (x,y) values for each ROI and my code will still work. Did you even try it?
yanqi liu
yanqi liu on 14 Jan 2022
yes,sir,use poly2mask get logica mask,and then use it to other images,if the image size not equal,may be use imresize to get mask to image size,and do segment

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!