         
showObjectBoundingBox=1;

%% set rgb format of grayscale image as background of "matchBoundingBox".    
tmp_img=imageLoaded.imageOriginal;
tmp_img=imresize(tmp_img, size(imageLoaded.ImageMultiResolution{Mind}), 'nearest');
greyImg = rgb2gray(tmp_img);  % images at multiple resolutions 
            
rgb_greyImg = cat(3,greyImg,greyImg,greyImg);
matchedBoundingBox = rgb_greyImg;           


%% prepare the output variable for visualization of matched template
if showObjectBoundingBox
    margin = 2;  % line width of the bounding box
    xx = repmat((1:sx),1, margin*2);
    yy = [];
    for y = [1:margin sy-margin+1:sy]
        yy = [yy,ones(1,sx)*y];
    end
    yy = [yy,repmat((1:sy),1,margin*2)];
    for x = [1:margin sx-margin+1:sx]
        xx = [xx,ones(1,sy)*x];
    end
    
    %% small square in the top left corner
     sx_small=8;
     sy_small=8;
     xx = [xx, repmat((0:-1:-sx_small+1),1, margin*2)];
     for y = [1:margin sy_small-margin+1:sy_small]
        yy = [yy,ones(1,sx_small)*y];
     end
     yy = [yy,repmat((1:sy_small),1,margin)];
     for x = [1-sx_small:margin-sx_small]
        xx = [xx,ones(1,sy_small)*x];
     end
    
    inRow = single(xx-floor(sx/2));
    inCol = single(yy-floor(sy/2));
    tScale = 0; rScale = 1; cScale = 1; inO = zeros(numel(inRow),1,'single'); inS = zeros(numel(inRow),1,'single');
    
    [outRow, outCol] = mexc_TemplateAffineTransform(tScale,rScale,cScale,Mrot,inRow,inCol,inO,inS,nOrient);
    
    % directly overwrite the corresponding pixels
    for p = 1:length(outRow)
        x = round(MFx + outRow(p));
        y = round(MFy + outCol(p));
        if x > 0 && x <= size(matchedBoundingBox,1) && y > 0 && y <= size(matchedBoundingBox,2)
            matchedBoundingBox(x,y,:) = [255 0 0];  % color of the bounding box
            % matchedBoundingBox(x,y) = [0.1];
        end
    end
end
            
imwrite(matchedBoundingBox, fullfile(boundingBoxSavingFolder,['boundingBox-img-' num2str(iImg,'%04d') '.png']));  
% 
            