concat images in order
Show older comments
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?
khaled alsaih
on 18 Oct 2019
Accepted Answer
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
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.
khaled alsaih
on 21 Oct 2019
Ganesh Regoti
on 21 Oct 2019
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
khaled alsaih
on 21 Oct 2019
khaled alsaih
on 21 Oct 2019
Edited: Rik
on 21 Oct 2019
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
on 21 Oct 2019
Edited: khaled alsaih
on 21 Oct 2019
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
khaled alsaih
on 21 Oct 2019
khaled alsaih
on 21 Oct 2019
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.
Categories
Find more on Matrix Indexing 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!