Applying transformations to a group of images in a folder

2 次查看(过去 30 天)
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.

回答(1 个)

Image Analyst
Image Analyst 2017-1-26
编辑:Image Analyst 2017-1-26
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 个评论
shivasakthi
shivasakthi 2017-1-27
Hi, I have now revised the code by changing the 'directory-name/image(%d).bmp') syntax in the above program. I am not copying the entire program here, due to space constrains. As I run the code, I encounter the following issues:
My folder in 'D:\1' has 3 images, for instance, to which I am applying these geometric transformations. As I run the code, I am getting only the first image named 1(1) getting computed and stored in the declared five folders (for five different geometric transformations). Also, the for loop doesn't terminate and the program remains busy. Kindly help me in redesigning this code so that I should get all the three images in D:\1, transformed and stored in their respective declared folders. The loop has to exit and I need to view the contents in the directories I have declared. Kindly help with the code lines for this as well. Thank you so much.
Image Analyst
Image Analyst 2017-1-27
编辑:Image Analyst 2017-1-27
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.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by