0001 function [pos, rotangle] = findObjFit( regionObjs, regionBound, regionWeight, objErode, objbound, rotateAngle)
0002 
0003 
0004 
0005 
0006 
0007 
0008 
0009 
0010 
0011 
0012 
0013 
0014 
0015 
0016 
0017 
0018 
0019 
0020 
0021 
0022 
0023 
0024 
0025 
0026 
0027 
0028 
0029 
0030 
0031 
0032 
0033 if ~exist('regionWeight', 'var') | isempty(regionWeight)
0034     regionWeight = region./sum(region(:));
0035 end
0036 
0037 
0038 if ~exist('objErode', 'var') | isempty(objErode)
0039     
0040     objErode = double(obj)./sum(double(obj(:)));
0041 else
0042     objErode = double(objErode);
0043     objErode = objErode./sum(objErode(:));
0044 end
0045 if ~exist('rotateAngle', 'var') | isempty(rotateAngle)
0046     rotateAngle = 360;
0047 end
0048 
0049 
0050 nRotations = floor(360/rotateAngle);
0051 
0052 inds = cell(1, nRotations);
0053 angles = cell(1, nRotations);
0054 weights = cell(1, nRotations);
0055 
0056 region = regionObjs;
0057 regionsize = size(region);
0058 if length(regionsize) == 3 & regionsize(3) >1
0059     region = padarray(region, [0,0,1]);
0060 end
0061 
0062 for i = 1:nRotations
0063     convObj = imrotate(objErode, rotateAngle*(i-1));
0064     erodeRegion = convnfft_fast(region, convObj);
0065     if islogical(region)
0066         erodeRegion = erodeRegion > 0.999999;
0067     end
0068     
0069     if exist('regionBound', 'var') && exist('objbound', 'var')
0070         boundObj = imrotate(objbound, rotateAngle*(i-1));
0071         
0072         erodeBound = convnfft_fast(region, boundObj);
0073         if islogical(regionBound)
0074             erodeBound = erodeBound > 0.999999;
0075         end
0076         
0077         erodeRegion = erodeRegion .* erodeBound;
0078     end
0079     
0080     
0081     if length(regionsize) == 3 & regionsize(3) >1
0082         erodeRegion = erodeRegion(:,:,2:end-1);
0083     end
0084     
0085     weight = regionWeight;
0086     weight(~erodeRegion) = 0;
0087     
0088     
0089     
0090     inds{i} = find(erodeRegion);
0091     angles{i} = ones(size(inds{i})) * i;
0092     weights{i} = weight(inds{i});
0093 
0094 
0095 
0096 
0097     centroid = round(size(convObj)/2);
0098     
0099     if length(centroid) == 2
0100         centroid = [centroid,1];
0101     end
0102 
0103     centroids(i,:) = centroid;
0104 
0105 end
0106 inds = vertcat(inds{:});
0107 angles = vertcat(angles{:});
0108 weights = vertcat(weights{:});
0109 
0110 if isempty(inds)
0111     pos = [-1, -1, -1];
0112     return;
0113 end
0114 
0115 try
0116     ind = randsample(1:length(inds), 1, true, weights);
0117 catch
0118     disp('asdf')
0119 end
0120 
0121 angle = angles(ind);
0122 
0123 placementIndex = inds(ind);
0124 [y, x, z] = ind2sub(size(region), placementIndex);
0125 
0126 pos = [y,x,z];
0127 
0128 rotangle = rotateAngle*(angle-1);
0129 
0130 
0131 
0132 end
0133 
0134 function centroid = getCentroid(obj)
0135     objsize = size(obj);
0136     if length(objsize) == 2
0137         objsize = [objsize 1];
0138     end
0139     
0140     [x,y,z] = meshgrid(1:objsize(2), 1:objsize(1), 1:objsize(3));
0141     
0142     x1 = x.*obj;
0143     y1 = y.*obj;
0144     z1 = z.*obj;
0145     
0146     centroid = [sum(y1(:)) sum(x1(:)) sum(z1(:))]./sum(obj(:));
0147 end