File: ef230-2023-08/www/examples/image_trans.m Download
function main
% example of making part of an image transparent
close all;
i_es=imread(''); % get image matrix
[h w c] = size(i_es); % get the size of everett's image
t_es = mycircle(h,w,0.5); % get the transparency matrix
h_es = image(i_es); % show the image
set(h_es,'alphadata',t_es); % set the transparency
axis equal
end % main

function t=mycircle(h,w,v)
% input h,w - height and width of the matrix
%       v - transparency value for the border, default to 0
% output t - h x w transparency matrix with 1's (transparent) inside the
% circle
if nargin<3, v=0; end
t = ones(h,w); % create a matrix for transparency data, all ones to start with
cx = floor(w/2); % center x
cy = floor(h/2); % center y
rad = min(cx,cy); % get a radius that will fit
% loop through the pixels
for r=1:h
    for c=1:w
        x = c-cx; % x location from center
        y = r-cy; % y location from center
        d = sqrt(x*x+y*y); % distance from center
        if d>rad, t(r,c) = v; end % zero it if outside the circle
end %mycircle