Cropping an image using extents in stored variables
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
10B
el 20 de En. de 2016
Comentada: Image Analyst
el 22 de En. de 2016
Hello Community,
As the title says, I am trying to crop images using the axis extents which are already stored in individual variables. The variables refer to the 'baseFileName' for the image eg IMG_1234.jpg, so the corresponding variable is called 'x1234' (Matlab put the x there when I imported them in...). In each variable is X1, X2, Y1, Y2 eg [1600;3250;1600;2800].
My script will be run several times, so I want the script to be able to automatically look up the variable (and then the X Y data) needed each time the corresponding image file is loaded. The X Y will then be used to 'clip' the image to the area of interest.
This code is used to extract the image number from the original file name:
ImgName = regexpi(baseFileName, '(?<=IMG_)\d+', 'match', 'once');
A RGB image is loaded, which is then converted to grayscale for indexing purposes:
I = rgb2gray(RGB);
and at this point, I want to be able to look up the variable 'xImgName' eg 'x1234' and extract the axis extents to create a new image 'idx', I think, something similar to this:
idx = imcrop(I,[Y1 Y2 X1 X2] 1);
I am struggling with the bits in-between ie getting the code to look-up the right variable relative to the filename, and then getting the crop to be done to the size I need.
As I said before, the script will be run more than once on different image file names, so hard coding this is not going to work.
Could anyone offer any ideas as to how to tackle this? Thanks in advance.
Regards,
10B.
0 comentarios
Respuesta aceptada
Image Analyst
el 20 de En. de 2016
Try this:
% Assign one of a bunch of x variables that the other script makes.
x1234 = [1600;3250;1600;2800]
filename = 'IMG_1234.jpg';
rgbImage = imread(filename);
% The corresponding variable is called 'x1234' (Matlab put the x there when I imported them in...).
% In each variable is X1, X2, Y1, Y2 eg [1600;3250;1600;2800].
% Find the number as the 4 characters before .jpg.
dotLocation = strfind(filename, '.jpg');
% Get the number as a string
idNumber = filename(dotLocation-4:dotLocation-1)
% Get the cropping rows and columns from the variable
commandString = sprintf('xy = x%s', idNumber)
% Execute it with eval
eval(commandString)
% Turn it into a rect format
theRect = [xy(1), xy(3), abs(xy(2)-xy(1)), abs(xy(4)-xy(3))]
% Now do the cropping into a image badly-named "idx"
idx = imcrop(rgbImage, theRect);
Obiously, you'll need to comment out or adapt the first 3 lines. But basically you're finding the right variable in your workspace based on the name of the image file you've read in. Then it takes that variable and turns it into an [xLeft, yTop, width, height] format which is what imcrop() wants.
I'd choose a different name for the cropped image than idx, which sounds more like indexes rather than an image.
4 comentarios
Image Analyst
el 22 de En. de 2016
No, that wouldn't matter - the other variables are simply ignored. Well, anyway, glad it's solved, and thanks for Accepting.
Más respuestas (0)
Ver también
Categorías
Más información sobre Image Processing Toolbox en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!