concat images in order

조회 수: 24 (최근 30일)
khaled alsaih
khaled alsaih 2019년 10월 18일
댓글: khaled alsaih 2019년 10월 23일
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
Rik
Rik 2019년 10월 18일
How are the image currently stored and what did you try so far?
khaled alsaih
khaled alsaih 2019년 10월 18일
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

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2019년 10월 22일
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
khaled alsaih
khaled alsaih 2019년 10월 23일
thanks man it works very well

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Ganesh Regoti
Ganesh Regoti 2019년 10월 21일
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
Rik
Rik 2019년 10월 21일
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 2019년 10월 21일
편집: khaled alsaih 2019년 10월 21일
yeah true but seems that your code just writing the same images in the form of 512*512 but not the form of 512x512x3

댓글을 달려면 로그인하십시오.


Rik
Rik 2019년 10월 21일
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
khaled alsaih
khaled alsaih 2019년 10월 21일
or should i modify this line
imageArray(1,1,length(theFiles)) = 0;%extend to full array size
Rik
Rik 2019년 10월 21일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by