Stroing an image with different names from a loop

9 ビュー (過去 30 日間)
Sreejoe Kaniyamparambil
Sreejoe Kaniyamparambil 2017 年 5 月 18 日
コメント済み: Jan 2017 年 5 月 18 日
Hello everyone, its me... again. So I have a loop. This loop takes an image crops it and creates two gradient images. These images are stitched together as one image. I would like to save this image at a specific path. Since the images are created through a loop, I need to write the store command into the loop. I did this using 'imwrite' . The problem I have is to name each file differently. I tried using sprintf within the imwrite code like this :
%%Display Image
image_final=cat(2,im2uint8(image_bin_grad_mask),image_cropped,im2uint8(image_dil_grad_mask));
imshow(image_final)
%%Store Image
imwrite(image_final,sprintf('C:\Users\ska\Desktop\Test Gradient Images\%dframe.tif',i),'tif');
end
This does not work though. Do you have any suggestions? Your help, time and efforts are much appreciated.

回答 (2 件)

Cam Salzberger
Cam Salzberger 2017 年 5 月 18 日
編集済み: Cam Salzberger 2017 年 5 月 18 日
Hello Sreejoe,
If you run just part of your code with a sample "i" value:
sprintf('C:\Users\ska\Desktop\Test Gradient Images\%dframe.tif',4)
You might notice that you get a warning:
Warning: Escaped character '\U' is not valid. See 'doc sprintf' for supported special characters.
This should give you a clue that sprintf('C:\Users...') isn't quite doing what you expect it to. The backslash character is special in "sprintf", just like the percent sign, and the single quote in any character vector. If you want the output character vector to have a literal backslash, you have to "escape" it first:
sprintf('C:\\Users\\ska\\Desktop\\Test Gradient Images\\%dframe.tif',4)
Other than that, I didn't see any issues with the code.
-Cam
  1 件のコメント
Steven Lord
Steven Lord 2017 年 5 月 18 日
Alternately, to avoid having to double up the backslash characters, only use sprintf on the piece that includes the format string and glue the "constant" part together with the varying part using fullfile.
thename = sprintf('%dframe.tif', 4);
thepath = 'C:\Users\ska\Desktop\Test Gradient Images';
thefullpath = fullfile(thepath, thename);
Note that while I defined all three of these variables together, if you're using a loop only thename and thefullpath depend on the loop variable. So you can define thepath once outside the loop.

サインインしてコメントする。


KSSV
KSSV 2017 年 5 月 18 日
I hope this pseudo code helps...
path = pwd ; % present directory, you put your path
for i = 1:10
I = rand(100) ;
name = strcat(path,'\',num2str(i),'.jpg') ;
imwrite(I,name)
end
  1 件のコメント
Jan
Jan 2017 年 5 月 18 日
Using the name of the very important builtin function "path" as name of a variable can cause strange effects during debugging. fullfile is smarter than strcat when constructing file names, because it considers the file separators automatically.

サインインしてコメントする。

カテゴリ

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by