How to Resize an Image to a Specific Size in Opencv

Resize an image without distortion OpenCV

You may try below. The function will keep the aspect rate of the original image.

def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
(h, w) = image.shape[:2]

# if both the width and height are None, then return the
# original image
if width is None and height is None:
return image

# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the
# dimensions
r = height / float(h)
dim = (int(w * r), height)

# otherwise, the height is None
else:
# calculate the ratio of the width and construct the
# dimensions
r = width / float(w)
dim = (width, int(h * r))

# resize the image
resized = cv2.resize(image, dim, interpolation = inter)

# return the resized image
return resized

Here is an example usage.

image = image_resize(image, height = 800)

How to resize an image to a specific size in OpenCV?

You can use cvResize. Or better use c++ interface (eg cv::Mat instead of IplImage and cv::imread instead of cvLoadImage) and then use cv::resize which handles memory allocation and deallocation itself.

Resize multiple images with OpenCV to square size without padding

This answer assumes you are able to use Pillow (since I can't comment to ask), which makes this so much more simple.

Pillows Image.resize function allows you to pass in a box that you want the resized image to come from, which is exactly what you are looking for.

From the docs:

Image.resize(size, resample=None, box=None, reducing_gap=None)[source]¶
Returns a resized copy of this image.

docs

Parameters

  • size – The requested size in pixels, as a 2-tuple: (width, height).

  • box – An optional 4-tuple of floats providing the source image region to be scaled. The values must be within (0, 0, width, height) rectangle. If omitted or None, the entire source is used.

Here's my solution

from PIL import Image

def smart_resize(input_image, new_size):
width = input_image.width
height = input_image.height

# Image is portrait or square
if height >= width:
crop_box = (0, (height-width)//2, width, (height-width)//2 + width)
return input_image.resize(size = (new_size,new_size),
box = crop_box)

# Image is landscape
if width > height:
crop_box = ((width-height)//2, 0, (width-height)//2 + height, height)

return input_image.resize(size = (new_size,new_size),
box = crop_box)

Here's how it works, and since a picture is worth a thousand words, here's a picture of what it does:
Sample Image

It checks for portrait or landscape because in portrait, the crop area fills the width and is offset from the height; vice versa in landscape. You could probably do it in one statement with clever min and max statements if you really wanted.

How to resize image size (use opencv resize function)

You said that you already know the coordinates of red rectangle. So, create a (region of interest) ROI using rect(). And, then resize its size using the resize() of openCV.

Mat original_image;
----
----
----
Rect region_of_interest(x, y, w, h);
Mat image_roi = image(region_of_interest);

Size size(5,1); //the dst image size,e.g.5 x 1
Mat dst; //dstimage
resize(image_roi ,dst,size); //resize image


Related Topics



Leave a reply



Submit