My apologies, I jumped ahead without noticing the issue of it being a
4D file (this is the issue) and varying sizes (this isn't the issue as
spm_imcalc resamples the data).
Here is the work around:
outputname='outputfilenamewithextension';
files=dir('beta*img'); % Can also use files=spm_select(Inf,'image');
and replace numel(files) with size(files,1) and files(ii).name with
files(ii,:)
for ii=1:numel(files); hdr(ii)=spm_vol(files(ii).name);
img=spm_read_vols(hdr); end
img2=2.*img; % or any other equation
for ii=1:numel(files); hdr(ii).fname=outputname; hdr(ii).n(1)=ii; end
for ii=1:numel(files); spm_write_vol(hdr(ii),img2(:,:,:,ii)); end
%The later loops could be combined into a single loop.
In the case where your files are different sizes, the additional step
needs to be done:
You need to remove the first for loop and use:
hold=0; %NN interpolation, see spm_imcalc for details
for ii=1:numel(files); hdr(ii)=spm_vol(files(ii).name); end
img=zeros([hdr(1).dim numel(files)]);
for p = 1:hdr(1).dim(3),
B = spm_matrix([0 0 -p 0 0 0 1 1 1]);
X=zeros(numel(files),prod(hdr(1).dim(1:2)));
for jj = 1:numel(files)
M = inv(B*inv(hdr(1).mat)*hdr(jj).mat);
d = spm_slice_vol(hdr(jj),M,hdr(1).dim(1:2),[hold,NaN]);
X(jj,:) = d(:)';
end
img(:,:,p,:) = reshape(X,[hdr(1).dim(1:2) 1 numel(files)]);
end
Then the rest of the code from above.
This should work for what you need. Hope this helps. Sorry for the
misreading of your first email.
Best Regards, Donald McLaren
=================
D.G. McLaren, Ph.D.
Postdoctoral Research Fellow, GRECC, Bedford VA
Research Fellow, Department of Neurology, Massachusetts General Hospital and
Harvard Medical School
Office: (773) 406-2464
=====================
This e-mail contains CONFIDENTIAL INFORMATION which may contain PROTECTED
HEALTHCARE INFORMATION and may also be LEGALLY PRIVILEGED and which is
intended only for the use of the individual or entity named above. If the
reader of the e-mail is not the intended recipient or the employee or agent
responsible for delivering it to the intended recipient, you are hereby
notified that you are in possession of confidential and privileged
information. Any unauthorized use, disclosure, copying or the taking of any
action in reliance on the contents of this information is strictly
prohibited and may be unlawful. If you have received this e-mail
unintentionally, please immediately notify the sender via telephone at (773)
406-2464 or email.
On Tue, Oct 18, 2011 at 1:44 PM, MCLAREN, Donald
<[log in to unmask]> wrote:
> 2.*X should work.
>
> Has to do with matrix multiplication versus element-wise multiplication.
>
> Best Regards, Donald McLaren
> =================
> D.G. McLaren, Ph.D.
> Postdoctoral Research Fellow, GRECC, Bedford VA
> Research Fellow, Department of Neurology, Massachusetts General Hospital and
> Harvard Medical School
> Office: (773) 406-2464
> =====================
> This e-mail contains CONFIDENTIAL INFORMATION which may contain PROTECTED
> HEALTHCARE INFORMATION and may also be LEGALLY PRIVILEGED and which is
> intended only for the use of the individual or entity named above. If the
> reader of the e-mail is not the intended recipient or the employee or agent
> responsible for delivering it to the intended recipient, you are hereby
> notified that you are in possession of confidential and privileged
> information. Any unauthorized use, disclosure, copying or the taking of any
> action in reliance on the contents of this information is strictly
> prohibited and may be unlawful. If you have received this e-mail
> unintentionally, please immediately notify the sender via telephone at (773)
> 406-2464 or email.
>
>
>
>
> On Tue, Oct 18, 2011 at 1:33 PM, Uenal <[log in to unmask]> wrote:
>> Hi,
>>
>> I am wondering whether anyone had similar issues below with imcalc in datamatrix mode and I'd appreciate any help and suggestions...
>>
>> I am trying to use imcalc (SPM8) in order to read my 3D .img volumes in the data matrix (X) and then operate on X; however it is giving errors even if I want to make simple operations such as 2*X. All 948 images have the same dimension and orientations since they are normalized. I also played with different masking options but no luck.
>> Here is the error, I get:
>>
>>>SPM8: spm_imcalc_ui (v3691) 12:08:33 - 18/10/2011
>>>========================================================================
>>>Failed 'Image Calculator'
>>>Error using ==> spm_imcalc at 103
>>>"2*X" produced incompatible image.
>>>In file "C:\spm8\spm8\spm_imcalc.m" (v1143), function "spm_imcalc" at line 103.
>>>In file "C:\spm8\spm8\spm_imcalc_ui.m" (v3691), function "spm_imcalc_ui" at line 159.
>>>In file "C:\spm8\spm8\config\spm_cfg_imcalc.m" (v3736), function "my_spm_imcalc_ui" at line 174.
>>>
>>>The following modules did not run:
>>>Failed: Image Calculator
>>
>> And here is the line 103 of spm_imcalc:
>>
>> if prod(Vo.dim(1:2)) ~= numel(Yp),
>> error(['"',f,'" produced incompatible image.']);
>> end
>> When I check this, Vo.dim(1)=53 and Vo.dim(2)=63. However, numel(Yp)=3165372 (=53*63*46*948). So it is complaining that they are not matching.
>>
>> I understand that this is because imalc seems to try calculations plane by plane and trying to write one single img file; is that right? Is it not possible to use imcalc to operate on images and write out a big 4D file or prefixed 3D img volumes instead of one single 3D img volume?
>>
>> Thanks,
>>
>> ..
>> Unal
>>
>
|