%-------------------------------------------------------------------- % Function : add_to_image % % This function adds the specified face image to the specified % background image. The function will ask the user to click on where % in the background image to add the face image. % % Input: % source path of rotated image bmp (face) % target path of backgrouand image bmp to add face to % transparency how transparent should face be when added to % background. Value should be between 0.0 and 1.0 % % Output: % final_image returns the source image added to the target image % at the location specified by user % % Example: % fin_image = add_to_image('face.bmp', 'building.bmp', 0.1); % % Info: % Henry Lin % 01/15/2008 for 600.161.01 %-------------------------------------------------------------------- function [final_image] = add_to_image(source, target, transparency); inverse = 1 - transparency; source_image = imread(source); target_image = imread(target); size_source = size(source_image) size_target = size(target_image) figure; imagesc(target_image); axis image stop = 1; %-------------------------------------------------------------------- % Get user input for upper-left coord of where to put source image %-------------------------------------------------------------------- while (stop == 1) disp('--------------------------------------------------------------'); disp('Please select where you would like to put the upper left of the face:'); origin = uint16(floor(ginput(1))) origin = [origin(2) origin(1)]; if (origin(1) + size_source(1) -1 > size_target(1)) stop = 1; disp('--------------------------------------------------------------'); disp('ERROR: Face will not fit on target image in y-direction'); disp('Please try again...'); elseif (origin(2) + size_source(2) -1 > size_target(2)) stop = 1; disp('--------------------------------------------------------------'); disp('ERROR: Face will not fit on target image in x-direction'); disp('Please try again...'); else stop = 0; end end %origin = [origin(2) origin(1)]; %-------------------------------------------------------------------- % add the source image to the target image % use the transparency argument as the transparency %-------------------------------------------------------------------- for y=origin(1):(origin(1) + size_source(1)-1) for x=origin(2):(origin(2) + size_source(2)-1) y_indx = y-origin(1)+1; x_indx = x-origin(2)+1; if(((source_image(y_indx,x_indx,1) >= 240) && (source_image(y_indx,x_indx,2) >= 240) && (source_image(y_indx,x_indx,3) >= 240)) || ((source_image(y_indx,x_indx,1) == 0) && (source_image(y_indx,x_indx,2) == 0) && (source_image(y_indx,x_indx,3) == 0))) else % add to all three color channels target_image(y,x,1) = (transparency * target_image(y,x,1)) + (inverse * source_image(y_indx,x_indx,1)); target_image(y,x,2) = (transparency * target_image(y,x,2)) + (inverse * source_image(y_indx,x_indx,2)); target_image(y,x,3) = (transparency * target_image(y,x,3)) + (inverse * source_image(y_indx,x_indx,3)); end end end final_image = target_image; imagesc(final_image); axis image