Home > cellorganizer > utilities > findObjFit.m

findObjFit

PURPOSE ^

Region - binary mask of area to place objects, to be eroded by objWeight

SYNOPSIS ^

function [pos, rotangle] = findObjFit( regionObjs, regionBound, regionWeight, objErode, objbound, rotateAngle)

DESCRIPTION ^

Region - binary mask of area to place objects, to be eroded by objWeight
regionWeight (optional) - pdf of object placement, independent of obj
obj - 3d matrix of an object to be placed
objWeight(optional) - the eroding element for region
rotate angle

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function [pos, rotangle] = findObjFit( regionObjs, regionBound, regionWeight, objErode, objbound, rotateAngle)
0002 %Region - binary mask of area to place objects, to be eroded by objWeight
0003 %regionWeight (optional) - pdf of object placement, independent of obj
0004 %obj - 3d matrix of an object to be placed
0005 %objWeight(optional) - the eroding element for region
0006 %rotate angle
0007 
0008 % grj 9/19/13 - improved object placement in 3D images
0009 
0010 %
0011 % Copyright (C) 2013 Murphy Lab
0012 % Carnegie Mellon University
0013 %
0014 % This program is free software; you can redistribute it and/or modify
0015 % it under the terms of the GNU General Public License as published
0016 % by the Free Software Foundation; either version 2 of the License,
0017 % or (at your option) any later version.
0018 %
0019 % This program is distributed in the hope that it will be useful, but
0020 % WITHOUT ANY WARRANTY; without even the implied warranty of
0021 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0022 % General Public License for more details.
0023 %
0024 % You should have received a copy of the GNU General Public License
0025 % along with this program; if not, write to the Free Software
0026 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0027 % 02110-1301, USA.
0028 %
0029 % For additional information visit http://murphylab.web.cmu.edu or
0030 % send email to murphy@cmu.edu
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)%This doesn't do anything. should be if ~islogical
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 %     centroids(i,:) = round(getCentroid(convObj));
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 % objImg = placeObject(obj, targetSize, [y,x,z]);
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

Generated on Sun 29-Sep-2013 18:44:06 by m2html © 2005