select slice from 3D image [B3, Xdim, Ydim] = select_slice(B,indx,vdim) B : 3D-image [NBx, NBy, NBz]. The coordinate system must be RAS. Note that the default of ANALYZE coordinate is LAS. indx : slice index in vdim-axis vdim : slice cut direction = 'x' : Sagittal cut : Y-Z plane = 'y' : Coronal cut : X-Z plane = 'z' : Transverse (Axial) cut : X-Y plane B3 selected 2D slice image Xdim Ydim image(Xdim, Ydim, B3, 'CDataMapping','scaled'); Copyright (C) 2011, ATR All Rights Reserved. License : New BSD License(see VBMEG_LICENSE.txt)
0001 function [B3, Xdim, Ydim] = select_slice(B,indx,vdim) 0002 % select slice from 3D image 0003 % [B3, Xdim, Ydim] = select_slice(B,indx,vdim) 0004 % 0005 % B : 3D-image [NBx, NBy, NBz]. The coordinate system must be RAS. 0006 % Note that the default of ANALYZE coordinate is LAS. 0007 % indx : slice index in vdim-axis 0008 % vdim : slice cut direction 0009 % = 'x' : Sagittal cut : Y-Z plane 0010 % = 'y' : Coronal cut : X-Z plane 0011 % = 'z' : Transverse (Axial) cut : X-Y plane 0012 % B3 selected 2D slice image 0013 % Xdim 0014 % Ydim 0015 % 0016 % image(Xdim, Ydim, B3, 'CDataMapping','scaled'); 0017 % 0018 % Copyright (C) 2011, ATR All Rights Reserved. 0019 % License : New BSD License(see VBMEG_LICENSE.txt) 0020 0021 0022 indx = round(indx); 0023 0024 [NBx,NBy,NBz]=size(B); 0025 0026 % dimension of image data 0027 xdim = [0, NBx]; 0028 ydim = [0, NBy]; 0029 zdim = [0, NBz]; 0030 0031 % Analyze-slice-cut direction 0032 switch vdim, 0033 case 'x', 0034 % 'SAG' : Sagittal cut : Y-Z plane 0035 B2 = reshape(B(indx,:,:),NBy,NBz); 0036 Xdim = ydim; 0037 Ydim = zdim; 0038 case 'y', 0039 % 'COR' : Coronal cut : X-Z plane 0040 B2 = reshape(B(:,indx,:),NBx,NBz); 0041 Xdim = xdim; 0042 Ydim = zdim; 0043 case 'z', 0044 % 'TRN' : Transverse (Axial) cut : X-Y plane 0045 B2 = B(:,:,indx); 0046 Xdim = xdim; 0047 Ydim = ydim; 0048 end; 0049 0050 % Squeeze to 2D 0051 B2 = squeeze(B2); 0052 0053 % RGB color matrix (gray level): transpose of 2D matrix for image plot 0054 B3 = repmat(B2',[1 1 3]); 0055 0056 return 0057 %%%%%%%%%%%%%--------------------END----------------- 0058 0059 [Nx,Ny] = size(B2); 0060 0061 % Index color -> True color 0062 if nargin < 4 0063 B3 = repmat(B2,[1 1 3]); 0064 0065 image(Xdim, Ydim, B3, 'CDataMapping','scaled'); 0066 else 0067 BC = zeros(Nx,Ny,3); 0068 BC(:,:,cdim) = B2; 0069 % 0070 % sum(BC(:) > 0) 0071 0072 image(Xdim, Ydim, BC, 'CDataMapping','scaled'); 0073 end 0074 0075 set(gca,'YDir','normal','XLimMode','manual','YLimMode','manual'); 0076 axis equal 0077 axis tight