i use the following MATLAB code to crop all images in a folder, it works perfectly but the problem is that it crops all the images with the same coordinates, what i want is to crop each image with specific coordinate. the coordinates of the images exist in a table in order, for example the coordinates of the image 1 are in row 1 and so on. here is the code to get the coordinates in a table:
filename = 'fich.txt';fid = fopen(filename);A = textscan(fid, '%*s%*s%*s%*s%f%f%*f');res = cell2mat(A);res(isnan(res))=300;fclose(fid)
and here is the code that cropps all images with the same coordinates:
% operating all pictures in a folder
clear allclose allclcdname_open = ('myfold') ; %Default Directory To be Opened
dname_save = ('myfold-cropped') ;test = 0 ; % 0 - does every file in folder, 1 - only does first file
%%Set up basic file name path to read
top_file= [dname_open '\'] ; %Set up main database to open and look inside
ls_top_file = ls(top_file) ; %List Files inside main folder
c = cellstr(ls_top_file) ; %Turn cells from ls function into strings
cc = c(3:length(c)) ; %Set up a matrix without the . and .. produces by the ls function
S = size(cc) ; %Find the size of matrix containing names of files inside of main database
a = 1 ; %This counter is set to 3 to account for the . and .. at the beggining of each matrix created by ls
S(1)while a <= S(1) close all file = char(cellstr([top_file char(cc(a))])) ; %File to be operated on
data_n = char(cc(a)) file_name = char(cc(a)) ;% Operations on files in folder
imagename = (file_name) ;%Input image image
fileToRead2 = [dname_open '\' imagename] ;I = imread(fileToRead2);%I2 = imcrop(I, rect) crops the image I. rect is a four-element position
%vector[xmin ymin width height] that specifies the size and position of the crop rectangle.
I2 = imcrop(I,[189.5 1.5 588 1022]);imshow(I2,'Border','tight')set(gcf, 'PaperPositionMode', 'auto');h = gcf ;saveas(h, [dname_save '\' 'z_' imagename ], 'pgm');if test == 1 fprintf('breaking loop to set axis - test==1') breakend a = a+1 ; end
thank you
Best Answer