Function with multiple inputs

3 次查看(过去 30 天)
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 个评论
Poreddy ajaykumar reddy
@Walter Roberson they have different names and they are from different folders
extension is same
Walter Roberson
Walter Roberson 2021-9-12
Okay, so given the name in one folder, how do you know which name in the other folder is the corresponding image ?

请先登录,再进行评论。

采纳的回答

Prateek Rai
Prateek Rai 2021-9-14
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Image Processing and Computer Vision 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by