Function with multiple inputs
조회 수: 1 (최근 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
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
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!