MATLAB Answers

How do I stitch multiple images in a spiral order

11 views (last 30 days)
Alexander Hsu
Alexander Hsu on 27 Aug 2020
Commented: Alexander Hsu on 28 Aug 2020
Hi I am trying to stitch images in a spiral order starting from the center moving outward.
I have anywhere from 16-100 microscope images that need to be stitched together to form a square. The microscope takes images in a spiral and are labeled in sequential order of how they are taken. The images are named like so, AS-PC-7173_200824160001_A01f00d0, AS-PC-7173_200824160001_A01f01d0, AS-PC-7173_200824160001_A01f02d0,... the bolded letters represent the image number so for example, a 4x4 montage image will have images in order from 00-015. I am currently using the imtile() function but I have to manually rename each image in the position the function stitches them which is right to left and top to bottom.
This is a representation of how the images are taken, 0 being the first image and 63 being the last. In order for my imtile() function to work I rename image 63 (AS-PC-7173_200824160001_A63f01d0 as 01, image 62 as 02, image 61 as 03, and so on.
The only code I have right now is as follows:
myFolder = '/Users/...';
fileFolder = fullfile(myFolder,'*.tif');
imds = imageDatastore(fileFolder);
stitched = imtile(imds);
imshow(stitched);
Is there a way to have matlab stitch images in an order of my choosing or even a way to rename my images in the order imtile() would stitch. (Rename them as 63, 63, 61, 60, 59, 58, 57, 56, 36, 35,...
Thanks for the help!

  2 Comments

Image Analyst
Image Analyst on 27 Aug 2020
Just make up that array going out all the way to 100 (10-by-10), then it should be trivial. If you can't figure it out, give us code where you assign the 10-by-10 array and we'll do it.
Alexander Hsu
Alexander Hsu on 28 Aug 2020
Thanks for you help. My code is as follows. I have written it so that it renames files in the order I have specified in the array. I'm not sure how to call image files for the imtile() function using my array. My code works but is there a way to skip renaming of files and have my array specify the way imtile() calls my images??
a = [99 98 97 96 95 94 93 92 91 90 64 63 62 61 60 59 58 57 56 89 65 36 35 34 33 32 31 30 55 88 66 37 16 15 14 13 12 29 54 87 67 38 17 4 3 2 11 28 53 86 68 39 18 5 0 1 10 27 52 85 69 40 19 6 7 8 9 26 51 84 70 41 20 21 22 23 24 25 50 83 71 42 43 44 45 46 47 48 49 82 72 73 74 75 76 77 78 79 80 81];
n = numel(a);
for i=1:n
if a(i)<10
filenum1 = ['0', num2str(a(i))];
else
filenum1 = [num2str(a(i))];
end
filenum2 = num2str(i-1);
if i<11
filenum2 = ['0', filenum2];
else
filenum2 = [num2str(i-1)];
end
file = [pwd '/AS-PC-7173_200505000003/AS-PC-7173_200505000003_A01f'];
folder = [pwd '/AS-PC-7173_200505000003n/AS-PC-7173_200505000003_A01f'];
filename = [file, filenum1 'd0.TIF'];
img = imread(filename);
newfilename = [folder, filenum2,'.TIF'];
imwrite(img, newfilename);
end
myFolder = [pwd '/AS-PC-7173_200505000003n/'];
fileFolder = fullfile(myFolder,'*.tif');
imds = imageDatastore(fileFolder);
montage = imtile(imds);
imshow(montage);

Sign in to comment.

Answers (1)

jonas
jonas on 27 Aug 2020
Edited: jonas on 27 Aug 2020
I would just put each image in a cell array, A(1)... A(n) based on their original name.
Then it's just a matter of indexing. Here I've made 10000 2x2 RGB images as an example, using spiral() to make the indices.
for i = 1:10000
A{i} = repmat(i/10000,2,2,3);
end
new_id = spiral(100);
A_new = A(new_id);
I = cell2mat(A_new);
imshow(I)

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by