www.gusucode.com > 个用于图像标定的算法matlab源码程序 > code16/lk20_common/occlude_image.m

    function output = occlude_image (input_image, template_loc, occl_img, occl_percentage, occl_coords)
% OCCLUDE_IMAGE - Occlude image portions 
%  OUTPUT = OCCLUDE_IMAGE (INPUT_IMAGE, TEMPLATE_LOC, OCCL_IMG, OCCL_PERCENTAGE, OCCL_COORDS)
%
% INPUT_IMAGE is occluded by a rectangular region taken from OCCL_IMG to a degree determined 
% by OCCL_PERCENTAGE. The location of the rectangle in both output and occl_img is determined
% by the coordinates in OCCL_COORDS.

% Ralph Gross
% Carnegie Mellon University, Pittsburgh

if occl_percentage==0
  img_occl=input_image;
  return;
end

tmpltWidth  = template_loc(3)-template_loc(1)+1;
tmpltHeight = template_loc(4)-template_loc(2)+1;

imgHeight   = size(occl_img, 1);
imgWidth    = size(occl_img, 2);

nbrOcclPixels = tmpltHeight*tmpltWidth*occl_percentage;
avgSideLength = sqrt(nbrOcclPixels);
maxVar        = avgSideLength*0.3;

foundValid = 0;
counter    = 1;
countThres = 100;

while (foundValid==0) & (counter < countThres)
  fact = 1.0;
  if occl_coords(1)> 0.5
    fact=-1.0;
  else
    fact=1.0;
  end
  
  occlWidth  = round(maxVar*occl_coords(2)*fact + avgSideLength);
  occlHeight = round(nbrOcclPixels/occlWidth);
  xpos       = round((tmpltWidth-occlWidth-1)*occl_coords(3)+1);
  ypos       = round((tmpltHeight-occlHeight-1)*occl_coords(4)+1);
  
  xposImg    = round((imgWidth - occlWidth-1)*occl_coords(5)+1);
  yposImg    = round((imgHeight - occlHeight-1)*occl_coords(6)+1);
  
  if (xpos+occlWidth < tmpltWidth) & (ypos+occlHeight < tmpltHeight) & ...
	(xposImg+occlWidth < imgWidth) & (yposImg+occlHeight < imgHeight)
    foundValid = 1;
  else
    counter = counter+1;
  end
  
  xpos = xpos+template_loc(1);
  ypos = ypos+template_loc(2);
  
end

output = input_image;
output(ypos:ypos+occlHeight, xpos:xpos+occlWidth) = occl_img(yposImg:yposImg+occlHeight, xposImg:xposImg+occlWidth);