Stroing an image with different names from a loop

8 visualizaciones (últimos 30 días)
Sreejoe Kaniyamparambil
Sreejoe Kaniyamparambil el 18 de Mayo de 2017
Comentada: Jan el 18 de Mayo de 2017
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.

Respuestas (2)

Cam Salzberger
Cam Salzberger el 18 de Mayo de 2017
Editada: Cam Salzberger el 18 de Mayo de 2017
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 comentario
Steven Lord
Steven Lord el 18 de Mayo de 2017
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.

Iniciar sesión para comentar.


KSSV
KSSV el 18 de Mayo de 2017
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 comentario
Jan
Jan el 18 de Mayo de 2017
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Geometric Transformation and Image Registration en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by