how can I concatenate cropped images from a folder?
Show older comments
Hi
I am a beginner in matlab, and I was assigned a project to crop a series of images in a folder and then combine the together.
I always get stuck because I don't know how to make the loop. so far I called the path of the folder. can you direct me to what kind of functions should I be using?
Thank You.
Answers (2)
Here is a bit of code to get you going. It creates a dialog title to allow the user to select the folder containing tiff files (you can change to other extensions if you want) to crop, then store the images in a cell array and use a loop to actually crop the images using imcrop. For the example I'm assuming a grayscale image, but it can easily be modified.
clear all
clc
dialog_title = 'Select the directory containing the images to be processed';
folder_name = uigetdir('',dialog_title);
addpath(folder_name);
% select current folder
cd(folder_name);
ImagesToRead = dir('*.tif');
ImageCell = cell(1,length(ImagesToRead));
CroppedImageCell = cell(1,length(ImagesToRead));
for k = 1:length(ImagesToRead)
ImageCell{k} = imread(ImagesToRead(i).name); % Read image and store in cell array.
CroppedImageCell{k} = imcrop(ImageCell{k},CropRectangle); % See comments below
end
% Save to resulting images in a sequence or a stack:
% 1) Sequence:
SequenceName = 'DummyName' ; name of the sequence...change as you wish.
for k = 1:length(ImagesToRead)
ExportedName = sprintf('%s%d.tif',SequenceName,k); % Add the frame # after the name
imwrite(CroppedImageCell{k},ExportedName,'tif','WriteMode','overwrite','Compression','none');
end
% 2) Stack
SequenceName = 'DummyName' ; name of the sequence...change as you wish.
for k = 1:length(ImagesToRead)
imwrite(CroppedImageCell{k},SequenceName,'tif','WriteMode','append','Compression','none'); % Note the "append" writing mode.
end
In the above code, you will want to change "CropRectangle" to a rectangle with the size/position that best fits your need. Consult the link I provided above to know how to do so.
If it's not clear please ask for more details :)
4 Comments
Anders Mahler
on 14 Oct 2014
Hello, i read you answer, and it helped me a lot, but i have one question, if you dont mind. When you make the export name, it will write eks, dummyname 1 dummyname 2 dummyname 3 ... dummyname 10 dummyname 11
Is there any way to make it write dummyname 001 dummyname 002 dummyname 003 ... dummyname 010 dummyname 011 dummyname 012 ... dummyname 103 dummyname 104 dummyname 105
filename = ['dummyname' sprintf('%03d', i)];
Ben11
on 15 Oct 2014
Oh sorry I did not see your comment. @Thorsten's answer is the way to go. Glad it helped you!
Image Analyst
on 15 Oct 2014
Seems like a weird, unnecessary mixing of different string methods to me. I'd do it more simply like this:
filename = sprintf('dummyname%03d', i);
Image Analyst
on 18 Aug 2014
0 votes
The images have to match up in the dimension they are to be stitched together in. You can try the montage() function.
You can also try this stitching app: http://www.mathworks.com/matlabcentral/fileexchange/25797-stitch which handles images of mismatched sizes.
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!