Creating a binary mask from pixels within rectangles

15 views (last 30 days)
Hello all. In previous code, I drew rectangles on an image to visually see regions of interest. I'd like to take these images and turn them into binary masks of logical value.
My goal is to create 2 different binary masks that have 1's corresponding to the red within the green rectangles (caRNA_rect.png; really small but there is a green rectangle) and the red pixels within the yellow rectangles (virion_rect.png). I've attached the two images I'm referring to here.
Anyone have any thoughts? Happy to clarify as needed. Thank you!
EDIT: I created a binary mask initially before inserting rectangles, although I'm still trying to isolate just the pixels within the rectangles. I've uploaded updated figures with the moniker "BW".

Answers (1)

Namnendra
Namnendra on 27 Apr 2023
Hey Aaron,
To create binary masks from the regions of interest (ROIs) defined by the rectangles in your images, you can follow these steps:
1. Load the image and the binary mask:
img = imread('carNA_rect.png'); % or 'virion_rect.png'
BW = imread('carNA_rect_BW.png'); % or 'virion_rect_BW.png'
2. Convert the image to grayscale:
gray_img = rgb2gray(img);
3. Initialize two binary masks to all zeros with the same size as the image:
mask1 = false(size(gray_img));
mask2 = false(size(gray_img));
4. Loop through the rectangles and set the corresponding pixels in the binary masks to 1:
% Define the rectangles
rects = [10 10 50 50; 100 100 75 75]; % example rectangles, replace with your own
for i = 1:size(rects, 1)
rect = rects(i, :);
x = rect(1); y = rect(2); w = rect(3); h = rect(4);
rect_mask = (x <= 1:size(gray_img, 2)) & ((x + w - 1) >= 1:size(gray_img, 2)) & ...
(y <= 1:size(gray_img, 1)) & ((y + h - 1) >= 1:size(gray_img, 1));
rect_mask = rect_mask & BW; % use the binary mask to limit the rectangle to the region of interest
rect_img = gray_img(y:y+h-1, x:x+w-1);
% set pixels in mask1 to 1 if the pixel value in rect_img is > 128 (or any threshold you choose)
mask1(y:y+h-1, x:x+w-1) = rect_mask & (rect_img > 128);
% set pixels in mask2 to 1 if the pixel value in rect_img is > 64 (or any threshold you choose)
mask2(y:y+h-1, x:x+w-1) = rect_mask & (rect_img > 64);
end
In this code, we loop through each rectangle, define a binary mask to restrict the rectangle to the region of interest, extract the corresponding region of the grayscale image, and set the corresponding pixels in the binary masks `mask1` and `mask2` to 1 if the pixel value in the grayscale image exceeds a certain threshold.
Note that the code assumes that the rectangles are defined as `[x y width height]` and stored in the `rects` variable. You'll need to replace this variable with your own rectangles.
Also, the code assumes that you have a binary mask `BW` that specifies the region of interest. If you don't have this mask, you can create it using MATLAB's Image Processing Toolbox functions, such as `imbinarize` or `im2bw`.
Finally, note that the choice of threshold values for creating the binary masks depends on the specific images and the visual appearance of the regions of interest. You may need to experiment with different thresholds to get the best results.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!