How to save segmented images into different folders?

I have written a code to segment some images and save those images into different folders.
Program should work like below:
  • Image 01 --> Segmentation --> Save segmented images into first folder & so on.
But my problem is;
  • The segmented images are getting saved into only one folder.
I want some help to save the segmented images into different folders.
Please help.
Thanks!

 채택된 답변

Guillaume
Guillaume 2018년 11월 8일
편집: Guillaume 2018년 11월 8일
Well, clearly the code you wrote has a bug but it's hard for us to tell you the problem if you don't show us the code.
It is trivial to save an image in whatever folder you want. The simplest way to do that:
destinationfolder = 'C:\somewhere\somefolder'; %folder path generated however you want
imwrite(yourimage, fullfile(destinationfolder, 'nameofimage.png'));

댓글 수: 5

Guillaume
Guillaume 2018년 11월 8일
편집: Guillaume 2018년 11월 8일
The code you show will work as you intend. However, it can't be the full code you're using (since there's nothing in there about loading the images, or the calculation of your labeling matrix). Perhaps, the problem comes from the parts you haven't shown.
I would recommend you create a full path for your outputFolder. At the moment, all your folders are relative to the current directory. If you change the current directory in the code you haven't shown, then there'll be a problem.
@MD: today I formatted your code correctly for you. In future you can do it yourself:
  • do not add an empty line between each code line.
  • align your code consistently.
  • select the code text, then
  • press the {} Code button.
@Stephen:
Thank you for that.
With the full code, the problem is obvious. I would expect that when you run your code you get repeated warnings from mkdir that folders already exist. The important bits of your code summarised to show the problem:
for each file (j loop)
do some processing
for each file (k loop)
create destination directory
assign destination directory to outputfoldername
end of k loop, outputfolde is thus the directory of the last file
save images of file j into outputfolder
end of j loop
The fix is simple replace the whole k loop by:
outputFolder = sprintf('Cropped Images%d', j);
mkdir(outputFolder);
As I mentioned before, I would recommend you use full path for your output folder rather than relative path. You already use full paths for your input folders. Note that fullfile is safer than strcat to build paths.
@Guillaume
Thank you so much for the support. It works perfectly now, as i want.
And also thank you for explaining the problem clearly. Now only i understood what i have done wrong. I will do other modifications also, as you recommended to me.
Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

질문:

2018년 11월 8일

편집:

2019년 1월 3일

Community Treasure Hunt

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

Start Hunting!

Translated by