Function with multiple inputs

7 views (last 30 days)
Hello everyone
I have a function with two inputs
function [output_image]=multiple(input_1,input_2)
assume
input_1=imread('image from input folder_1')
input_2=imread('image from input folder_2 ')
output_image=input_1+input_2 % assume this as preprocessing stage
%i need to stire all the images after prerocessig in folder
%i have 90 images in folder_1 and 90 images in folder_2 and this images
%should be read in sequence
%example like folder_1 : 1st image
% folder_2=first image
end
please help me to write a loop
  3 Comments
Walter Roberson
Walter Roberson on 12 Sep 2021
Okay, so given the name in one folder, how do you know which name in the other folder is the corresponding image ?

Sign in to comment.

Accepted Answer

Prateek Rai
Prateek Rai on 14 Sep 2021
To my understanding, you have 2 folders with same number of images and you want to preprocess the images and use the final output image. First of all you can make a hierarchy of files in the following way.
  • folder_1 contains 90 images
  • folder_2 contains 90 images
  • folder_3 (empty folder to save output image)
Now you can use imageDatastore function to read all images from folder_1 and folder_2.
imds1 = imageDatastore('folder_1');
imds2 = imageDatastore('folder_2');
Number of images in folder can be retrieved using:
l = length(imds1.Files);
Now you can run a loop from i=1 to length and read images from folder_1 and folder_2, make the required calculations and finally write the new preprocessed image in folder_3.
for i=1:1:l
% reading images one by one from folder_1 and folder_2
img1 = imread(imds1.Files{i});
img2 = imread(imds2.Files{i});
out_img = img1+img2; % preprocessing stage
% this is important to create a location where final image can be saved
% You can change '.jpg' with the file format your images have
output_loc = "folder_3\image" + num2str(i) + ".jpg";
% It will save the prepocessed image in the location specified by output_loc
imwrite(out_img,output_loc)
end
Here is a full working function for your problem:
imds1 = imageDatastore('folder_1');
imds2 = imageDatastore('folder_2');
l = length(imds1.Files);
for i=1:1:l
% reading images one by one from folder_1 and folder_2
img1 = imread(imds1.Files{i});
img2 = imread(imds2.Files{i});
out_img = img1+img2; % preprocessing stage
% this is important to create a location where final image can be saved
% You can change '.jpg' with the file format your images have
output_loc = "folder_3\image" + num2str(i) + ".jpg";
% It will save the prepocessed image in the location specified by output_loc
imwrite(out_img,output_loc)
end
You can refer to imageDatastore MathWorks documentation page to find more on Datastore for image data.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!