concat images in order
6 views (last 30 days)
Show older comments
khaled alsaih
on 18 Oct 2019
Commented: khaled alsaih
on 23 Oct 2019
hello guys
i have 10 images of size 512x512
i need to concat 3 images at first for example
i want to concat 1st image and 2nd image and 3rd image in one image (like RGB form)
then i want to concat 2nd image and 3rd image and 4th image in one image to make the second image
then i want to concat 3rd image and 4th image and 5th image in one image to make the third image and so on
so the total number of images that should be obtained 8 images
can you please help me on that
thanks
2 Comments
Accepted Answer
Image Analyst
on 22 Oct 2019
Try this:
inputFolder = '/home/user/PycharmProjects/2.5dataconversion/images/patient_1';
outputFolder = '/home/user/PycharmProjects/2.5dataconversion/images/patient_2';
filePattern = fullfile(inputFolder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles) - 2
% Read in image 1
baseFileName = theFiles(k).name;
fullFileName = fullfile(inputFolder, baseFileName);
image1 = imread(fullFileName);
% Read in image 2
baseFileName = theFiles(k+1).name;
fullFileName = fullfile(inputFolder, baseFileName);
image2 = imread(fullFileName);
% Read in image 3
baseFileName = theFiles(k+2).name;
fullFileName = fullfile(inputFolder, baseFileName);
image3 = imread(fullFileName);
% Now create an RGB image from those 3 gray scale images.
rgbImage = cat(3, image1, image2, image3);
% Write to disk
baseFileName = sprintf('image %2.2d.png', k);
outputFullFileName = fullfile(outputFolder, baseFileName);
imwrite(rgbImage, outputFullFileName);
end
More Answers (2)
Ganesh Regoti
on 21 Oct 2019
Hi,
Assuming that you have images in 2D format i.e., 512 X 512 matrices. Here is a sample code that could help you out in doing it where I assumed images to be of size 4 X 4.
clc; clear;
%3D matrix to store images of size 4 X 4 (for testing)
images = zeros(4,4,10);
for i = 1:10
k = randi(4,4); %Generate random 4 X 4 matrices
images(:,:,i) = k(:,:);
end
temp = zeros(4,4,3); % temporary storage which stores the image data in RGB format
for i = 1:8 % Traverse through 1 to 8 images
if i == 1 % For the first attempt fill all the layers of temp
temp(:,:,1) = images(:,:,i);
temp(:,:,2) = images(:,:,i+1);
temp(:,:,3) = images(:,:,i+2);
else % For the rest delete the first layer and append third layer
temp(:,:,1) = [];
temp(:,:,3) = images(:,:,i+2);
end
% temp is the expected image at each iteration.
end
Hope this helps !
8 Comments
Rik
on 21 Oct 2019
The randi call was just to generate the example array. There are two steps in your problem: the first step is to read the slices to get the 3D volume, the second step is to write triplets to an RGB image.
Rik
on 21 Oct 2019
folder= '/home/user/PycharmProjects/2.5dataconversion/images/patient_1'
dest = '/home/user/PycharmProjects/2.5dataconversion/images/patient_2'
filePattern = fullfile(folder, '*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
%load slices to 3D array
for k = 1 : numel(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(folder, baseFileName);
temp=imread(fullFileName);
if k==1
%initialize array with first slice
imageArray = temp(:,:,1);%assume all channels are the same
imageArray(1,1,length(theFiles)) = 0;%extend to full array size
else
imageArray(:,:,k) = temp(:,:,1);
end
end
%write slices
for k = 1 : (size(imageArray,3)-2)
temp = imageArray(:,:,k+[0 1 2]);
base = sprintf('Slice%d.png', k);
full = fullfile(dest, base);
imwrite(temp, full);
end
3 Comments
Rik
on 21 Oct 2019
Use the debugger; set a breakpoint at the first line of code and go through this code line by line. There should either be an error, or something unexpected happening during the reading of the source images, or the files should be written as expected.
See Also
Categories
Find more on Image Segmentation and Analysis 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!