How can i resize an image to a specified size? - perl

Actually, i am shrinking high qualitiy images. I need to have one parameter (width or heigth) fixed and the other is flexible but with a defined minimum.
I want to keep the widht/height - ratio of the image.
Example:
I have an image (width x heigth) = 2000px x 3000px and i want it to shrink to a width of 968px and a minimum height of 640px while keeping the widht/height - ratio of the image.
Using the imagemagick Perl API, what do i need to issue to shrink such an image?
So far i have used this, but the results were only some white images:
my $image = Image->new();
$image->Read('my_2000x3000_image.jpg');
$image = $image->[0];
$image->Resize('geometry' => '968' . 'x' . '>');
$image->Write('image_968_min_640.jpg');

What you need is Image::Magick and the Scale method, which takes a maximum width and height
The following creates a thumbnail from an existing image:
# Thumbnail Dimensions
my ($max_height, $max_width) = (60,60);
my $thumbImage = new Image::Magick;
$thumbImage->Read($oldfile);
$thumbImage->Scale(geometry => qq{${max_height}x${max_width}});
$thumbImage->Write($newfile);

Related

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 without just resizing it [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.
I didn't take into account that the height and width for some scales would NOT be whole integers. Since Matlab cannot crop images that are inbetween whole pixel numbers, the "Expected input to be integer-valued." popped up.
I solved my issue by using Math.floor() on the 'scaled_width' and 'scaled_height' variables.

Resize an image for Histogram of Gradient

I have an image in size of 150 pixel in height and 188 pixel in width. I'm going to calculate HOG on this image. As this descriptor needs the size of detector window(image) to be 64x128, Should I resize my image to 64x128 and then use this descriptor? like this :
Image<Gray, Byte> GrayFrame = new Image<Gray, Byte>(_TrainImg.Size);
GrayFrame = GrayFrame.Resize(64, 128, INTER.CV_INTER_LINEAR);
I concern resizing may change the original gradients orientation depending on how it is resized since we are ignoring the aspect ratio of the image?
By the way, The image is croped and I can't crop it anymore. It means this is the size of image after cropping and this is my final bounding box.
Unfortunately the openCV HoGDescriptor documentation is missing.
In openCV you can change the values for detection window, cell size, blockStride and block size.
cv::HOGDescriptor hogDesc;
hogDesc.blockSize = cv::Size(2*binSize, 2*binSize);
hogDesc.blockStride = cv::Size(binSize, binSize);
hogDesc.cellSize = cv::Size(binSize, binSize);
hogDesc.winSize = cv::Size(imgWidth, imgHeight);
Then extract features using
std::vector<float> descriptors;
std::vector<cv::Point> locations;
hogDesc.compute(img, descriptors, cv::Size(0,0), cv::Size(0,0), locations);
Note:
I guess, that the winSize has to be divisible by the blockSize and the blockSize by the cellSize.
The size of the features is dependent on all these variables, so ensure to use images of same size and do not change the settings to not run into trouble.

Matlab imresize function rounding up pixels

I'm looking to take in an image of 162x193 pixels and basically scale it down by 0.125 i.e 162/8 = 20.25 and 193/8 = 24.125. Thus I would like a picture of size 20x24 The only problem I'm currently having is that when I use the imresize function it rounds up the images pixel values i.e I get an image of size 21x25 instead of 20x24. Any way of getting 20x24 or is this problem something I'm going to have to live with? Here is some code:
//Read in original Image
imageBig = imread(strcat('train/',files(i).name));
//Resize the image
image = imresize(imageBig,0.125);
disp(size(image));
It appears that with the scale argument being provided, imresize ceils up the dimensions as your results show. So, I guess an obvious choice is to manually provide it the rounded values as dimensions.
Code
%%// Scaling ratio
scale1 = 0.125;
%%// Get scaled up/down version
[M,N,~] = size(imageBig);
image = imresize(imageBig,[round(scale1*M) round(scale1*N)]);

Image size with respect to pixels

I want to increase the image size with respect to pixels, that is a image of size 150x225 should be changed to 250x250. How can I do that in Matlab?
You can use the matlab function imresize.
e.g. B = imresize(A, [250 250]);
where A is your initial image with size (150x225).

getting gtkimage width and height

How can i get the GtkImage width and height after reading from a file.
If you want the size of the actual image, you need to go through the GdkPixbuf API:
const GdkPixbuf *pb = gtk_image_get_pixbuf(GTK_IMAGE(image));
printf("image is %ux%u pixels\n", gdk_pixbuf_get_width(pb),
gdk_pixbuf_get_height(pb));
Note that this size is not the same as the size of the GtkImage widget that shows the image, since the widget can be given a different size by GTK+'s layout engine.
GtkImage is a subclass of GtkWidget. You should be able to use gtk_widget_get_allocation, and use the height and width fields of the GtkAllocation * that it returns.