Displace images from a txt file

1 view (last 30 days)
Stelios Fanourakis
Stelios Fanourakis on 4 Jul 2018
Hi. I have a stack of images (4D images). I have a txt file that has 2 columns and 15 rows, same number as the images. Now for every row that corresponds to an image, there should be 2 values (x,y) where x denotes the horizontal position of the image and y denotes the vertical one.
All images have the same x value. Means they are properly aligned horizontally with each other. But, they slightly differ on their y vertical position. Some slices are upper or lower than others by a few mm.
Any code idea of how to do that?
I tried this one,but, the translation is applied to all images and the whole stack is shifted. I want to be able to individually translate slices and not the whole stack in one time.
data = dlmread('imgpositions.txt');
[row, col] = size(data);
x=data(1:row, 1);
y=data(1:row, 2);
translation_vector=[row,col,0,0];
IM2=imtranslate(im2(:,:,:,1),translation_vector(1:3));
for n=2:size(im2,4)
IM2(:,:,:,n)=imtranslate(im2(:,:,:,n),translation_vector(1:3));
end
if translation_vector(4)~=0
IM2b=imtranslate(squeeze(IM2(1,:,:,:)),[row col translation_vector(4)]);
IM2b=permute(IM2b,[4 1 2 3]);
for n=2:size(IM2,1)
IM2b(n,:,:,:)=imtranslate(squeeze(IM2(n,:,:,:)),[row col translation_vector(4)]);
end
IM2=IM2b;
end
  2 Comments
Stelios Fanourakis
Stelios Fanourakis on 15 Jul 2018
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?
Jan
Jan on 15 Jul 2018
Edited: Jan on 15 Jul 2018
Even after reading your pile of other related thread, I do not have an idea what this means:
When I use result = F(g) I apply interpolation again to g which
are the original set points (image) multiplied by
translation_vector.
Think twice. "Would it be correct to visualize only with g?" is not clear enough to be answered also.

Sign in to comment.

Answers (1)

Jan
Jan on 15 Jul 2018
Edited: Jan on 15 Jul 2018
Use the debugger to find out, that your translation_vector is exactly the same in each iteration, because it contains the dimension of the imported data only. But I guess, you want to translate by x and y:
data = dlmread('imgpositions.txt');
x = data(:, 1);
y = data(:, 2);
for k = 1:size(im2,4)
IM2(:,:,:,k) = imtranslate(im2(:,:,:,k), [x(k), y(k)]);
end
The part in if translation_vector(4)~=0 is meaningless, because you et this element explicitly to zero. But again translating by:
[row col translation_vector(4)]
cannot be useful, when row and col are the dimension of the imported data, but not the data itself.
Use the debugger to find such problems directly by your own.
  9 Comments
Stelios Fanourakis
Stelios Fanourakis on 17 Jul 2018
Sorry, this is your image for above example. Ignore the previous one.

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!