From ImageJ to Matlab Conversion

HI.
Can someone translate this ImageJ piece of code to MATLAB equivalent. If not, is there an app for automated conversion between those two formats?
Thank you
for(i=1;i<=nSlices;i++){
setSlice(i);
run("Translate...", "x="+i*xTranslate+" y="+i*yTranslate+"
interpolation=None slice");
}

6 Comments

Guillaume
Guillaume on 12 Jul 2018
Edited: Guillaume on 12 Jul 2018
Considering that the subset of persons that know how to script ImageJ and know matlab is probably not that big, if you know what the above code is doing (particularly the setSlice) you'll get more chance of getting help.
All I can guess is that it translate (which you can do with imtranslate) some slices. It's unknown what these slices are.
I believe it might be the solution to my problem. It opens a stack of images and translate individually the slices. I need to try it. There are some plug-ins for the Matlab/ImageJ operation. I need to better search on them
Translating a stack of image is trivially done in matlab with imtranslate. So, if it's all you need to do, just use that function.
Have you read my previous posts? It's not that simple to my case. It won't do what I want to do. Please read my previous posts and questions
Oh, I didn't realised it was you. Well, as you've requested, I will not get involved in your questions.

Sign in to comment.

Answers (1)

Sean de Wolski
Sean de Wolski on 12 Jul 2018
Edited: Sean de Wolski on 12 Jul 2018
Looks like imtranslate should work. My interpretation of that is: loop over slices, translate each slice by some amount (that value may vary by slice - I don't know). You may not be able to do it all in one shot but it should certainly work. Something along these lines, not tested.
VT = zeros(size(V),'like',V)
for ii = 1:size(V,3)
VT(:,:,ii) = imtranslate(V(:,:,ii), [something_row(ii), something_col(ii)])
end
Can you describe what you're trying to do, i.e. what is the end goal?

15 Comments

What about if it is a 4D image?
Does it become like this?
VT = zeros(size(V),'like',V) for ii = 1:size(V,4) VT(:,:,:,ii) = imtranslate(V(:,:,:,ii), [something_row(ii), something_col(ii)]) end
I get the error
"Error using imtranslate>validateTranslation (line 494) TRANSLATION must be a 2-element or 3-element translation vector.
Error in imtranslate>preparseSpatialReferencingObjects (line 526) validateTranslation(translation);
Error in imtranslate (line 129) [R_A, varargin] = preparseSpatialReferencingObjects(varargin{:});
Error in experiment (line 82) VT(:,:,:,ii) = imtranslate(im2(:,:,:,ii), [row(ii), col(ii),0,0])
>> Even if I do it for 3D image, still I get another error. It needs to be modified somehow
[row(ii), col(ii),0,0]
is four elements. It should be two to translate in row/col or three to translate in row/col/slice. It won't translate into 4d.
So you tell me that 4D images cannot be translated?
Even with 3D I get the following error
Index exceeds matrix dimensions.
Error in experiment (line 86)
VT(:,:,ii) = imtranslate(im2(:,:,ii), [row(ii), col(ii)])
I also get that error if I use that translation
Error using imtranslate>postValidateTranslation (line 506) When TRANSLATION is a three-element vector, the input image, A, must be 3-D.
Error in imtranslate>preparseSpatialReferencingObjects (line 527) postValidateTranslation(varargin{1},translation);
Error in imtranslate (line 129) [R_A, varargin] = preparseSpatialReferencingObjects(varargin{:});
Error in experiment (line 86) VT(:,:,ii) = imtranslate(im2(:,:,ii), [row(ii), col(ii) 0])
Squeeze a 4d grayscale image to make it 3d.
Apparently to translate into 3d, you need a 3d matrix. That might mean it makes more sense to do the 3d part of the translation all at once. I.e. loop over translating in 2d if slices translate different amounts, then do the 3d piece all at once.
I don't really understand what your goal is but certainly some combination of these steps is what you want.
Excuse me. Can permute function be used for slice transposition?
Nobody is born knowing how to do stuff in Matlab. A great method of learning how Matlab works is to actually read the documentation of function you're guessing might be useful. The documentation is quite good, it's one of the big advantages over Octave.
Permute changes the order of dimensions of an array. You could certainly use it here, but you don't necessarily need it.
Rik I suppose I cannot use permute and define columns and rows of a txt file for its dimensions (e.g
permute(im2[col rows 0])).
I tried it with no luck but you may have a different opinion.
@Sean
I don't really understand what your goal is but certainly some combination of these steps is what you want.
Well, as I said, I want the translation parameters to be loaded from a txt file and be applied to every slice of the 4D stack, independently.
As a result, different slices will have different y positions. Some will be upper or lower than others, in the stack.
Sean am I clear. Can you please suggest a code for that?
Thank you
Is there an add-on to use ImageJ code in Matlab?
Different y/x but no translation in z? If so, squeeze the stack of images to get 3d, run the loop over each one like I did above, and pass just the x/y parameters into imtranslate. Basically exactly what I have in the answer.
permute is the equivalent of imagej reslice I believe. I haven't looked at ImageJ since 2009 when I rewrote everything we were doing there in MATLAB. There are no plugins to my knowledge and with my knowledge of ImageJ at the time getting as far away from it as possible was the best practice.
@Sean. I tried your answer but it doesn't work for me. It doesn't translate separate slices
@Sean. I though of another solution. When I use result = F(g) I apply interpolation again to g which are the original set points (image) multiplied by translation_vector.
That means that the original image has already been shifted plus a new interpolation coming on the way (F(g)). Does it make it somehow to be smushed or squeezed?
If I visualize with only g without F(g) I get errors later on to the uicontrol of the slider (e.g. Index Exceeds Matrix Dimensions).
Would it be correct to visualize only with g?

Sign in to comment.

Asked:

on 12 Jul 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!