MATLAB Answers

0

concat images in order

Asked by khaled alsaih on 18 Oct 2019
Latest activity Commented on by khaled alsaih on 23 Oct 2019 at 18:36
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

Rik
on 18 Oct 2019
How are the image currently stored and what did you try so far?
Stored in jpg format and I could do Concat(image,image,image) But wanted instead of concating the same image 3 times I want to contact three consecutive images All images released are in 3 D format before I extracted them to 2d images

Sign in to comment.

3 Answers

Image Analyst
Answer by Image Analyst
on 22 Oct 2019
 Accepted Answer

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

  1 Comment

khaled alsaih on 23 Oct 2019 at 18:36
thanks man it works very well

Sign in to comment.


Answer by 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

i have tried this so far
but it is not working
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);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(folder, baseFileName);
imageArray = imread(fullFileName);
images=uint8(randi(255,[512 512 length(theFiles)]));%example array
for i=1:(size(imageArray,3)-2)
temp=imageArray(:,:,i+[0 1 2]);
% temp is the expected image at each iteration.
end
base = sprintf('Slice%d.png', k);
full = fullfile(dest, base);
imwrite(imageArray, full);
end
the image generated just 512*512 where it should be 512*512*3
plus number of files should be reduced with two so if i have 10 images the code shall generate only 8 cause the first output is 1st image +2nd image + 3 image (stored in one image) then 2nd image + 3 image + 4 th image (stored in second image) and so on till the 8th image + 9th image + 10th image (stored in the 8th image)
anyway i really apperciated your efforts :)
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.
yeah true but seems that your code just writing the same images in the form of 512*512 but not the form of 512x512x3

Sign in to comment.


Rik
Answer by 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

thanks friend for your help
the code shows no error but there is no single image was written to the Patient_2 folder
or should i modify this line
imageArray(1,1,length(theFiles)) = 0;%extend to full array size
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.

Sign in to comment.