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:
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
Undefined Reference to Symbol '_Zn5Boost6System15System_Categoryev' Error
Plug-In Architecture Based C/C++ Application
Sdl Fake Fullscreen Mode on Dual Monitor Setup Under Linux
Absence of Typeof Operator in C++03
C++ Makefile on Linux with Multiple *.Cpp Files
What's the Difference Between Cout<<Cout and Cout<<&Cout in C++
How to Take Snapshot in Linux - Programmatically C++
How to Find How Much Memory Is Shared Between Forked Process with Copy-On-Write in Linux
How to Set the Background Image in Qt Stylesheet
How to Embed Node.Js Interpreter into C/C++
Why Have Unary_Function, Binary_Function Been Removed from C++11
Can a C Compiler Rearrange Stack Variables
Random Numbers for Multiple Threads
Raw Socket Promiscuous Mode Not Sniffing What I Write
Why Can't Operator () of Stateless Functor Be Static
For-Loop in C++ Using Double Breaking Out One Step Early, Boundary Value Not Reached