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
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.

 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

More Answers (2)

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

You should avoid dynamic growing of variables. The code below will also generate the temp array, but doesn't resize the matrix every iteration.
images=uint8(randi(255,[4 4 10]));%example array
for k=1:(size(images,3)-2)
temp=images(:,:,k+[0 1 2]);
% temp is the expected image at each iteration.
end
Your code also ignores the reading stage (and probably conversion to greyscale), and has an unnecessary use of clc and clear. Nothing is being printed to the command window, and no variables are left un-preallocated.
Your use of the randi function could also be confusing: the first input is the maximum value, the second input is the size of the first dimension. If a third input is missing, a square matrix will be generated. Your comment looks like randi(4,5) should return a 4 by 5 matrix.
what does the 255 means?
Hi Rik,
Thanks for your catch. I have put the code in very basic and explanatory way so that the user could understand clearly on what's goin on in each step. I also assumed that the images won't be limited to 10, so instead of recreating temp I preferred dynamic allocation.
@khaled alsaih , here 255 is the limit for the randi to generate numbers. Refer the following link for more information
Rik
Rik on 21 Oct 2019
I chose to put 255 as the maximum value and convert it to uint8, because that is the format that you will have when you read a jpg file with imread. Apart from those two points, my code is the exact equivalent in functionality as the code in the answer by Ganesh.
but guys why there is no imread to read images
i mean why are generating randi not using imread?
khaled alsaih
khaled alsaih on 21 Oct 2019
Edited: Rik on 21 Oct 2019
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
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.
khaled alsaih
khaled alsaih on 21 Oct 2019
Edited: khaled alsaih on 21 Oct 2019
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
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
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.

Community Treasure Hunt

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

Start Hunting!