Concatenate series of images from different folders
Show older comments
I was trying to achieve following functionalities:
extract pic1 from folder2 and put it to the left part of the newImg, extract pic2 from folder2 and put it to the right part of the newImg, Then combine the left part and right part together to be a new image.
My question is I want to implement this function for a series of images, which means a series of images from folder1 and a series of images from folder2, then extract all the images from folder1, put it to the left part of the newImg; same for folder 2, extract all the images from folder2 and put it the right part of the newImg.
I have implemented to read series of images from different folders, but when trying to concatenate the images, matlab throws an error saying unexpected expression.
Please check my code below: ==========================================================================
clc;
clear all;
close all;
%%Read images from folder1
oriDir = '/home/folder1/';
oriList = dir(fullfile(oriDir,'*.jpg'));
%read images from folder2
convDir = '/home/folder2/';
convList = dir(fullfile(convDir,'*.bmp'));
for i = 1:length(oriList)
oriPath = [oriDir oriList(1).name];
oriImg = imread(oriPath);
end
for j = 1:length(convList)
convPath = [convDir convList(1).name];
convImg = imread(convPath);
end
newImg = cat(2,oriImg,convImg);
newImg = uint8(newImg);
imshow(newImage);
Answers (2)
Azzi Abdelmalek
on 15 Jul 2016
You should replace
oriPath = [oriDir oriList(1).name]
convPath = [convDir convList(1).name];
by
oriPath=fullfile(oriDir,oriList(1).name)
convPath =fullfile(convDir,convList(1).name)
Walter Roberson
on 15 Jul 2016
Edited: Walter Roberson
on 15 Jul 2016
%%Read images from folder1
oriDir = '/home/folder1/';
oriList = dir(fullfile(oriDir, '*.jpg'));
num_ori = length(oriList);
%read images from folder2
convDir = '/home/folder2/';
convList = dir(fullfile(convDir, '*.bmp'));
num_conv = length(convList);
numfile = min(num_ori, num_conv);
oriImg = cell(numfile, 1);
for i = 1 : numfile
ori_name = oriList(i).name;
oriPath = fullfile(oriDir, ori_name);
ori_img = imread(oriPath);
oriImg{i} = ori_img;
conv_name = convList(i).name;
convPath = fullfile(convDir, conv_name);
conv_img = imread(convPath);
convImg{i} = conv_img;
if size(ori_img}, 1) == size(conv_img, 1) && size(ori_img, 3) == size(conv_img, 3)
newImg{i} = cat(2, ori_img, conv_img);
imshow(newImg{i});
title( sprintf('"%s" with "%s"', ori_name, conv_name) );
pause(1);
else
fprintf('ori "%s" is incompatible size [%d,%d,%d] with conv "%s" [%d,%d,%d]', ...
ori_name, size(ori_img,1), size(ori_img,2), size(ori_img,3), ...
conv_name, size(conv_img,1), size(conv_img,2), size(conv_img,3) );
newImg{i} = [];
imshow(0);
title( sprintf('"%s" incompatible with "%s"', ori_name, conv_name) );
end
end
4 Comments
awa
on 18 Jul 2016
Walter Roberson
on 18 Jul 2016
If there are not the same number of ori files as there are conv files, then if you assume they are the same you are going to try to match a non-existant file against an existing file. The number of pairs that you can be sure you can match is the minimum of the number of files on each side. For example, if the search finds 7 ori files and only 5 conv files then there can be only min(7,5) = 5 outputs.
awa
on 18 Jul 2016
Azzi Abdelmalek
on 18 Jul 2016
Repmove one bracket, instead of
oriList = dir(fullfile(oriDir, '*.jpg'));
it should be
oriList = dir(fullfile(oriDir, '*.jpg');
Categories
Find more on Deep Learning Toolbox 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!