Cropping an image using extents in stored variables

1 view (last 30 days)
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.

Accepted Answer

Image Analyst
Image Analyst on 20 Jan 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 Comments
10B
10B on 22 Jan 2016
Yes, I had already done that. I don't know the reason why, but I think that its behaving this way as there are several variables already stored in the workspace that are of the same format ie x1234, x4321, x1243 etc. and only one image is read in.
I have subsequently moved all the variables/images into individual folders and wrapped your script (and my parts) into a function and it works fine, but when there were all the variables in the same place - 'theRect' output the answer based on the first x1234 variable only. Everything seems right with that code - I just can't understand why the problem appeared the way it did. Is it something to do with the 'sprintf' function and how it generates the xy for 'theRect' formula?
Never mind though, I think I have sorted a workaround by the individual filing - it was just a little manual and took some time, but its there now.
Thanks again for your help.
Image Analyst
Image Analyst on 22 Jan 2016
No, that wouldn't matter - the other variables are simply ignored. Well, anyway, glad it's solved, and thanks for Accepting.

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!