cropping all images in a folder with different coordinates

3 visualizaciones (últimos 30 días)
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 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
thank you

Respuesta aceptada

Image Analyst
Image Analyst el 22 de Dic. de 2013
Get the values from your table and use them in imcrop. Let's say the table is called croppingRects.
thisCroppingRect = croppingRects(rowNumber, :);
Then pass it into imcrop:
% Instead of
% I2 = imcrop(I,[189.5 1.5 588 1022])
% Do this:
I2 = imcrop(I, thisCroppingRect)
By the way, please don't use I as a variable. It looks too much like 1 and l.
  4 comentarios
Image Analyst
Image Analyst el 28 de En. de 2019
Yes. Start a new question with whatever code you have so far, and define what "the rest" is.
Anounymous85
Anounymous85 el 29 de En. de 2019
@Image Analyst: I have started a new question.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Image Processing Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by