我使用以下MATLAB代码裁剪文件夹中的所有图像,它工作得很好,但问题是它使用相同的坐标裁剪所有图像,我想要的是用特定坐标裁剪每个图像。图像的坐标按顺序存在于表中,例如图像1的坐标在行1中,依此类推。 这是获取表格中坐标的代码:
filename = 'fich.txt';
fid = fopen(filename);
A = textscan(fid, '%*s%*s%*s%*s%f%f%*f');
res = cell2mat(A);
res(isnan(res))=300;% remplacer les valeurs "NAN" dans la matrice par la valeur 300
fclose(fid);
以下是使用相同坐标裁剪所有图像的代码:
% operating all pictures in a folder
clear all
close all
clc
dname_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')
break
end
a = a+1 ;
end
谢谢