0001 function img2xml( img , filename )
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
0034 if( nargin > 2)
0035 error('CellOrganizer: Wrong number of input arguments.' );
0036 end
0037
0038 if (length(size(img)) ~= 3)
0039 error('Invalid image' );
0040 end
0041
0042 docNode = com.mathworks.xml.XMLUtils.createDocument('sparse');
0043
0044
0045 for z_index=1:size(img,3)
0046 z_slice = docNode.createElement('z-slice');
0047 img_slice = img(:,:,z_index);
0048 img_sparse = sparse(img_slice);
0049 [i,j,val] = find(img_sparse);
0050
0051 for n=1:length(val)
0052 x = i(n);
0053 y = j(n);
0054 temp_val = val(n);
0055
0056 sparse_element = docNode.createElement('element');
0057 x_node = docNode.createElement('x');
0058 x_text = docNode.createTextNode(num2str(x));
0059 x_node.appendChild(x_text);
0060 sparse_element.appendChild(x_node);
0061
0062 y_node = docNode.createElement('y');
0063 y_text = docNode.createTextNode(num2str(y));
0064 y_node.appendChild(y_text);
0065 sparse_element.appendChild(y_node);
0066
0067 val_node = docNode.createElement('value');
0068 val_text = docNode.createTextNode(num2str(temp_val));
0069 val_node.appendChild(val_text);
0070 sparse_element.appendChild(val_node);
0071
0072 z_slice.appendChild(sparse_element);
0073
0074 end
0075 docNode.getDocumentElement.appendChild(z_slice);
0076 end
0077
0078 xmlwrite([filename '.xml'], docNode);
0079 end