Concatenate series of images from different folders
2 次查看(过去 30 天)
显示 更早的评论
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);
2 个评论
Adam
2016-7-15
Have you debugged to check the rest of it works as expected?
e.g. you put 'foler2' instead of 'folder2' so your error may simply be because you have an empty list of files so no convolution happens.
回答(2 个)
Azzi Abdelmalek
2016-7-15
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)
0 个评论
Walter Roberson
2016-7-15
编辑:Walter Roberson
2016-7-15
%%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 个评论
Azzi Abdelmalek
2016-7-18
Repmove one bracket, instead of
oriList = dir(fullfile(oriDir, '*.jpg'));
it should be
oriList = dir(fullfile(oriDir, '*.jpg');
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!