How to overlay a solid rectangle on the top of a image?

Hi all,
I got a question of how to overlay a solid retangle on the top of a image? The example is shown below. The white solid rectangle blocks portion of the image. Also, is there anyway I can change the transparency of the white rectangle?
Thank you so much!

 Accepted Answer

How the rectangle mask is generated is up to you. In these examples, I'm generating it programmatically using basic indexing. You could also use ROI tools such as drawrectangle() and createMask().
The basics:
Using basic tools, assuming the background is single-channel and that the overlay is strictly white:
% get an image
inpict = imread('cameraman.tif');
% parameters
rect = [50 50 100 150]; % rectangle extents [x0 y0 w h]
alph = 0.4; % opacity of the rectangle
% create rectangle mask
sz = size(inpict);
mask = false(sz(1:2));
rows = rect(2):rect(2)+rect(4)-1;
cols = rect(1):rect(1)+rect(3)-1;
mask(rows,cols) = true;
% simplified output composition for white-only overlay
% this relies on the mask being of class 'logical'
outpict = inpict;
outpict(mask) = im2uint8((1-alph)*im2double(inpict(mask)) + alph);
imshow(outpict)
Using labeloverlay():
Since the output opacity may need to be adjusted, IPT imoverlay() isn't an option. You could abuse labeloverlay() instead. While this allows the background image to be either I/RGB, the foreground tuple and ouput image are both RGB regardless of what the background is.
% get an image
inpict = imread('cameraman.tif');
% parameters
rect = [50 50 100 150]; % rectangle extents [x0 y0 w h]
alph = 0.4; % opacity of the rectangle
% create rectangle mask
sz = size(inpict);
mask = false(sz(1:2));
rows = rect(2):rect(2)+rect(4)-1;
cols = rect(1):rect(1)+rect(3)-1;
mask(rows,cols) = true;
% create a colortable (with only one tuple)
CT = [1 1 1];
% reproduce the alpha blend example from before
outpict = labeloverlay(inpict,mask,'transparency',(1-alph),'colormap',CT);
imshow(outpict)
Using third-party tools:
If using MIMT replacepixels(), this becomes both simpler and more flexible. In this example, the output remains single-channel, as both the background image and the foreground tuple are single-channel.
% get an image
inpict = imread('cameraman.tif');
% parameters
rect = [50 50 100 150]; % rectangle extents [x0 y0 w h]
alph = 0.4; % opacity of the rectangle
% create rectangle mask
sz = size(inpict);
mask = false(sz(1:2));
rows = rect(2):rect(2)+rect(4)-1;
cols = rect(1):rect(1)+rect(3)-1;
mask(rows,cols) = true;
% specify the fg color
% can be I/IA/RGB/RGBA
% output is expanded only as necessary
FG = [1 alph]; % IA
% reproduce the alpha blend example from before
outpict = replacepixels(FG,inpict,mask);
imshow(outpict)
See also:

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 20 Feb 2023

Edited:

DGM
on 27 Mar 2024

Community Treasure Hunt

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

Start Hunting!