How to make two images with same dimensions to be same size in matlab - image

I have two images with same dimensions but with different sizes. I want the bigger image's size to be changed to match the smaller image's size.
For example, I have an image A with size 300x400x3 and image B with size 600x800x3. I want to change the size of image B to 300x400x3. Thanks for any help.

Just one-liner as suggested by Andrey -
B = imresize( B,[size(A,1) size(A,2)]);
For accessing more options with resizing, use this resource.

Related

Shoud I resize image for mask RCNN?

I am training custom object detection by using mask RCNN. I have custom images that are of different sizes, so I am wondering if I need to resize the images so that they are all of the same size or not?
And if so, which method should I use to resize them?
Also I guess that I have to resize before labeling the images right?
You don't necessarily have to resize it before hand.
you can use this option in the model config file to set the size limit for your training.
image_resizer {
keep_aspect_ratio_resizer {
min_dimension: 600
max_dimension: 1024
}
}
Please make sure all the bounding boxes are in range with the image dimensions. i.e. the within the range of width and height of the image. Then the boxes and the images will be auto resized according to the parameter set here.
In Matterplot's Mask RCNN you can find documentation in the config file:
# Input image resizing
# Generally, use the "square" resizing mode for training and predicting
# and it should work well in most cases. In this mode, images are scaled
# up such that the small side is = IMAGE_MIN_DIM, but ensuring that the
# scaling doesn't make the long side > IMAGE_MAX_DIM. Then the image is
# padded with zeros to make it a square so multiple images can be put
# in one batch.
# Available resizing modes:
# none: No resizing or padding. Return the image unchanged.
# square: Resize and pad with zeros to get a square image
# of size [max_dim, max_dim].
# pad64: Pads width and height with zeros to make them multiples of 64.
# If IMAGE_MIN_DIM or IMAGE_MIN_SCALE are not None, then it scales
# up before padding. IMAGE_MAX_DIM is ignored in this mode.
# The multiple of 64 is needed to ensure smooth scaling of feature
# maps up and down the 6 levels of the FPN pyramid (2**6=64).
# crop: Picks random crops from the image. First, scales the image based
# on IMAGE_MIN_DIM and IMAGE_MIN_SCALE, then picks a random crop of
# size IMAGE_MIN_DIM x IMAGE_MIN_DIM. Can be used in training only.
# IMAGE_MAX_DIM is not used in this mode.
IMAGE_RESIZE_MODE = "square"
IMAGE_MIN_DIM = 800
IMAGE_MAX_DIM = 1024
How I understand it. When you train or predict this configuration will be used without you having to resize it manually. Ofcourse if you have different sizes and ratios of images this can be a problem.
512x512: ratio = 1 so this will upscale to 1024x1024
2054x2456: ratio = 0.836... so this will downscale to 1024x1024 keeping the ratio of 0.836... but using zeropadding to get the square shape.
Where it could go wrong is if a dimension of an object is relatively smaller or bigger in comparison with the different image dimensions which can result in a stretched or compressed object. In this case you should preprocess it manually so that in the end your object is of the same size and shape after the Mask RCNN function has molded it into the right shape.
The Matterplot function is found in "utils.py" and is called "resize_image".
In the "model.py" this is used during training when loading in the data and during inference (detect) to reshape the given numpy-array.

Obtaining the ratio difference of two images

I have an image of the size 640*640*3, while another image of the size 125*314*3. I want to obtain the size ratio of the second image to the first image, but I can't find a way to do it.
I have tried the traditional divide method, as well as using rdivide but both are not working.
If I use the traditional approach of multiplying the image 3D values first, then compare, will the approach be correct?
For example, I would do something like 640*640*3 = 1,228,800 then 125*314*3 = 117,750 and finally, take 117,750 / 1,228,800 = 0.09. Is 0.09 the right answer?
I'm assuming you are referring to the ratio of the areas between the two images. If this is the case, just use the width and height. This looks like you are using RGB images, so don't use the number of channels. However, the number of channels cancels out when you use them in finding the ratio.
Therefore, yes your approach is correct:
(125*314) / (640*640) = 0.0958
This means that the smaller (or second) image occupies roughly 9.5% of the larger (or first) image.
That depends what you mean by size ratio.
Looks like you have RGB images, so if you mean the area, then it is (640*640)/(125*314), if you mean the height, then it is 640/314, more options too, be more specific in your question.

Matlab: Comparing 2 images with different dimension and pixel size

I have 2 images that I need to compare:
Image 1: size [512 x 512] with pixel dimension: 0.41 mm
Image 2: size [210 x 210] with pixel dimension 1 mm
I tried to use: imresize
imresize(Image_1, [210 210]) % to change size/pixel
However it reduce the resolution and image is not clear at all.
Any suggestion will be welcome!
if you meant to test if the two images are identical, instead of resizing the images, you can use filters with different bandwidths. or a higher level feature, such as sift feature, can usually take care of sizing issues because it picks the most interesting scale internally.
vlfeat is a good toolbox if you use matlab.
You always have that problem with comparing two images of different resolutions. I would do a pre-processing of images to make them comparable, maybe something more than just making them of the same size. That pre-processing really depends on your images.
Anyway, perhaps it would be better to re-size the smaller one to a larger version using one of the methods mentioned here: http://www.mathworks.com/help/images/ref/imresize.html and then compare them. For example, I would enlarge the smaller image using 'lanczos3' method.
imresize(Image_2,[512 512],'lanczos3');

Autocorrection between the position of two images

I have two images and have to divide that image. Due to manual error the area in the image is not as same. How can i correct it automatically using matlab so as to perform divison pixel by pixel accurately?
Once you've read both image files into the variables, say A and B, assuming A contains the image of the size you actually want and B contains the image of the size you want to change, you could use:
[numrows numcols] = size(A);
A = imresize(A, [numrows numcols]);

using get_serving_url() to resize SMALLER dimension (width or height) to a specific integer

I'm trying to use get_serving_url() to serve up thumbnails, but I need those those thumbnails' smaller dimension (could be width OR height) to be a certain number so that there won't be any gaps in its container div. According to the docs, the "size" argument for get_serving_url will adjust the LONGEST dimension to a given parameter. This might cause the opposite dimension to go below my required size. How can I fix this?
If you have the dimensions of the bitmap you can request a size that is larger than your smallest value by the factor of the image aspect ratio.
For example if the image is 1600x1200 and you want the thumbnail to be at least 32 pixels in in dimension, the size should be 43 (32*(1600/1200)) and the resulting image will be 43x32.
In order to get the image size you need to load the image date into the image class and use the width and height properties.
There's no way to do this currently; you will have to fetch the dimensions of the image and calculate the size of the longer size accordingly. You probably should do this anyway, or a 1x1000 image will break things for everyone.

Resources