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

1 view (last 30 days)
Ashley Salisbury
Ashley Salisbury on 22 Jul 2020
Answered: Raunak Gupta on 12 Aug 2020
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

Answers (1)

Raunak Gupta
Raunak Gupta on 12 Aug 2020
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