Applying transformations to a group of images in a folder
2 views (last 30 days)
Show older comments
Dear fellow researchers, I am proposing to apply image transformation operators (translations, random cropping, rotations etc.) to a set of 100 images (in .bmp format) in a folder named "1" in D:\. After applying individual transformations, I want these images to be stored in different folders (each corresponding to a particular transformation) in the same D:\ (working directory). I have written a few lines of code for this:
myFolder = 'D:\1';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage)); return;
end
filePattern = fullfile(myFolder, '*.bmp');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
imageArray = imread(fullFileName);
imageArray1=imtranslate(imageArray,[10,10]);
mkdir('chrom-translate1')
imwrite(imageArray1,'chrom-translate1\image(k).bmp');
%rotation
deg = -30;
imageArray2 = imrotate(imageArray, deg, 'bilinear', 'crop');
mkdir('chrom-rotate1')
imwrite(imageArray2,'chrom-rotate1\image(k).bmp')
%imageprocessing-flipping the columns
imageArray3 = flipdim(imageArray,2); %# Flips the columns, making a mirror image
mkdir('chrom-flipcol1')
imwrite(imageArray3,'chrom-flipcol1\image(k).bmp')
% imageprocessing-flipping the rows
imageArray4 = flipdim(imageArray,1); %# Flips the rows, making an upside-down image
mkdir('chrom-flipupdown1')
imwrite(imageArray4,'chrom-flipupdown1\image(k).bmp')
% image processing-random cropping of a 100*100 image from the input image
imageArray5=random_square_grab(imageArray,100);
mkdir('chrom-randcrop1')
imwrite(imageArray5,'chrom-randcrop1\image(k).bmp')
end As I execute this program in Matlab, I face the following issues: Only the first image in the folder "1" is undergoing the transformations and getting stored in the respective folders I have made. I expected the images to carry the name format as "image(1), image(2), ..., image(100) (with k=100), being stored in the respective folders, but only the first image is getting stored with the name "image(k)" in the folders after undergoing the transformations, but the remaining images are not at all processed. Also, the program issues a warning that the folders already exist. Kindly help me restructure the code.
0 Comments
Answers (1)
Image Analyst
on 26 Jan 2017
Edited: Image Analyst
on 26 Jan 2017
You can't do things like this:
imwrite(imageArray4,'chrom-flipupdown1\image(k).bmp')
You need to construct the filename from k, otherwise you'll get exactly 'chrom-flipupdown1\image(k).bmp' with the (k) in the filename itself, and all filenames will be the same since k is not replacing anything in the filename string.
Do this:
baseOutputFileName = sprintf('chrom-flipupdown1\image(%d).bmp', k);
fullOutputFileName = fullfile(outputFolder, baseOutputFileName);
imwrite(imageArray4, fullOutputFileName)
Be sure to define outputFolder in advance, even if it's simply pwd (the current folder).
4 Comments
Image Analyst
on 27 Jan 2017
Edited: Image Analyst
on 27 Jan 2017
Learn how to step through it with the debugger to figure out why the for loop never terminates and what statement it hangs up on.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!