Dynamically setting a 'targetSize' for centerCropWindow2d()

Following the example from the documentation page of the centerCropWindow2d function, I am trying to dynamically crop an image based on a 'scale' value that is set by the user. In the end, this code would be used in a loop that would scale an image at different increments, and compare the landmarks between them using feature detection and extraction methods.
I wrote some test code to try and isolate 1 instance of this user-specified image cropping,
file = 'frameCropped000001.png';
image = imread(file);
scale = 1.5;
scaled_width = scale * 900;
scaled_height = scale * 635;
target_size = [scaled_width scaled_height];
scale_window = centerCropWindow2d(size(image), target_size);
image2 = imcrop(image, scale_window);
figure;
imshow(image);
figure;
imshow(image2);
but I am met with this error:
Error using centerCropWindow2d (line 30)
Expected input to be integer-valued.
Error in testRIA (line 20)
scale_window = centerCropWindow2d(size(image), target_size);
Is there no way to do use this function the way I explained above? If not, what's the easiest way to "scale" an image around its center point [that is, if I scale it by 0.5, the image stays the same size but is zoomed in by 2x]?
Thank you in advance.

Réponses (1)

Is there no way to do use this function the way I explained above?
scale = 1.5
scale = 1.5000
scaled_width = scale * 900
scaled_width = 1350
scaled_height = scale * 635
scaled_height = 952.5000
You are asking the function to produce an output that is 952 1/2 pixels high, but MATLAB has no way to represent storing half of a pixel. You need to floor() or ceil() the values, depending whether you want 952 or 953 pixels for the result.

1 commentaire

Totally didn't think of the values of height in width that weren't whole numbers. That solved my issue; thank you very much!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Images dans Centre d'aide et File Exchange

Produits

Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by