Trouble importing file of image to run through function, then combining/exporting as a file

1 次查看(过去 30 天)
I am writing a function that takes an images and converts it to BW. I would like to make my function so that it takes in a file of images, runs through the function (making every image BW), and combines the new BW images into a new file. What should I do?
Here is my working BW function MidAirBW:
midAirImage = imread("MidAirTest.jpg");
midAirImage = rgb2gray(midAirImage);
bwImage = imbinarize(midAirImage,'adaptive','ForegroundPolarity','dark','Sensitivity',0.4);
imshow(bwImage);
This is what I tried for the file function:
D = 'Video2 gif stack';
S = dir(fullfile(D, 'name*.jpg'))
for k = 1:numel(S)
F = fullfile(D, S(k).name);
I = MidAirBW(F)
imwrite(I, 'BW edited images')
end

回答(1 个)

Raunak Gupta
Raunak Gupta 2020-8-12
Hi Ashley,
I assume by combining you means saving the images with distinct name into the same folder or different folder. You can probably rename the files with string like “_BW_edit” to every image name last part. Below code might help.
for k = 1:numel(S)
F = fullfile(D, S(k).name);
I = MidAirBW(F);
imwrite(I, strcat(F(1:end-4),'_BW_edit.jpg'));
end

Community Treasure Hunt

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

Start Hunting!

Translated by