Home > cellorganizer > im2blender.m

im2blender

PURPOSE ^

IMG2BLENDER Exports a generated instance from CellOrganizer to a .obj mesh format

SYNOPSIS ^

function [shiftvector, answer] = im2blender( img, savefile, downsample, patchsample, shiftvector_flag)

DESCRIPTION ^

IMG2BLENDER Exports a generated instance from CellOrganizer to a .obj mesh format
that can be read by Blender.

 Inputs: 
 img = a 3d image you wish to obtain the mesh for
 downsample = factor by which you wish to downsample 
 savefile = the path and file name you wish to save the generated file as
 patchsample = the percentage of the verticies calculated that the user
 wants kept. Keeping more will result in a larger .obj file but have
 better resolution. Default value is 0.05
 shiftvector = a 1x3 vector of the amount to shift the resulting mesh.
 This is used to place the mesh at the origin when called with
 syn2blender. 
 Outputs: 
 saves object file that can be loaded into blender specified by savefile
 shiftvector = a 1x3 vector of the amount to shift the resulting mesh.
 This is used to place the mesh at the origin when called with
 syn2blender.
 answer = boolean flag that indicates if the test was successful

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [shiftvector, answer] = im2blender( img, savefile, downsample, patchsample, shiftvector_flag)
0002 %IMG2BLENDER Exports a generated instance from CellOrganizer to a .obj mesh format
0003 %that can be read by Blender.
0004 %
0005 % Inputs:
0006 % img = a 3d image you wish to obtain the mesh for
0007 % downsample = factor by which you wish to downsample
0008 % savefile = the path and file name you wish to save the generated file as
0009 % patchsample = the percentage of the verticies calculated that the user
0010 % wants kept. Keeping more will result in a larger .obj file but have
0011 % better resolution. Default value is 0.05
0012 % shiftvector = a 1x3 vector of the amount to shift the resulting mesh.
0013 % This is used to place the mesh at the origin when called with
0014 % syn2blender.
0015 % Outputs:
0016 % saves object file that can be loaded into blender specified by savefile
0017 % shiftvector = a 1x3 vector of the amount to shift the resulting mesh.
0018 % This is used to place the mesh at the origin when called with
0019 % syn2blender.
0020 % answer = boolean flag that indicates if the test was successful
0021 
0022 % Author: Devin Sullivan
0023 %
0024 % Copyright (C) 2012 Murphy Lab
0025 % Lane Center for Computational Biology
0026 % School of Computer Science
0027 % Carnegie Mellon University
0028 %
0029 % May 8, 2012 Devin Sullivan First downsample img to be on a reasonable scale
0030 % where 1 pixel = 1micron for mcell simulation. For standard cell organizer
0031 % this is a downsampling of 5
0032 % June 19, 2012 I. Cao-Berg Changed the method from ml_makeobjfile.m to img2blender.m,
0033 % added license, documentation as well as checking of the input argument
0034 % July 26 2012 D. Sullivan Create centered object files and reorient cell
0035 % so that the bottom is on the horizontal plane
0036 % October 29, 2012 D. Sullivan Changed the isosurface threshold from 1 to
0037 % 0.5 to find the isosurface for the boolean images.
0038 % Nov 9, 2012 D. Sullivan Added a reducepatch call to significantly reduce
0039 % the file size for the .obj files (e.g. 70MB to ~100KB). Added the
0040 % reduction fraction to the list of inputs and an if statement to set a
0041 % default if it's not defined.
0042 % May 29, 2013 D. Sullivan added "shiftvector" parameter to maintain
0043 % consistent shift (to origin) when running multiple objects in a single
0044 %
0045 % This program is free software; you can redistribute it and/or modify
0046 % it under the terms of the GNU General Public License as published
0047 % by the Free Software Foundation; either version 2 of the License,
0048 % or (at your option) any later version.
0049 %
0050 % This program is distributed in the hope that it will be useful, but
0051 % WITHOUT ANY WARRANTY; without even the implied warranty of
0052 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0053 % General Public License for more details.
0054 %
0055 % You should have received a copy of the GNU General Public License
0056 % along with this program; if not, write to the Free Software
0057 % Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0058 % 02110-1301, USA.
0059 %
0060 % For additional information visit http://murphylab.web.cmu.edu or
0061 % send email to murphy@cmu.edu
0062 
0063 %icaoberg june 19, 2012
0064 %5/29/13 D.Sullivan added checks for shiftvector
0065 %step 0: check input arguments
0066 answer = false;
0067 if nargin == 3
0068     patchsample = 0.05;
0069 %    shiftvector = [0,0,0];
0070 % elseif nargin == 4
0071 %   shiftvector = [0,0,0];
0072 elseif nargin < 3
0073     error('im2blender requires at least 3 input arguments');
0074 end
0075 
0076 if isempty( img )
0077     warning('Input argument image cannot be empty.')
0078     return
0079 end
0080 
0081 %5/29/13 D.Sullivan - added catch for empty patchsample
0082 if isempty ( patchsample )
0083     patchsample = 0.05;
0084 end
0085 
0086 if ~isa( savefile, 'char' )
0087     warning('Input argument savefile must be a string')
0088     return
0089 end
0090 
0091 %need to resize this image
0092 %must give the downsized ratio for each dimension
0093 downsample3D = [downsample,downsample,downsample];
0094 img = ml_downsize(img,downsample3D);
0095 
0096 %need to pad the image so that the isosurface has no holes
0097 img = padarray(img,[3,3,3]);
0098 
0099 
0100 %7/26/12 D.Sullivan
0101  D = smooth3(squeeze(img),'gaussian',[11,11,11]); 
0102 %D = smooth3(squeeze(img),'gaussian',[1,1,1]);
0103 
0104 [x,y,z] = meshgrid(1:size(img,2),1:size(img,1),1:size(img,3));
0105 x = x-mean(x(:));
0106 y = y-mean(y(:));
0107 z = z-mean(z(:));
0108 v = D;
0109 
0110 %make iso-surface (Mesh) of skin
0111 %DPS 10/29/12 changed isosurface threshold for boolean images
0112 %FV = isosurface(D,1);
0113 FV = isosurface(D,0.5); 
0114 
0115 % R. Arepally 6/7/13 checks if the shiftvector_flag is empty. If it is
0116 % then calculate the mean of vertices. This becomes the shift vector for
0117 % all of the objects that are created.
0118 if isempty ( shiftvector_flag ) 
0119     shiftvector_flag = mean(FV.vertices);
0120 end
0121 
0122 
0123 
0124 
0125 %5/29/13 D.Sullivan changed from mean since mean of each object was slighly
0126 %different and the meshes were no longer lined up.
0127 % FV.vertices = FV.vertices-repmat(mean(FV.vertices),size(FV.vertices,1),1);
0128 FV.vertices = FV.vertices-repmat(shiftvector_flag,size(FV.vertices,1),1);
0129 %DPS 11/9/12 added reducepatch to significantly reduce the number of
0130 %verticies calculated for the image
0131 % FV = reducepatch(FV,0.05);
0132 FV = reducepatch(FV,patchsample);
0133 p = patch(FV);
0134 set(p, 'FaceColor', 'red', 'EdgeColor', 'none');
0135 
0136 %calculate iso-normals of the surface
0137 N = isonormals(x,y,z,v,p);
0138 L = sqrt(N(:,1).^2+N(:,2).^2+N(:,3).^2)+eps;
0139 N(:,1) = N(:,1)./L; N(:,2)=N(:,2)./L; N(:,3)=N(:,3)./L;
0140 
0141 % FV.faces = [FV.faces(:,3) FV.faces(:,2) FV.faces(:,1)];
0142   material(1).type='newmtl';
0143   material(1).data='skin';
0144   material(2).type='Ka';
0145   material(2).data=[0.8 0.4 0.4];
0146   material(3).type='Kd';
0147   material(3).data=[0.8 0.4 0.4];
0148   material(4).type='Ks';
0149   material(4).data=[1 1 1];
0150   material(5).type='illum';
0151   material(5).data=2;
0152   material(6).type='Ns';
0153   material(6).data=27;
0154     clear OBJ
0155   OBJ.vertices = FV.vertices;
0156   OBJ.vertices_normal = N;
0157   OBJ.material = material;
0158   OBJ.objects(1).type='g';
0159   OBJ.objects(1).data='skin';
0160   OBJ.objects(2).type='usemtl';
0161   OBJ.objects(2).data='skin';
0162   OBJ.objects(3).type='f';
0163   OBJ.objects(3).data.vertices=FV.faces;
0164   OBJ.objects(3).data.normal=FV.faces;
0165   write_wobj(OBJ,savefile);
0166 
0167 answer = true;
0168 shiftvector = shiftvector_flag;

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